Exemplo n.º 1
0
        /// <summary>
        /// Makes a request with the specified method.
        /// </summary>
        /// <param name="endpoint">The endpoint to request</param>
        /// <param name="httpMethod">What http method to use</param>
        /// <param name="payload">The payload to send on a post request</param>
        /// <param name="castPayloadWithoutJsonParsing">Whether to cast the payload to HttpContent directly instead of json parsing.</param>
        /// <param name="httpContent">When the httpContent is set then it will ignore the payload and castPayloadWithoutJsonParsing params.</param>
        /// <returns>The raw response object</returns>
        public async Task <Result <HttpResponseMessage, Error> > GetRawResponseAndEnsureSuccess(
            string endpoint,
            HttpMethods httpMethod             = HttpMethods.Get,
            object payload                     = null,
            bool castPayloadWithoutJsonParsing = false,
            HttpContent httpContent            = null)
        {
            HttpResponseMessage response;

            switch (httpMethod)
            {
            case HttpMethods.Get:
                response = await Client.GetAsync(endpoint).ConfigureAwait(false);

                break;

            case HttpMethods.Post:
            case HttpMethods.Patch:
            case HttpMethods.Put:

                HttpContent content = httpContent ?? (
                    castPayloadWithoutJsonParsing
                            ? (HttpContent)payload
                            : new StringContent(JsonSerializer.Serialize(payload, _jsonOptions), Encoding.UTF8,
                                                "application/json"));

                response = httpMethod switch
                {
                    HttpMethods.Post => await Client.PostAsync(endpoint, content).ConfigureAwait(false),
                    HttpMethods.Put => await Client.PutAsync(endpoint, content).ConfigureAwait(false),
                    HttpMethods.Patch => await Client.PatchAsync(endpoint, content).ConfigureAwait(false),
                    // ReSharper disable once UnreachableSwitchArmDueToIntegerAnalysis
                    _ => null     // Just for the sake of it :P
                };
                break;

            case HttpMethods.Delete:
                if (payload == null)
                {
                    response = await Client.DeleteAsync(endpoint).ConfigureAwait(false);

                    break;
                }

                HttpRequestMessage requestMessage = new HttpRequestMessage()
                {
                    Content = new StringContent(JsonSerializer.Serialize(payload, _jsonOptions), Encoding.UTF8,
                                                "application/json"),
                    Method     = HttpMethod.Delete,
                    RequestUri = Client.BaseAddress == null
                            ? new Uri(endpoint)
                            : new Uri(Client.BaseAddress, endpoint)
                };

                response = await Client.SendAsync(requestMessage).ConfigureAwait(false);

                break;

            default:
                throw new ArgumentException("Method not supported", nameof(httpMethod));
            }

            var successMaybe = await response.EnsureSuccessAndProperReturn().ConfigureAwait(false);

            return(successMaybe);
        }