public async Task InvokeAsync(HttpContext context, ScopedRequestMessage scopedRequestMessage)
        {
            using (_logger.BeginScope("ScopedRequestMiddleware executing for user with Claims: {@Claims}.", context.User.Claims)) {
                var cookies = context.Request.Cookies
                              .Where(c => _options.CookiesToCapture.Contains(c.Key, StringComparer.OrdinalIgnoreCase))
                              .ToList();

                _logger.LogDebug("Adding {@Cookies}", cookies);

                foreach (var cookie in cookies)
                {
                    scopedRequestMessage.AddCookie(cookie.Key, cookie.Value);
                }


                var headers = context.Request.Headers
                              .Where(h => h.Key == CachedTransactionOptions.COOKIE_KEY ||
                                     _options.HeadersToCapture.Contains(h.Key, StringComparer.OrdinalIgnoreCase))
                              .ToList();

                _logger.LogDebug("Adding {@Headers}", headers);

                foreach (var header in headers)
                {
                    scopedRequestMessage.AddHeader(header.Key, header.Value);
                }
            }

            await _next(context);
        }
 public QueryApiClient(IHttpClientFactory clientFactory, ITokenService tokenService,
                       ScopedRequestMessage scopedRequestMessage)
 {
     HttpClient = clientFactory.CreateClient(ClientName);
     tokenService.AssignTokenAsync(HttpClient).Wait();
     _scopedRequestMessage = scopedRequestMessage;
 }
 protected CrudApiClient(IHttpClientFactory clientFactory, ITokenService tokenService, ScopedRequestMessage scopedRequestMessage)
     : base(clientFactory, tokenService, scopedRequestMessage)
 {
 }
        /// <summary>
        /// Convenience method for invoking a DELETE request.
        /// This method returns a StatusCodeResult.
        /// NOTE: The type parameter is not used, except to differentiate the method from other extension methods;
        ///       for documentation purposes only, use the entity class associated with the deleted record.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="client">The HttpClient making the request</param>
        /// <param name="relativeUrlFromBase">Relative URL from the HttpClient's base url</param>
        /// <param name="msg">An optional starting Message (derived from HttpRequestMessage),
        /// which allow prepopulating headers, cookies (as headers), and query string (as Properties["QueryString"])</param>
        /// <returns>Response status code as StatusCodeResult</returns>
        /// <see cref="Delete{T}(HttpClient, string, ScopedRequestMessage)"/>
        public static async Task <StatusCodeResult> DeleteAsync <T>(this HttpClient client, string relativeUrlFromBase, ScopedRequestMessage msg = null)
        {
            msg ??= new ScopedRequestMessage();
            msg.Method     = HttpMethod.Delete;
            msg.RequestUri = new Uri(Url.Combine(client.BaseAddress.ToString(), relativeUrlFromBase) + (msg.Properties["QueryString"] ?? ""));
            var response = await client.SendAsync(msg);

            return(new StatusCodeResult((int)response.StatusCode));
        }
 /// <summary>
 /// Convenience method for invoking a DELETE request.
 /// This method returns a StatusCodeResult.
 /// NOTE: The type parameter is not used, except to differentiate the method from other extension methods;
 ///       for documentation purposes only, use the entity class associated with the deleted record.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="client">The HttpClient making the request</param>
 /// <param name="relativeUrlFromBase">Relative URL from the HttpClient's base url</param>
 /// <param name="msg">An optional starting Message (derived from HttpRequestMessage),
 /// which allow prepopulating headers, cookies (as headers), and query string (as Properties["QueryString"])</param>
 /// <returns>Response status code as StatusCodeResult</returns>
 /// <see cref="DeleteAsync{T}(HttpClient, string, ScopedRequestMessage)"/>
 public static StatusCodeResult Delete <T>(this HttpClient client, string relativeUrlFromBase, ScopedRequestMessage msg = null)
 => DeleteAsync <T>(client, relativeUrlFromBase, msg).Result;
        /// <summary>
        /// Convenience method for invoking a GET request and deserializing the result from JSON.
        /// This method returns an ObjectResult, which encapsulates
        /// both the response status code and the deserialized response body.
        /// </summary>
        /// <typeparam name="TResponseBody"></typeparam>
        /// <param name="client">The HttpClient making the request</param>
        /// <param name="relativeUrlFromBase">Relative URL from the HttpClient's base url</param>
        /// <param name="msg">An optional starting Message (derived from HttpRequestMessage),
        /// which allow prepopulating headers, cookies (as headers), and query string (as Properties["QueryString"])</param>
        /// <returns>ObjectResult instance holding response status code and deserialized response body</returns>
        /// <see cref="Get{TResponseBody}(HttpClient, string, ScopedRequestMessage)"/>
        public static async Task <ObjectResult <TResponseBody> > GetAsync <TResponseBody>(this HttpClient client, string relativeUrlFromBase, ScopedRequestMessage msg = null)
        {
            msg ??= new ScopedRequestMessage();
            msg.Method     = HttpMethod.Get;
            msg.RequestUri = new Uri(Url.Combine(client.BaseAddress.ToString(), relativeUrlFromBase) + (msg.Properties["QueryString"] ?? ""));
            var response = await client.SendAsync(msg);

            var objResult = await GenerateObjectResult <TResponseBody>(response);

            return(objResult);
        }
 /// <summary>
 /// Convenience method for invoking a PATCH request.
 /// This method returns an ObjectResult, which encapsulates
 /// both the response status code and the deserialized response body.
 /// </summary>
 /// <typeparam name="TRequestBody">The type of the request body</typeparam>
 /// <typeparam name="TResponseBody">The type of the response body</typeparam>
 /// <param name="client">The HttpClient making the request</param>
 /// <param name="relativeUrlFromBase">Relative URL from the HttpClient's base url</param>
 /// <param name="msg">An optional starting Message (derived from HttpRequestMessage),
 /// which allow prepopulating headers, cookies (as headers), and query string (as Properties["QueryString"])</param>
 /// <returns>ObjectResult instance holding response status code and deserialized response body</returns>
 /// <see cref="PatchAsync{TBody}(HttpClient, string, TBody, ScopedRequestMessage)"/>
 public static ObjectResult <TResponseBody> Patch <TRequestBody, TResponseBody>(this HttpClient client, string relativeUrlFromBase, TRequestBody body, ScopedRequestMessage msg = null)
 => PatchAsync <TRequestBody, TResponseBody>(client, relativeUrlFromBase, body, msg).Result;
 /// <summary>
 /// Convenience method for invoking a GET request and deserializing the result from JSON.
 /// This method returns an ObjectResult, which encapsulates
 /// both the response status code and the deserialized response body.
 /// </summary>
 /// <typeparam name="TResponseBody"></typeparam>
 /// <param name="client">The HttpClient making the request</param>
 /// <param name="relativeUrlFromBase">Relative URL from the HttpClient's base url</param>
 /// <param name="msg">An optional starting Message (derived from HttpRequestMessage),
 /// which allow prepopulating headers, cookies (as headers), and query string (as Properties["QueryString"])</param>
 /// <returns>ObjectResult instance holding response status code and deserialized response body</returns>
 /// <see cref="GetAsync{TResponseBody}(HttpClient, string, ScopedRequestMessage)"/>
 public static ObjectResult <TResponseBody> Get <TResponseBody>(this HttpClient client, string relativeUrlFromBase, ScopedRequestMessage msg = null)
 => GetAsync <TResponseBody>(client, relativeUrlFromBase, msg).Result;
 /// <summary>
 /// Convenience method for invoking a PUT request.
 /// This method returns an ObjectResult, which encapsulates
 /// both the response status code and the deserialized response body.
 /// </summary>
 /// <typeparam name="TBody">The type of the request body and response body (here assumed to be the same)</typeparam>
 /// <param name="client">The HttpClient making the request</param>
 /// <param name="relativeUrlFromBase">Relative URL from the HttpClient's base url</param>
 /// <param name="msg">An optional starting Message (derived from HttpRequestMessage),
 /// which allow prepopulating headers, cookies (as headers), and query string (as Properties["QueryString"])</param>
 /// <returns>ObjectResult instance holding response status code and deserialized response body</returns>
 /// <see cref="PutAsync{TBody}(HttpClient, string, TBody, ScopedRequestMessage)"/>
 public static ObjectResult <TBody> Put <TBody>(this HttpClient client, string relativeUrlFromBase, TBody body, ScopedRequestMessage msg = null)
 => PutAsync(client, relativeUrlFromBase, body, msg).Result;
Esempio n. 10
0
 public ScopedRequestPropagatingHandler(ScopedRequestMessage scopedRequestMessage)
 {
     _scopedRequestMessage = scopedRequestMessage;
     CookieContainer       = new CookieContainer();
 }