예제 #1
0
 public async Task <HttpResponseMessage> PostAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, HttpContent content = null, string token = null) => await HTTPCallAsync("post", uri, httpOptions, content, token);
예제 #2
0
    private static async Task <HttpResponseMessage> HTTPCallAsync(string method, Uri uri, SwaggerHTTPClientOptions options, HttpContent content = null, string token = null)
    {
        using (var client = CreateClient())
        {
            using (var cancellationTokenSource = new CancellationTokenSource(options.Timeout))
            {
                var errorMessage = string.Empty;
                try
                {
                    if (content != null)
                    {
                        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    }

                    if (!string.IsNullOrWhiteSpace(token))
                    {
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
                    }

                    var response = default(HttpResponseMessage);
                    switch (method.ToUpperInvariant())
                    {
                    case "DELETE":
                    {
                        response = await client.DeleteAsync(uri, cancellationTokenSource.Token);

                        break;
                    }

                    case "POST":
                    {
                        response = await client.PostAsync(uri, content, cancellationTokenSource.Token);

                        break;
                    }

                    case "PUT":
                    {
                        response = await client.PutAsync(uri, content, cancellationTokenSource.Token);

                        break;
                    }

                    case "GET":
                    {
                        response = await client.GetAsync(uri, HttpCompletionOption.ResponseContentRead, cancellationTokenSource.Token);

                        break;
                    }

                    case "HEAD":
                    {
                        response = await client.SendAsync(new HttpRequestMessage
                            {
                                Method     = new HttpMethod(method),
                                RequestUri = uri
                            }, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token);

                        break;
                    }

                    case "OPTIONS":
                    {
                        response = await client.SendAsync(new HttpRequestMessage
                            {
                                Method     = new HttpMethod(method),
                                RequestUri = uri
                            }, HttpCompletionOption.ResponseContentRead, cancellationTokenSource.Token);

                        break;
                    }

                    case "PATCH":
                    {
                        response = await client.SendAsync(new HttpRequestMessage
                            {
                                Method     = new HttpMethod(method),
                                RequestUri = uri,
                                Content    = content
                            }, HttpCompletionOption.ResponseContentRead, cancellationTokenSource.Token);

                        break;
                    }
                    }

    #if DEBUG
                    Debug.WriteLine($"HTTP {method} to {uri} returned {response.StatusCode} with content {await response.Content?.ReadAsStringAsync()}");
    #endif
                    return(response);
                }
                catch (FileNotFoundException) { errorMessage = $"HTTP {method} exception - file not found exception"; /* this can happen if WP cannot resolve the server */ }
                catch (WebException) { errorMessage = $"HTTP {method} exception - web exception"; }
                catch (HttpRequestException) { errorMessage = $"HTTP {method} exception - http exception"; }
                catch (TaskCanceledException) { errorMessage = $"HTTP {method} exception - task cancelled exception"; }
                catch (UnauthorizedAccessException) { errorMessage = $"HTTP {method} exception - unauth exception"; }

    #if DEBUG
                Debug.WriteLine(errorMessage);
    #endif
            }
        }

        return(null);
    }
예제 #3
0
 public async Task <HttpResponseMessage> GetAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, string token = null) => await HTTPCallAsync("get", uri, httpOptions, token : token);