Пример #1
0
        /// <summary>
        /// Reset Token
        /// </summary>
        /// <param name="token">Token Account</param>
        /// <returns>ImgurToken new token</returns>
        public async Task <ImgurToken> ResetToken(ImgurToken token)
        {
            //Console.WriteLine("RefreshToken token");
            bool checkToken = await CheckToken(token);

            if (checkToken == true)
            {
                return(token);
            }
            using (HttpClient client = new HttpClient())
            {
                SetHeader(client);
                //get token
                client.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded");
                var formContent = new FormUrlEncodedContent(new[] {
                    new KeyValuePair <string, string>("client_id", clientID),
                    new KeyValuePair <string, string>("client_secret", clientSecret),
                    new KeyValuePair <string, string>("grant_type", "refresh_token"),
                    new KeyValuePair <string, string>("refresh_token", token.Refresh_token)
                });

                HttpResponseMessage response = await client.PostAsync(new Uri(UrlToken), formContent);
                await CheckHttpStatusCode(response);

                string content = await response.Content.ReadAsStringAsync();

                System.Console.WriteLine(content);
                ImgurToken deleteRoot = JsonConvert.DeserializeObject <ImgurToken>(content);
                return(deleteRoot);
            }
        }
Пример #2
0
        /// <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);
            }
        }
Пример #3
0
 /// <summary>
 /// Setup Headers
 /// </summary>
 /// <param name="client">HttpClient</param>
 /// <param name="token">Token Account</param>
 /// <returns></returns>
 void SetHeader(HttpClient client, ImgurToken token = null)
 {
     if (token != null)
     {
         client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.Access_token);
     }
     else
     {
         client.DefaultRequestHeaders.Add("Authorization", "Client-ID " + clientID);
     }
     if (this.keyMashape != null)
     {
         client.DefaultRequestHeaders.Add("X-Mashape-Key", this.keyMashape);
     }
 }
Пример #4
0
        /// <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);
            }
        }
Пример #5
0
        /// <summary>
        /// Get list ImgurImage from account
        /// </summary>
        /// <param name="token">Token Account</param>
        /// <returns>List ImgurImage</returns>
        public async Task <List <ImgurImage> > GetImages(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/"));
                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);
            }
        }
Пример #6
0
        /// <summary>
        /// Check Token
        /// </summary>
        /// <param name="token">Token Account(</param>
        /// <returns>bool token expires</returns>
        public async Task <bool> CheckToken(ImgurToken token)
        {
            //Console.WriteLine("Check token");
            //get token
            using (HttpClient client = new HttpClient())
            {
                SetHeader(client, token);
                client.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded");
                HttpResponseMessage response = await client.GetAsync(new Uri(SecretUrl));

                if ((int)response.StatusCode == 200)
                {
                    return(true);
                }
                return(false);
            }
        }
Пример #7
0
        /// <summary>
        /// Removes images from album
        /// </summary>
        /// <param name="key">DeleteHash or Id of Image, obtained when creating album</param>
        /// <param name="imageIds">ALL images must be here, imgur will otherwise remove the ones missing</param>
        /// <param name="token">Token account</param>
        /// <returns></returns>
        public async Task <bool> RemoveImagesFromAlbum(string key, IEnumerable <string> imageIds, ImgurToken token = null)
        {
            using (HttpClient client = new HttpClient())
            {
                SetHeader(client, token);

                HttpResponseMessage response = await client.DeleteAsync(new Uri(BaseUrl + "album/" + key + "/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);
            }
        }
Пример #8
0
        /// <summary>
        /// Add images to excisting album
        /// </summary>
        /// <param name="key">DeleteHash or Id of Image, obtained when creating album</param>
        /// <param name="imageIds">ALL images must be here, imgur will otherwise remove the ones missing</param>
        /// <param name="token">Token account</param>
        /// <returns></returns>
        public async Task <bool> AddImagesToAlbum(string key, IEnumerable <string> imageIds, 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))
                });

                HttpResponseMessage response = await client.PostAsync(new Uri(BaseUrl + "album/" + key), formContent);

                await CheckHttpStatusCode(response);

                string content = await response.Content.ReadAsStringAsync();

                ImgurRootObject <bool> addRoot = JsonConvert.DeserializeObject <ImgurRootObject <bool> >(content);

                return(addRoot.Data);
            }
        }
Пример #9
0
        /// <summary>
        /// Updates ImgurAlbum
        /// </summary>
        /// <param name="key">DeleteHash or Id of Image, 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>
        /// <param name="token">Token account</param>
        /// <returns>bool of result</returns>
        public async Task <bool> UpdateAlbum(string key, IEnumerable <string> imageIds, string title, string description, ImgurAlbumPrivacy privacy, ImgurAlbumLayout layout, string cover, 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", cover),
                });

                HttpResponseMessage response = await client.PutAsync(new Uri(BaseUrl + "album/" + key), formContent);
                await CheckHttpStatusCode(response);

                string content = await response.Content.ReadAsStringAsync();

                ImgurRootObject <bool> updateRoot = JsonConvert.DeserializeObject <ImgurRootObject <bool> >(content);

                return(updateRoot.Data);
            }
        }
Пример #10
0
        /// <summary>
        /// Update Image
        /// </summary>
        /// <param name="deleteHash">DeleteHash  or Id 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> UpdateImage(string key, string title, string description, ImgurToken token = null)
        {
            using (HttpClient client = new HttpClient())
            {
                SetHeader(client, token);

                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/" + key), formContent);
                await CheckHttpStatusCode(response);

                string content = await response.Content.ReadAsStringAsync();

                ImgurRootObject <bool> deleteRoot = JsonConvert.DeserializeObject <ImgurRootObject <bool> >(content);

                return(deleteRoot.Data);
            }
        }
Пример #11
0
        /// <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);
            }
        }