Exemple #1
0
        private async Task <Tuple <T, Dictionary <string, TE> > > GetRequestWithExpansions <T, TE>(string baseAddress, string endpoint)
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(baseAddress);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                if (LoginType == LoginType.Anonymous)
                {
                    endpoint = string.Format("{0}{2}APIKey={1}", endpoint, smugmugTokenManager.ConsumerKey, endpoint.Contains('?') ? "&" : "?");
                }
                else if (LoginType == LoginType.OAuth)
                {
                    smugmugConsumer = new DesktopConsumer(smugmugServiceDescription, smugmugTokenManager);
                    HttpDeliveryMethods resourceHttpMethod = HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest;

                    var resourceEndpoint = new MessageReceivingEndpoint(baseAddress + endpoint, resourceHttpMethod);
                    var httpRequest      = smugmugConsumer.PrepareAuthorizedRequest(resourceEndpoint, smugmugTokenManager.AccessToken);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", httpRequest.Headers["Authorization"].Substring(6));
                }
                else
                {
                    throw new NotSupportedException(string.Format("LoginType {0} is unsupported", LoginType));
                }

                HttpResponseMessage httpResponse = client.GetAsync(endpoint).Result;
                System.Diagnostics.Trace.WriteLine(string.Format("GET {0}", httpResponse.RequestMessage.RequestUri));
                httpResponse.EnsureSuccessStatusCode();
                GetResponseWithExpansionStub <T, TE> contentResponse = await httpResponse.Content.ReadAsAsync <GetResponseWithExpansionStub <T, TE> >();

                System.Diagnostics.Trace.WriteLine(string.Format("---{0}:{1}", contentResponse.Code, contentResponse.Message));

                return(new Tuple <T, Dictionary <string, TE> >(contentResponse.Response, contentResponse.Expansions));
            }
        }
Exemple #2
0
        private async Task <ImageUpload> UploadImage(string albumUri, string fileName, byte[] image, CancellationToken cancellationToken)
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(SMUGMUG_API_v2_UploadEndpoint);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("X-Smug-AlbumUri", albumUri);
                client.DefaultRequestHeaders.Add("X-Smug-FileName", fileName);
                client.DefaultRequestHeaders.Add("X-Smug-ResponseType", "JSON");
                client.DefaultRequestHeaders.Add("X-Smug-Version", "v2");

                if (LoginType == LoginType.OAuth)
                {
                    smugmugConsumer = new DesktopConsumer(smugmugServiceDescription, smugmugTokenManager);
                    HttpDeliveryMethods resourceHttpMethod = HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest;

                    var resourceEndpoint = new MessageReceivingEndpoint(SMUGMUG_API_v2_UploadEndpoint, resourceHttpMethod);
                    var httpRequest      = smugmugConsumer.PrepareAuthorizedRequest(resourceEndpoint, smugmugTokenManager.AccessToken);

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", httpRequest.Headers["Authorization"].Substring(6));
                }
                else
                {
                    throw new NotSupportedException(string.Format("LoginType {0} is unsupported", LoginType));
                }

                var content = new StreamContent(new MemoryStream(image));

                HttpResponseMessage httpResponse = client.PostAsync(SMUGMUG_API_v2_UploadEndpoint, content, cancellationToken).Result;
                System.Diagnostics.Trace.WriteLine(string.Format("POST {0}", httpResponse.RequestMessage.RequestUri));
                httpResponse.EnsureSuccessStatusCode();
                ImagePostResponse contentResponse = await httpResponse.Content.ReadAsAsync <ImagePostResponse>();

                System.Diagnostics.Trace.WriteLine(string.Format("---{0} {1}: {2}", contentResponse.Stat, contentResponse.Method, contentResponse.Image));

                return(contentResponse.Image);
            }
        }
Exemple #3
0
        private async Task <T> PostRequest <T>(string baseAddress, string endpoint, string jsonContent)
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(baseAddress);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                if (LoginType == LoginType.Anonymous)
                {
                    endpoint = string.Format("{0}{2}APIKey={1}", endpoint, smugmugTokenManager.ConsumerKey, endpoint.Contains('?') ? "&" : "?");
                }
                else if (LoginType == LoginType.OAuth)
                {
                    smugmugConsumer = new DesktopConsumer(smugmugServiceDescription, smugmugTokenManager);
                    HttpDeliveryMethods resourceHttpMethod = HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest;

                    var resourceEndpoint = new MessageReceivingEndpoint(baseAddress + endpoint, resourceHttpMethod);
                    var httpRequest      = smugmugConsumer.PrepareAuthorizedRequest(resourceEndpoint, smugmugTokenManager.AccessToken);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", httpRequest.Headers["Authorization"].Substring(6));
                }
                else
                {
                    throw new NotSupportedException(string.Format("LoginType {0} is unsupported", LoginType));
                }

                HttpResponseMessage httpResponse = client.PostAsync(endpoint, new StringContent(jsonContent)).Result;
                System.Diagnostics.Trace.WriteLine(string.Format("POST {0}: {1}", httpResponse.RequestMessage.RequestUri, jsonContent));

                PostResponseStub <T> contentResponse = null;
                if (httpResponse.IsSuccessStatusCode)
                {
                    contentResponse = await httpResponse.Content.ReadAsAsync <PostResponseStub <T> >();
                }
                else
                {
                    var     failedResponse    = httpResponse.Content.ReadAsStringAsync();
                    JObject response          = JObject.Parse(failedResponse.Result);
                    var     invalidParameters =
                        from p in response["Options"]["Parameters"]["POST"]
                        where p["Problems"] != null
                        select new POSTParameter
                    {
                        ParameterName = (string)p["Name"],
                        Problem       = (string)p["Problems"].First()
                    };

                    if (invalidParameters.Count() > 0)
                    {
                        List <ArgumentException> argumentExceptions = new List <ArgumentException>();
                        foreach (POSTParameter invalidParameter in invalidParameters)
                        {
                            argumentExceptions.Add(new ArgumentException(invalidParameter.Problem, invalidParameter.ParameterName));
                        }
                        throw new AggregateException("HTTP POST Request failed.  See inner exceptions for individual reasons.", argumentExceptions.ToArray());
                    }
                    else
                    {
                        throw new HttpRequestException("HTTP POST Request failed for unknown reasons");
                    }
                }

                System.Diagnostics.Trace.WriteLine(string.Format("---{0} {1}: {2}", contentResponse.Code, contentResponse.Message, contentResponse.Response));

                return(contentResponse.Response);
            }
        }
Exemple #4
0
 public HttpWebRequest PrepareAuthorizedRequest(IDictionary <string, string> parameters)
 {
     return(_consumer.PrepareAuthorizedRequest(_serviceEndPoint, AccessToken, parameters));
 }