/// <summary>
        /// Creates a HttpClient object for every endpoint
        /// </summary>
        /// <param name="url">The URL to the end point</param>
        /// <param name="customHeaders">Any custom headers that needs to be added to the HttpClient</param>
        /// <param name="timeOutInMilliseconds">Http time in milliseconds, will be default to 100,000ms (10s)</param>
        /// <returns>The HttpClient instance</returns>
        public static HttpClient GetOrCreateClient(string url, Dictionary <string, string> customHeaders = null, int timeOutInMilliseconds = 10000)
        {
            var uri = new Uri(url);

            var client = new HttpClient()
            {
                BaseAddress = uri,
                MaxResponseContentBufferSize = int.MaxValue,
                Timeout = TimeSpan.FromMilliseconds(timeOutInMilliseconds),
            };

            customHeaders?.ToList().ForEach(h => client.AddOrUpdateHeader(h.Key, h.Value));
            return(client);
        }
Example #2
0
        public static void AddHeadersToRequest(this HttpClient client, IDictionary <string, object> clientHeaders)
        {
            HandleLanguageSettings(clientHeaders);

            foreach (KeyValuePair <string, object> header in clientHeaders)
            {
                try
                {
                    client.AddOrUpdateHeader(header);
                }
                catch (Exception ex)
                {
                    LogFactory.Log.Error($"Fatal error adding headers to request : {ex}");
                    throw;
                }
            }
        }