/// <summary> /// Creates an Album on Imgur /// </summary> /// <param name="imageIds">List of string, ImageIds</param> /// <param name="title">Title of album</param> /// <param name="description">Description of album</param> /// <param name="privacy">Privacy level, use NONE for standard</param> /// <param name="layout">Layout, use NONE for standard</param> /// <param name="coverImageId">Cover image of album, imageId. Should be in the album</param> /// <param name="token">Token account</param> /// <returns>ImgurCreateAlbum which contains deletehash and link</returns> public async Task <ImgurCreateAlbum> CreateAlbum(IEnumerable <string> imageIds, string title, string description, ImgurAlbumPrivacy privacy, ImgurAlbumLayout layout, string coverImageId, ImgurToken token = null) { using (HttpClient client = new HttpClient()) { SetHeader(client, token); var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("ids", imageIds.Aggregate((a, b) => a + "," + b)), new KeyValuePair <string, string>("title", title), new KeyValuePair <string, string>("description", description), new KeyValuePair <string, string>("privacy", GetNameFromEnum <ImgurAlbumPrivacy>((int)privacy)), new KeyValuePair <string, string>("layout", GetNameFromEnum <ImgurAlbumLayout>((int)layout)), new KeyValuePair <string, string>("cover", coverImageId), }); HttpResponseMessage response = await client.PostAsync(new Uri(BaseUrl + "album"), formContent); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <ImgurCreateAlbum> createRoot = JsonConvert.DeserializeObject <ImgurRootObject <ImgurCreateAlbum> >(content); return(createRoot.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>ImgurImage object</returns> public async Task <ImgurImage> UploadImageAnonymous(Stream imageStream, string name, string title, string description) { using (HttpClient client = new HttpClient()) { SetHeaders(client); 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(new Uri(baseUrl + "upload"), jsonContent); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <ImgurImage> imgRoot = JsonConvert.DeserializeObject <ImgurRootObject <ImgurImage> >(content); return(imgRoot.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> /// <param name="token">Token account</param> /// <returns>ImgurImage object</returns> public async Task <ImgurImage> UploadImage(string url, string title, string description, ImgurToken token = null) { using (HttpClient client = new HttpClient()) { SetHeader(client, token); var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("image", url), new KeyValuePair <string, string>("name", "Anonymous"), new KeyValuePair <string, string>("title", title), new KeyValuePair <string, string>("description", description) }); if (token != null) { formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("image", url), new KeyValuePair <string, string>("title", title), new KeyValuePair <string, string>("name", token.Account_username), new KeyValuePair <string, string>("description", description) }); } HttpResponseMessage response = await client.PostAsync(new Uri(BaseUrl + "upload"), formContent); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <ImgurImage> imgRoot = JsonConvert.DeserializeObject <ImgurRootObject <ImgurImage> >(content); return(imgRoot.Data); } }
/// <summary> /// Updates ImgurAlbum /// </summary> /// <param name="deleteHash">DeleteHash, obtained at creation</param> /// <param name="imageIds">List images 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> imageIds, string title, string description, ImgurAlbumPrivacy privacy, ImgurAlbumLayout layout, string cover) { using (HttpClient client = new HttpClient()) { SetHeaders(client); var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("ids", imageIds.Aggregate((a, b) => a + "," + b)), new KeyValuePair <string, string>("title", title), new KeyValuePair <string, string>("description", description), new KeyValuePair <string, string>("privacy", GetNameFromEnum <ImgurAlbumPrivacy>((int)privacy)), new KeyValuePair <string, string>("layout", GetNameFromEnum <ImgurAlbumLayout>((int)layout)), new KeyValuePair <string, string>("cover", cover), }); HttpResponseMessage response = await client.PutAsync(new Uri(baseUrl + "album/" + deleteHash), formContent); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <bool> updateRoot = JsonConvert.DeserializeObject <ImgurRootObject <bool> >(content); return(updateRoot.Data); } }
private async Task CheckHttpStatusCode(HttpResponseMessage responseMessage) { //Imgur StatusCodes var content = await responseMessage.Content.ReadAsStringAsync(); ImgurRootObject <ImgurRequestError> errorRoot = null; try { errorRoot = JsonConvert.DeserializeObject <ImgurRootObject <ImgurRequestError> >(content); } catch (Exception) { } if (errorRoot == null) { return; } if ((int)responseMessage.StatusCode / 100 > 2) { throw new Exception(string.Format(" Error: {0} \n Request: {1} \n Verb: {2} ", errorRoot.Data.Error, errorRoot.Data.Request, errorRoot.Data.Method)); } return; }
/// <summary> /// Get Account /// </summary> /// <param name="token">Token Account</param> /// <returns>Amount Image</returns> public async Task <long> GetImageCount(ImgurToken token = null) { using (HttpClient client = new HttpClient()) { SetHeader(client, token); HttpResponseMessage response = await client.GetAsync(new Uri(BaseUrl + "account/me/images/count")); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <long> imgRoot = JsonConvert.DeserializeObject <ImgurRootObject <long> >(content); return(imgRoot.Data); } }
/// <summary> /// Gets an image from Imgur /// </summary> /// <param name="imageId">Id of Image</param> /// <returns></returns> public async Task <ImgurImage> GetImage(string imageId) { using (HttpClient client = new HttpClient()) { SetHeaders(client); HttpResponseMessage response = await client.GetAsync(new Uri(baseUrl + "image/" + imageId)); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <ImgurImage> imageRoot = JsonConvert.DeserializeObject <ImgurRootObject <ImgurImage> >(content); return(imageRoot.Data); } }
/// <summary> /// Deletes Image from Imgur /// </summary> /// <param name="key">DeleteHash or Id of Image, attained when creating image</param> /// <param name="token">Token account</param> /// <returns>bool of result</returns> public async Task <bool> DeleteImage(string key, ImgurToken token = null) { using (HttpClient client = new HttpClient()) { SetHeader(client, token); HttpResponseMessage response = await client.DeleteAsync(new Uri(BaseUrl + "image/" + key)); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <bool> deleteRoot = JsonConvert.DeserializeObject <ImgurRootObject <bool> >(content); return(deleteRoot.Data); } }
/// <summary> /// Gets an album from Imgur /// </summary> /// <param name="albumId">Id of Album</param> /// <returns></returns> public async Task <ImgurAlbum> GetAlbum(string albumId) { using (HttpClient client = new HttpClient()) { SetHeaders(client); HttpResponseMessage response = await client.GetAsync(new Uri(BaseUrl + "album/" + albumId)); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <ImgurAlbum> albumRoot = JsonConvert.DeserializeObject <ImgurRootObject <ImgurAlbum> >(content); return(albumRoot.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) { using (HttpClient client = new HttpClient()) { SetHeaders(client); HttpResponseMessage response = await client.DeleteAsync(new Uri(baseUrl + "album/" + deleteHash + "/remove_images?ids=" + imageIds.Aggregate((a, b) => a + "," + b))); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <bool> removeRoot = JsonConvert.DeserializeObject <ImgurRootObject <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) { using (HttpClient client = new HttpClient()) { SetHeaders(client); HttpResponseMessage response = await client.DeleteAsync(new Uri(baseUrl + "album/" + deleteHash)); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <bool> deleteRoot = JsonConvert.DeserializeObject <ImgurRootObject <bool> >(content); return(deleteRoot.Data); } }
/// <summary> /// Get list ID image from Album /// </summary> /// <param name="ID">ID Album</param> /// <returns>List String</returns> public async Task <List <ImgurImage> > GetImagesAlbum(string ID) { using (HttpClient client = new HttpClient()) { SetHeader(client); //var formContent = new FormUrlEncodedContent(username); HttpResponseMessage response = await client.GetAsync(new Uri(BaseUrl + "album/" + ID + "/images")); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); Console.WriteLine(content); ImgurRootObject <List <ImgurImage> > imgRoot = JsonConvert.DeserializeObject <ImgurRootObject <List <ImgurImage> > >(content); return(imgRoot.Data); } }
/// <summary> /// Get list ID image from account /// </summary> /// <param name="token">Token Account</param> /// <returns>List String</returns> public async Task <List <string> > GetImageIDs(ImgurToken token = null) { using (HttpClient client = new HttpClient()) { SetHeader(client, token); //var formContent = new FormUrlEncodedContent(username); HttpResponseMessage response = await client.GetAsync(new Uri(BaseUrl + "account/me/images/ids/")); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); Console.WriteLine(content); ImgurRootObject <List <string> > imgRoot = JsonConvert.DeserializeObject <ImgurRootObject <List <string> > >(content); return(imgRoot.Data); } }
/// <summary> /// Get Account /// </summary> /// <param name="username">Username Account</param> /// <returns>ImgurAccount object</returns> public async Task <ImgurAccount> GetAccount(string username = "******") { using (HttpClient client = new HttpClient()) { SetHeader(client); //var formContent = new FormUrlEncodedContent(username); HttpResponseMessage response = await client.GetAsync(new Uri(BaseUrl + "account/" + username)); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <ImgurAccount> imgRoot = JsonConvert.DeserializeObject <ImgurRootObject <ImgurAccount> >(content); return(imgRoot.Data); } }
/// <summary> /// Add images to excisting album /// </summary> /// <param name="deleteHash">DeleteHash, obtained when creating album</param> /// <param name="imageIds">ALL images must be here, imgur will otherwise remove the ones missing</param> /// <returns></returns> public async Task <bool> AddImagesToAlbumAnonymous(string deleteHash, IEnumerable <string> imageIds) { using (HttpClient client = new HttpClient()) { SetHeaders(client); var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("ids", imageIds.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(); ImgurRootObject <bool> addRoot = JsonConvert.DeserializeObject <ImgurRootObject <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) { using (HttpClient client = new HttpClient()) { SetHeaders(client); 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(); ImgurRootObject <bool> deleteRoot = JsonConvert.DeserializeObject <ImgurRootObject <bool> >(content); return(deleteRoot.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>ImgurImage object</returns> public async Task <ImgurImage> UploadImageAnonymous(Stream imageStream, string name, string title, string description) { using (HttpClient client = new HttpClient()) { SetHeaders(client); string base64Image = PhotoStreamToBase64(imageStream); var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("image", base64Image), new KeyValuePair <string, string>("name", name), new KeyValuePair <string, string>("title", title), new KeyValuePair <string, string>("description", description) }); HttpResponseMessage response = await client.PostAsync(new Uri(BaseUrl + "upload"), formContent); await CheckHttpStatusCode(response); string content = await response.Content.ReadAsStringAsync(); ImgurRootObject <ImgurImage> imgRoot = JsonConvert.DeserializeObject <ImgurRootObject <ImgurImage> >(content); return(imgRoot.Data); } }