返回带有通用反序列化值的HTTP状态代码的方法

huangapple go评论54阅读模式
英文:

Method to return Http Status Code with Generic deserialized value

问题

I want to return HttpStatus code form the method as well. Something like below

我也想从这个方法中返回 HttpStatus 状态码,类似以下的方式:

public async Task<(T, HttpStatusCode)> Get<T>(string clientName, string resource, CancellationToken cancellationToken)
{
    var client = ConfigureHttpClient(clientName);
    var result = await client.GetFromJsonAsync<T>(resource, cancellationToken: cancellationToken);
    // 返回类似以下的内容
    return (result, result.StatusCode);
}

但不确定如何从中获取状态码。

但不确定如何从中获取状态码。

英文:

I am writing a method to get the de serialized result from a HttpClient call as below

public async Task&lt;T&gt; Get&lt;T&gt;(string clientName, string resource, CancellationToken cancellationToken)
{
    var client = ConfigureHttpClient(clientName);
    return await client.GetFromJsonAsync&lt;T&gt;(resource, cancellationToken: cancellationToken);
}

private HttpClient ConfigureHttpClient(string clientName)
{
    var client = _clientFactory.CreateClient(clientName);
    client.Timeout = TimeSpan.FromSeconds(Convert.ToDouble(_configuration[&quot;HttpClientSettings:Timeout&quot;]));
    return client;
}

Now I want to return HttpStatus code form the method as well. Something like below

public async Task&lt;(T,HttpStatusCode)&gt; Get&lt;T&gt;(string clientName, string resource, CancellationToken cancellationToken)
{
    var client = ConfigureHttpClient(clientName);
    var result = await client.GetFromJsonAsync&lt;T&gt;(resource, cancellationToken: cancellationToken);
    //return something like below
    return(result, result.Statuscode)
}

but not sure how to get the status code form it.

答案1

得分: 1

在这种情况下,您需要使用GetAsync方法,并从中获取响应和状态码。使用忽略状态码,仅关心结果的扩展方法是行不通的。

类似于:

public async Task<(T, HttpStatusCode)> Get<T>(string clientName, string resource, CancellationToken cancellationToken)
{
    var client = ConfigureHttpClient(clientName);
    var response = await client.GetAsync(resource, cancellationToken: cancellationToken);
    var content = await response.Content.ReadFromJsonAsync<T>(cancellationToken: cancellationToken);
    return (content!, response.StatusCode);
}
英文:

In that case you need to use GetAsync method and from that get the response and status code. Using extension method that ditches the status code and only care about the result won't work.

Something like:

public async Task&lt;(T,HttpStatusCode)&gt; Get&lt;T&gt;(string clientName, string resource, CancellationToken cancellationToken)
{
    var client = ConfigureHttpClient(clientName);
    var response = await client.GetAsync(resource, cancellationToken: cancellationToken);
    var content = await response.Content.ReadFromJsonAsync&lt;T&gt;(cancellationToken: cancellationToken);
    return (content!, response.StatusCode);
}

答案2

得分: 1

以下是翻译好的代码部分:

以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻译好的代码部分:

```csharp
以下是翻

<details>
<summary>英文:</summary>

The following ready to use class wraps the standard `System.Net.Http.HttpClient` and supports generic usage:

    using Newtonsoft.Json;
    using System;
    using System.Net;
    using System.Net.Http;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Test
    {
        class HttpClientWrapper
        {
            private readonly HttpClient httpClient;
    
            public HttpClientWrapper(HttpClient httpClient)
            {
                this.httpClient = httpClient;
            }
    
            public async Task&lt;Result&lt;T&gt;&gt; GetAsync&lt;T&gt;(string requestUri, CancellationToken ct)
            {
                try
                {
                    string response = await httpClient.GetStringAsync(requestUri, ct);
                    T? obj = JsonConvert.DeserializeObject&lt;T&gt;(response);
                    return new Result&lt;T&gt;(obj);
                }
                catch (HttpRequestException ex)
                {
                    return new Result&lt;T&gt;(ex.StatusCode);
                }
                catch (Exception)
                {
                    // Log the exception.
                    throw;
                }
            }
        }
    
        class Result&lt;T&gt;
        {
            public Result(T? value)
            {
                Value = value;
                StatusCode = null;
            }
    
            public Result(HttpStatusCode? statusCode)
            {
                Value = default;
                StatusCode = statusCode;
            }
    
            public T? Value { get; }
            public HttpStatusCode? StatusCode { get; }
        }
    }


Please note that

1) `GetAsync()` method returns the deserialized object and a `null` `HttpStatusCode` if the HTTP call succeeds.
2) `GetAsync()` method returns a `null` deserialized object and `HttpStatusCode` if the call throws an `HttpRequestException`.
3) I *do not* prefer using tuples in public APIs since tuples are more appropriate for internal implementation, hence I defined the `Result&lt;T&gt;` class.

You may make an objection to the `GetAsync()` method since *it returns HTTP status code only when the HTTP call fails*. For me this is no problem since normally I will not be interested in the status code when the HTTP call is successful. If you insist on returning the status code even when the call is successful then this is certainly doable.

</details>



huangapple
  • 本文由 发表于 2023年6月6日 13:10:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定