示例#1
0
        /// <summary>
        /// Perform a DELETE call
        /// </summary>
        /// <param name="client">HTTP client</param>
        /// <param name="endpoint">uri endpoint</param>
        /// <param name="headers">authentication cookie</param>
        /// <param name="body">body data</param>
        /// <param name="parameters">parameters</param>
        /// <returns>web response</returns>
        public static async Task <bool> DeleteAsync(HttpClient client, Uri endpoint, WebHeaders headers, object body, object parameters = null)
        {
            var uri = CombineUri(endpoint, parameters);

            using var httpResponse = await SendRequest(client, uri, HttpMethod.Delete, headers, body);

            var response = new WebResponse(httpResponse);

            response.EnsureSuccessStatusCode(httpResponse);
            return(await ConvertContent <bool>(response, await httpResponse.Content.ReadAsStreamAsync()));
        }
示例#2
0
 internal WebResponse(HttpResponseMessage response)
 {
     StatusCode      = (int)response.StatusCode;
     ReasonPhrase    = response.ReasonPhrase;
     RequestMethod   = response.RequestMessage.Method.ToString();
     RequestUri      = response.RequestMessage.RequestUri;
     ContentHeaders  = new WebHeaders(response.Content.Headers);
     RequestHeaders  = new WebHeaders(response.RequestMessage.Headers);
     ResponseHeaders = new WebHeaders(response.Headers);
     ContentType     = GetContentType();
 }
示例#3
0
        /// <summary>
        /// Sets up the HttpClient request header.
        /// </summary>
        /// <param name="client">http client</param>
        /// <param name="headers">http headers</param>
        /// <param name="body">body data</param>
        private static void SetupRequestHeader(HttpClient client, WebHeaders headers, object body)
        {
            client.DefaultRequestHeaders.Accept.Clear();

            if (body != null)
            {
                switch (body)
                {
                case byte[] _:
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(WebContentType.Octet));
                    break;

                case string _:
                    var data = body.ToString().TrimStart();
                    switch (data[0])
                    {
                    case '{':
                    case '[':
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(WebContentType.Json));
                        break;

                    case '<':
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(WebContentType.Xml));
                        break;

                    default:
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(WebContentType.Text));
                        break;
                    }
                    break;
                }

                client.DefaultRequestHeaders.TryAddWithoutValidation("Access-Control-Allow-Origin", "*");
            }

            headers.ForEach(x => client.DefaultRequestHeaders.TryAddWithoutValidation(x.Key, x.Value));
        }
示例#4
0
 public ApiRequest()
 {
     Headers = new WebHeaders();
 }
示例#5
0
 public Task <bool> DeleteAsync(Uri endpoint, TBody body, WebHeaders headers = null, object parameters = null) =>
 DeleteAsync <TBody>(endpoint, body, headers, parameters);
示例#6
0
 public Task <bool> DeleteAsync(string endpoint, TBody body, WebHeaders headers = null, object parameters = null) =>
 DeleteAsync(new Uri(endpoint), body, headers, parameters);
示例#7
0
 public Task <TResponse> PutAsync(Uri endpoint, TBody body, WebHeaders headers = null, object parameters = null) =>
 PutAsync <TBody, TResponse>(endpoint, body, headers, parameters);
示例#8
0
 public Task <TResponse> PutAsync(string endpoint, TBody body, WebHeaders headers = null, object parameters = null) =>
 PutAsync(new Uri(endpoint), body, headers, parameters);
示例#9
0
 public Task <TResponse> GetAsync(Uri endpoint, WebHeaders headers = null, object parameters = null) =>
 GetAsync <TResponse>(endpoint, headers, parameters);
示例#10
0
 public Task <TResponse> GetAsync(string endpoint, WebHeaders headers = null, object parameters = null) =>
 GetAsync(new Uri(endpoint), headers, parameters);
示例#11
0
 public ApiRequest(WebHeaders headers)
 {
     Headers = headers ?? new WebHeaders();
 }
示例#12
0
        protected Task <bool> DeleteAsync <TIn>(Uri endpoint, TIn body, WebHeaders headers = null, object parameters = null)
        {
            var json = JsonConvert.SerializeObject(body);

            return(WebRequestImpl.DeleteAsync(_client, endpoint, headers ?? Headers, json, parameters));
        }
示例#13
0
 protected Task <TOut> GetAsync <TOut>(Uri endpoint, WebHeaders headers = null, object parameters = null)
 {
     return(WebRequestImpl.GetAsync <TOut>(_client, endpoint, headers ?? Headers, parameters));
 }
示例#14
0
 /// <summary>
 /// WebRequestBase constructor
 /// </summary>
 /// <param name="headers">request headers</param>
 protected WebRequestBase(WebHeaders headers)
 {
     _client = new HttpClient();
     Headers = headers ?? new WebHeaders();
 }
示例#15
0
        /// <summary>
        /// Issue a http request
        /// </summary>
        /// <param name="client">HTTP client</param>
        /// <param name="uri">endpoint uri</param>
        /// <param name="method">http method</param>
        /// <param name="headers">http headers</param>
        /// <param name="body">body data</param>
        /// <exception cref="NotImplementedException"></exception>
        /// <returns>response message</returns>
        private static async Task <HttpResponseMessage> SendRequest(HttpClient client, Uri uri, HttpMethod method, WebHeaders headers, object body)
        {
            SetupRequestHeader(client, headers, body);

            switch (method.Method)
            {
            case var x when x == HttpMethod.Get.Method:
                return(await client.GetAsync(uri));

            case var x when x == HttpMethod.Post.Method:
            {
                var content = body != null
                        ? body is byte[] bytes
                            ? new ByteArrayContent(bytes)
                            : new StringContent(body.ToString(), Encoding.UTF8, WebContentType.Json)
                        : null;

                return(await client.PostAsync(uri, content));
            }

            case var x when x == HttpMethod.Put.Method:
            {
                var content = body != null
                        ? body is byte[] bytes
                            ? new ByteArrayContent(bytes)
                            : new StringContent(body.ToString(), Encoding.UTF8, WebContentType.Json)
                        : null;

                return(await client.PutAsync(uri, content));
            }

            case var x when x == HttpMethod.Delete.Method:
                return(await client.DeleteAsync(uri));
            }

            throw new MissingMethodException(nameof(method));
        }