Home > Software engineering >  ASP.NET Core Web API - How to Consume 3rd party API using HttpClient
ASP.NET Core Web API - How to Consume 3rd party API using HttpClient

Time:05-20

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.

CodePudding user response:

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;
    }
}
  • Related