コード例 #1
0
        public static HeaderConfig BuildHeaderConfig(Dictionary <string, string> headers)
        {
            if (!headers.Any())
            {
                return(null);
            }
            else
            {
                var cfg = new HeaderConfig
                {
                    CustomHeaders = new Dictionary <string, string>()
                };

                foreach (var h in headers)
                {
                    cfg.CustomHeaders.Add(h.Key, h.Value);
                }

                return(cfg);
            }
        }
コード例 #2
0
ファイル: Helper.cs プロジェクト: mr-kg/KGSoft.TinyHttpClient
 /// <summary>
 /// Create a DELETE request without expecting a type returned
 /// </summary>
 /// <param name="url">The URL we want to make the request to</param>
 /// <param name="body">The body we are sending with the request</param>
 /// <param name="tkn">The cancellation token we want to use with this request</param>
 /// <param name="config">Configuration unique to this request. Used to override what is defined in HttpConfig</param>
 /// <returns></returns>
 public static Task <Response> DeleteAsync(string url, string body = "", CancellationToken tkn = default(CancellationToken), HeaderConfig config = default(HeaderConfig))
 => MakeHttpRequest(url, HttpMethod.Delete, body, tkn: tkn, cfg: config);
コード例 #3
0
ファイル: Helper.cs プロジェクト: mr-kg/KGSoft.TinyHttpClient
 /// <summary>
 /// Create a PUT request expecting T in the response
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="url">The URL we want to make the request to</param>
 /// <param name="body">The body we are sending with the request</param>
 /// <param name="tkn">The cancellation token we want to use with this request</param>
 /// <param name="config">Configuration unique to this request. Used to override what is defined in HttpConfig</param>
 /// <returns></returns>
 public static Task <Response <T> > PutAsync <T>(string url, string body, CancellationToken tkn = default(CancellationToken), HeaderConfig config = default(HeaderConfig))
 => MakeHttpRequest <T>(url, HttpMethod.Put, body, tkn: tkn, cfg: config);
コード例 #4
0
ファイル: Helper.cs プロジェクト: mr-kg/KGSoft.TinyHttpClient
        /// <summary>
        /// Create and send an HttpRequestMessage with HttpContent
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="content"></param>
        /// <param name="tkn"></param>
        /// <param name="cfg"></param>
        /// <returns></returns>
        private static async Task <HttpResponseMessage> GetResponseMessage(string url, HttpMethod method, HttpContent content = null, CancellationToken tkn = default, HeaderConfig cfg = default)
        {
            HttpRequestMessage request = new HttpRequestMessage(method, url);

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(HttpConfig.MediaTypeHeader));

            LogHelper.LogMessage($"[{method.Method}]: {url}");

            // Prepare the headers as per the config passed through
            if (cfg?.AuthHeader == null && HttpConfig.DefaultAuthHeader != null)
            {
                request.Headers.Authorization = HttpConfig.DefaultAuthHeader;
            }
            else if (cfg?.AuthHeader != null)
            {
                request.Headers.Authorization = cfg.AuthHeader;
            }

            // Prepare the custom headers as per the config passed through
            if (cfg?.CustomHeaders == null && HttpConfig.CustomHeaders != null)
            {
                foreach (var kvp in HttpConfig.CustomHeaders)
                {
                    request.Headers.Add(kvp.Key, kvp.Value);
                }
            }
            else if (cfg?.CustomHeaders != null)
            {
                foreach (var kvp in cfg.CustomHeaders)
                {
                    request.Headers.Add(kvp.Key, kvp.Value);
                }
            }

            // Prepare the content
            if (content != null)
            {
                request.Content = content;
            }

            // Fire any pre-auth delegates before sending the request
            HttpConfig.PreRequestAuthAction?.Invoke();
            if (HttpConfig.PreRequestAuthAsyncFunc != null)
            {
                await HttpConfig.PreRequestAuthAsyncFunc();
            }

            var message = await HttpClientPool.Client.SendAsync(request, tkn);

            // If we have any 401 actions defined, evaluate the result and fire them
            await HandlePostRequestUnauthorizedActions(message.StatusCode);

            return(message);
        }
コード例 #5
0
ファイル: Helper.cs プロジェクト: mr-kg/KGSoft.TinyHttpClient
 /// <summary>
 /// Create and send an HttpRequestMessage with a string body
 /// </summary>
 /// <param name="url"></param>
 /// <param name="method"></param>
 /// <param name="body"></param>
 /// <param name="tkn"></param>
 /// <param name="cfg"></param>
 /// <returns></returns>
 private static Task <HttpResponseMessage> GetResponseMessage(string url, HttpMethod method, string body = "", CancellationToken tkn = default, HeaderConfig cfg = default)
 => GetResponseMessage(url, method, CreateContent(body), tkn, cfg);
コード例 #6
0
ファイル: Helper.cs プロジェクト: mr-kg/KGSoft.TinyHttpClient
 /// <summary>
 /// Create a GET request without expecting a type returned
 /// </summary>
 /// <param name="url">The URL we want to make the request to</param>
 /// <param name="tkn">The cancellation token we want to use with this request</param>
 /// <param name="config">Configuration unique to this request. Used to override what is defined in HttpConfig</param>
 /// <returns></returns>
 public static Task <Response> GetAsync(string url, CancellationToken tkn = default(CancellationToken), HeaderConfig config = default(HeaderConfig))
 => MakeHttpRequest(url, HttpMethod.Get, string.Empty, tkn: tkn, cfg: config);
コード例 #7
0
ファイル: Helper.cs プロジェクト: mr-kg/KGSoft.TinyHttpClient
        /// <summary>
        /// Makes an HttpRequest without expecting a response type
        /// </summary>
        /// <param name="url">Url to which the request is made</param>
        /// <param name="method">HttpMethod</param>
        /// <param name="body">Desired body</param>
        /// <param name="content">Desired content</param>
        /// <param name="tkn">Cancellation token for request</param>
        /// <param name="cfg">Additional config</param>
        /// <returns></returns>
        internal static async Task <Response> MakeHttpRequest(string url, HttpMethod method, string body = "", HttpContent content = null, CancellationToken tkn = default, HeaderConfig cfg = default)
        {
            var message = content == null
                ? await GetResponseMessage(url, method, body, tkn, cfg)
                : await GetResponseMessage(url, method, content, tkn, cfg);

            var response = await message.BuildResponse();

            HandleLogging(response);

            return(response);
        }