Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RestHttpClient"/> class.
        /// </summary>
        /// <param name="httpClient">The HttpClient from http client factory.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="httpContextAccessor">The HTTP context accessor.</param>
        public RestHttpClient(HttpClient httpClient,
                              ILoggerFactory loggerFactory,
                              RestHttpClientOptions configuration,
                              IHttpContextAccessor?httpContextAccessor)
        {
            _httpClient = httpClient;

            HttpContextAccessor = httpContextAccessor;
            LoggerFactory       = loggerFactory;
            Configuration       = configuration;

            _log = loggerFactory.CreateLogger <RestHttpClient>();

            // To reuse the HttpClient instance, we will use the cancellation token to manage timeouts.
            // To do this, you need to set the main timeout to the maximum value,
            // because it will override the value specified in the cancellation token.
            _httpClient.Timeout = System.Threading.Timeout.InfiniteTimeSpan;

            if (HttpContextAccessor?.HttpContext is not null &&
                HttpContextAccessor.HttpContext.Request.Headers.TryGetValue(AuthorizationHeader, out var authorizeHeader) &&
                !string.IsNullOrEmpty(authorizeHeader))
            {
                var token = authorizeHeader.FirstOrDefault();
                if (token is not null && token.StartsWith(BearerIdentifier, StringComparison.OrdinalIgnoreCase))
                {
                    token = token.Replace(BearerIdentifier, string.Empty).TrimStart();
                    httpClient.SetBearerToken(token);
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="RestHttpClientFromOptions{TOptions}"/> class.
 /// </summary>
 /// <param name="optionsAccessor">The options.</param>
 /// <param name="httpClient">The HttpClient from http client factory.</param>
 /// <param name="loggerFactory">The logger factory.</param>
 /// <param name="configuration">The configuration.</param>
 /// <param name="httpContextAccessor">The HTTP context accessor.</param>
 /// <param name="baseUri">The base Uri of all requests. For example: http://rsm.api.monq.cloud</param>
 public RestHttpClientFromOptions(
     IOptions <TOptions> optionsAccessor,
     HttpClient httpClient,
     ILoggerFactory loggerFactory,
     RestHttpClientOptions configuration,
     IHttpContextAccessor httpContextAccessor,
     string baseUri) : this(httpClient, loggerFactory, configuration, httpContextAccessor, baseUri)
 {
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="RestHttpClientFromOptions{TOptions}"/> class.
        /// </summary>
        /// <param name="httpClient">The HttpClient from http client factory.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="httpContextAccessor">The HTTP context accessor.</param>
        /// <param name="baseUri">The base Uri of all requests. For example: http://rsm.api.monq.cloud</param>
        public RestHttpClientFromOptions(
            HttpClient httpClient,
            ILoggerFactory loggerFactory,
            RestHttpClientOptions configuration,
            IHttpContextAccessor httpContextAccessor,
            string baseUri) : base(httpClient, loggerFactory, configuration, httpContextAccessor)
        {
            if (string.IsNullOrWhiteSpace(baseUri))
            {
                throw new ArgumentNullException(nameof(baseUri), "The base uri not set.");
            }
            BaseUri = AddTrailingSlash(baseUri);

            HttpClient.BaseAddress = new Uri(BaseUri);
        }