response.Content.ReadAsAsync<OperationResponse<T>> vs response.Content.ReadAsAsync<T>() what is the different?

58 views Asked by At

I saw code like below

May I know what is the different and when I show use

  • response.Content.ReadAsAsync<OperationResponse> and return return operationResponse.SuccessResponse;
  • response.Content.ReadAsAsync() and return return await response.Content.ReadAsAsync();
private async Task<T> ReadOperationResponse<T>(HttpResponseMessage response)
        where T : class
        {
            if (!response.IsSuccessStatusCode)
            {
                var rawContent = await response.Content.ReadAsStringAsync();

                if(!string.IsNullOrEmpty(rawContent))
                    throw new ApiException(rawContent);

                var rawContentStream = await response.Content.ReadAsStreamAsync();
                    throw new ApiException(rawContentStream.ToString());
            }

            var operationResponse = await response.Content.ReadAsAsync<OperationResponse<T>>();
            if (!operationResponse.HasSucceeded)
                throw new ApiException(operationResponse.ErrorResponse.Message);

            return operationResponse.SuccessResponse;
        }
private async Task<T> ReadResponse<T>(HttpResponseMessage response)
        where T : class
        {
            if (!response.IsSuccessStatusCode)
            {
                throw new ApiException(response.ReasonPhrase);
            }

            return await response.Content.ReadAsAsync<T>();
        }

Try to understand what is the differnt and when to use either one

***No worries. I think I found the answer. We have an object try to store more information

    public class OperationResponse<T> : IOperationResponse<T> where T : class
{
    public bool HasSucceeded { get; set; }

    public ErrorResponse ErrorResponse { get; set; }

    public T SuccessResponse { get; set; }
}
0

There are 0 answers