Ejemplo n.º 1
0
        private async Task <T> PatchRequest <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.PatchRequest | 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.PatchAsync(endpoint, new StringContent(jsonContent)).Result;
                System.Diagnostics.Trace.WriteLine(string.Format("PATCH {0}: {1}", httpResponse.RequestMessage.RequestUri, jsonContent));
                httpResponse.EnsureSuccessStatusCode();
                PostResponseStub <T> contentResponse = await httpResponse.Content.ReadAsAsync <PostResponseStub <T> >();

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

                return(contentResponse.Response);
            }
        }
Ejemplo n.º 2
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);
            }
        }