Esempio n. 1
0
        /// <summary>
        /// Uploads the image asynchronously to imgur.com
        /// </summary>
        /// <param name="imagePath">Path to the image file.</param>
        /// <returns>A <c>Task</c> object containing the <see cref="ImageInfo"/>.</returns>
        /// <exception cref="WebException">Thrown if imgur.com returns any status code other than <see cref="HttpStatusCode.OK"/>.</exception>
        public Task <ImageInfo> UploadAsync(string imagePath)
        {
            // http://api.imgur.com/endpoints/image#image-upload

            var request = new RestRequest("image", Method.POST);

            request.AddParameter("image", Convert.ToBase64String(File.ReadAllBytes(imagePath)), ParameterType.RequestBody);

            return(client.ExecuteAsyncTask <dynamic>(request, response =>
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return new JsonFx.Json.JsonReader().Read(response.Content);
                }

                throw new WebException(response.Content);
            }).ContinueWith(previousTask =>
            {
                dynamic uploadResponse = previousTask.Result;

                return new ImageInfo
                {
                    Id = uploadResponse.data.id,
                    Url = uploadResponse.data.link,
                    DeleteHash = uploadResponse.data.deletehash,
                };
            }));
        }
        /// <summary>
        /// Uploads the image asynchronously to imgur.com
        /// </summary>
        /// <param name="imagePath">Path to the image file.</param>
        /// <returns>A <c>Task</c> object containing the <see cref="ImageInfo"/>.</returns>
        /// <exception cref="WebException">Thrown if imgur.com returns any status code other than <see cref="HttpStatusCode.OK"/>.</exception>
        public Task <ImageInfo> UploadImage(string imagePath)
        {
            // http://api.imgur.com/endpoints/image#image-upload

            var request = new RestRequest("image", Method.POST);

            request.AddParameter("image", Convert.ToBase64String(File.ReadAllBytes(imagePath)), ParameterType.RequestBody);

            return(client.ExecuteAsyncTask <dynamic>(request, response =>
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return new JsonFx.Json.JsonReader().Read(response.Content);
                }
                if (response.StatusCode == HttpStatusCode.Forbidden && Regex.IsMatch(response.Content, "access token.*?invalid|expired"))
                {
                    client.Authenticator = RefreshToken();
                    return UploadImage(imagePath).Result;
                }

                throw new WebException(response.Content);
            }).ContinueWith(previousTask =>
            {
                var info = previousTask.Result as ImageInfo;
                if (info != null)
                {
                    return info;
                }

                dynamic uploadResponse = previousTask.Result;

                return new ImageInfo
                {
                    Id = uploadResponse.data.id,
                    Url = uploadResponse.data.link,
                    DeleteHash = uploadResponse.data.deletehash,
                };
            }));
        }