/// <summary> /// Gets an album from Imgur /// </summary> /// <param name="albumId">Id of Album</param> /// <returns></returns> public async Task <Album> GetAlbum(string albumId) { HttpResponseMessage response = await client.GetAsync(baseUrl + "album/" + albumId); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ResponseRootObject <Album> albumRoot = JsonConvert.DeserializeObject <ResponseRootObject <Album> >(content); return(albumRoot.Data); }
/// <summary> /// Gets an image from Imgur /// </summary> /// <param name="imageId">Id of Image</param> /// <returns></returns> public async Task <Image> GetImage(string imageId) { HttpResponseMessage response = await client.GetAsync(baseUrl + "image/" + imageId); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ResponseRootObject <Image> imageRoot = JsonConvert.DeserializeObject <ResponseRootObject <Image> >(content); return(imageRoot.Data); }
/// <summary> /// Removes images from album /// </summary> /// <param name="deleteHash">Album DeleteHash, obtained at creation</param> /// <param name="imageIds">List of string, imageIds of images to remove from album</param> /// <returns></returns> public async Task <bool> RemoveImagesFromAlbumAnonymous(string deleteHash, IEnumerable <string> imageIds) { HttpResponseMessage response = await client.DeleteAsync(baseUrl + "album/" + deleteHash + "/remove_images?ids=" + imageIds.Aggregate((a, b) => a + "," + b)); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ResponseRootObject <bool> removeRoot = JsonConvert.DeserializeObject <ResponseRootObject <bool> >(content); return(removeRoot.Data); }
/// <summary> /// Deletes album from Imgur /// </summary> /// <param name="deleteHash">DeleteHash, obtained when creating Album</param> /// <returns></returns> public async Task <bool> DeleteAlbumAnonymous(string deleteHash) { HttpResponseMessage response = await client.DeleteAsync(baseUrl + "album/" + deleteHash); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ResponseRootObject <bool> deleteRoot = JsonConvert.DeserializeObject <ResponseRootObject <bool> >(content); return(deleteRoot.Data); }
/// <summary> /// Add images to excisting album /// </summary> /// <param name="deleteHash">DeleteHash, obtained when creating album</param> /// <param name="imageDeleteHashes">ALL images must be here, imgur will otherwise remove the ones missing</param> /// <returns></returns> public async Task <bool> AddImagesToAlbumAnonymous(string deleteHash, IEnumerable <string> imageDeleteHashes) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("deletehashes", imageDeleteHashes.Aggregate((a, b) => a + "," + b)) }); HttpResponseMessage response = await client.PostAsync(new Uri(baseUrl + "album/" + deleteHash), formContent); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ResponseRootObject <bool> addRoot = JsonConvert.DeserializeObject <ResponseRootObject <bool> >(content); return(addRoot.Data); }
/// <summary> /// Update Image /// </summary> /// <param name="deleteHash">DeleteHash of Image, attained when created</param> /// <param name="title">New title</param> /// <param name="description">New Description</param> /// <returns>bool of result</returns> public async Task <bool> UpdateImageAnonymous(string deleteHash, string title, string description) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("description", description), new KeyValuePair <string, string>("title", title) }); HttpResponseMessage response = await client.PutAsync(new Uri(baseUrl + "image/" + deleteHash), formContent); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ResponseRootObject <bool> deleteRoot = JsonConvert.DeserializeObject <ResponseRootObject <bool> >(content); return(deleteRoot.Data); }
/// <summary> /// Upload Image /// </summary> /// <param name="url">Url to image http://some.url.to.image.com/image.jpg</param> /// <param name="name">Name of image</param> /// <param name="title">Title of image</param> /// <param name="description">Description of image</param> /// <returns>Image object</returns> public async Task <Image> UploadImageAnonymous(string url, string name, string title, string description) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("image", url), new KeyValuePair <string, string>("name", name), new KeyValuePair <string, string>("title", title), new KeyValuePair <string, string>("description", description) }); HttpResponseMessage response = await client.PostAsync(baseUrl + "upload", formContent); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ResponseRootObject <Image> imgRoot = JsonConvert.DeserializeObject <ResponseRootObject <Image> >(content); return(imgRoot.Data); }
/// <summary> /// Updates ImgurAlbum /// </summary> /// <param name="deleteHash">DeleteHash, obtained at creation</param> /// <param name="imageDeleteHashes">List image deletehashes in the album</param> /// <param name="title">New title</param> /// <param name="description">New description</param> /// <param name="privacy">New privacy level, use NONE for standard</param> /// <param name="layout">New layout, use NONE for standard</param> /// <param name="cover">new coverImage, imageId</param> /// <returns>bool of result</returns> public async Task <bool> UpdateAlbumAnonymous(string deleteHash, IEnumerable <string> imageDeleteHashes, string title, string description, AlbumPrivacy privacy, AlbumLayout layout, string cover) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("deleteHashes", imageDeleteHashes.Aggregate((a, b) => a + "," + b)), new KeyValuePair <string, string>("title", title), new KeyValuePair <string, string>("description", description), new KeyValuePair <string, string>("privacy", GetNameFromEnum <AlbumPrivacy>((int)privacy)), new KeyValuePair <string, string>("layout", GetNameFromEnum <AlbumLayout>((int)layout)), new KeyValuePair <string, string>("cover", cover), }); HttpResponseMessage response = await client.PutAsync(baseUrl + "album/" + deleteHash, formContent); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ResponseRootObject <bool> updateRoot = JsonConvert.DeserializeObject <ResponseRootObject <bool> >(content); return(updateRoot.Data); }
/// <summary> /// Upload Image /// </summary> /// <param name="imageStream">Stream of image</param> /// <param name="name">Name of image</param> /// <param name="title">Title of image</param> /// <param name="description">Description of image</param> /// <returns>Image object</returns> public async Task <Image> UploadImageAnonymous(Stream imageStream, string name, string title, string description) { string base64Image = PhotoStreamToBase64(imageStream); var jsonData = JsonConvert.SerializeObject(new { image = base64Image, name, title, description }); var jsonContent = new StringContent(jsonData, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(baseUrl + "upload", jsonContent); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ResponseRootObject <Image> imgRoot = JsonConvert.DeserializeObject <ResponseRootObject <Image> >(content); return(imgRoot.Data); }
private async Task CheckHttpStatusCode(HttpResponseMessage responseMessage) { var content = await responseMessage.Content.ReadAsStringAsync(); ResponseRootObject <RequestError> errorRoot = null; try { errorRoot = JsonConvert.DeserializeObject <ResponseRootObject <RequestError> >(content); } catch (Exception) { } if (errorRoot == null) { return; } if ((int)responseMessage.StatusCode / 100 > 2) { throw new ResponseException(string.Format(" Error: {0} \n Request: {1} \n Verb: {2} ", errorRoot.Data.Error, errorRoot.Data.Request, errorRoot.Data.Method)); } return; }