1

In my ASP.NET Core-6 Web API, I am given a third party API to consume and then return the account details.

api:

https://api.thirdpartycompany.com:2233/UserAccount/api/AccountDetail?accountNumber=112123412

Headers:

X-GivenID:Given2211
X-GivenName:Givenyou
X-GivenPassword:Given@llcool

Then JSON Result is shown below:

{
  "AccountName": "string",
  "CurrentBalance": 0,
  "AvailableBalance": 0,
  "Currency": "string"
}

So far, I have done this:

BalanceEnquiryResponse:

public class BalanceEnquiryResponse
{
    public string Response
    {
        get;
        set;
    }

    public bool IsSuccessful
    {
        get;
        set;
    }

    public List<BalanceList> AccountBalances
    {
        get;
        set;
    }
}

BalanceList:

public class BalanceList
{
    public string AccountNumber
    {
        get;
        set;
    }

    public decimal CurrentBalance
    {
        get;
        set;
    }

    public decimal AvailableBalance
    {
        get;
        set;
    }

    public string Currency
    {
        get;
        set;
    }
}

Then the service is shown below.

IDataService:

public interface IDataService
{
    BalanceEnquiryResponse GetAccountBalance(string accountNo);
}

DataService:

public class DataService : IDataService
{
    private readonly ILogger<DataService> _logger;
    private readonly HttpClient _myClient;
    public DataService(ILogger<DataService> logger, HttpClient myClient)
    {
        _logger = logger;
        _myClient = myClient;
    }

    private void PrepareAPIHeaders()
    {
        _myClient.DefaultRequestHeaders.Add("X-GivenID", "Given2211");
        _myClient.DefaultRequestHeaders.Add("X-GivenName", "Givenyou");
        _myClient.DefaultRequestHeaders.Add("X-GivenPassword", "Given@llcool");
        _myClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
        _myClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json; charset=utf-8");
    }

    public BalanceEnquiryResponse GetAccountBalance(string accountNo)
    {
        _logger.LogInformation("Accessing Own Account");
        var url = $"https://api.thirdpartycompany.com:2233/UserAccount/api/AccountDetail?accountNumber={accountNo}";
        var responseResults = new BalanceEnquiryResponse();
        var response = await _myClient.GetAsync(url);
        return response;
    }
}

Using HttpClient, I want to return a response in connection with the url, headers and BalanceEnquiryResponse

For the first time I am trying to consume third party API using HttpClient, and I'm following this Consume Web API in .NET using HttpClient

So far, I got this error:

Cannot implicitly convert type 'System.Net.Http.HttpResponseMessage' to 'BalanceEnquiryResponse'

and response is highlighted in return response

How do I correct the error and also achieve my goal in returning the response.

Thanks.

5
  • This might be of interest to you: Typed clients Commented May 20, 2022 at 11:34
  • If you followed the linked tutorial (which is horrendous, IMHO) you probably missed this line: result.Content.ReadAsAsync<Student[]>(); Your GetAccountBalance method lacks the respective line to extract the payload from the HttpResponse. Commented May 20, 2022 at 11:40
  • @Fildor - Kindly pardon me. I got confused along the way. Can you show me how to apply it to what I have in GetAccountBalance Commented May 20, 2022 at 11:45
  • You are getting response from the HttpClient. But this response is a HttpResponseMessage not your custom BalanceEnquiryResponse. So, what you probably want to do is responseResults = await result.Content.ReadAsAsync<BalanceEnquiryResponse>(); and then return that. Commented May 20, 2022 at 11:53
  • @Fildor - Is it possible for you to help rewrite how everything should be on a code editor. Also putting the headers into consideration. Thanks Commented May 20, 2022 at 11:58

1 Answer 1

1

Based on your class, these small changes should put you in the position to fix the rest:

public class DataService : IDataService
{
    private readonly ILogger<DataService> _logger;
    private readonly HttpClient _myClient;
    public DataService(ILogger<DataService> logger, HttpClient myClient)
    {
        _logger = logger;
        _myClient = myClient;
        PrepareAPIHeaders(); // Actually apply the headers!
    }

    private void PrepareAPIHeaders()
    {
        _myClient.DefaultRequestHeaders.Add("X-GivenID", "Given2211");
        _myClient.DefaultRequestHeaders.Add("X-GivenName", "Givenyou");
        _myClient.DefaultRequestHeaders.Add("X-GivenPassword", "Given@llcool");
        _myClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
        _myClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json; charset=utf-8");
    }

    // If you want to use async API, you need to go async all the way.
    // So make this Method async, too!
    public async Task<BalanceEnquiryResponse> GetAccountBalance(string accountNo)
    {
        _logger.LogInformation("Accessing Own Account");
        var url = $"https://api.thirdpartycompany.com:2233/UserAccount/api/AccountDetail?accountNumber={accountNo}";

        var response = await _myClient.GetAsync(url);
        // vv Get your payload out of the Http Response.
        var responseResults = await response.Content.ReadAsAsync<BalanceEnquiryResponse>();
        return responseResults;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.