0

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

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);
}

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!
    }

    public BalanceEnquiryResponse GetAccountBalance(string accountNo)
    {
        _logger.LogInformation("Accessing Own Account");
        string custAccountNoUrl = _config.GetSection("MyEndpoints").GetValue<string>("custAccountNoUrl") + accountNo;
        string xGivenId = _config.GetSection("MyEndpoints").GetValue<string>("xGivenId");
        string xGivenName = _config.GetSection("MyEndpoints").GetValue<string>("xGivenName");
        string xGivenPassword = _config.GetSection("MyEndpoints").GetValue<string>("xGivenPassword");
        var responseResults = new BalanceEnquiryResponse();
        _myClient.DefaultRequestHeaders.Accept.Clear();
        _myClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _myClient.DefaultRequestHeaders.Add("X-GivenID", xGivenId);
        _myClient.DefaultRequestHeaders.Add("X-GivenName", xGivenName);
        _myClient.DefaultRequestHeaders.Add("X-GivenPassword", xGivenPassword);
        HttpResponseMessage response = _myClient.GetAsync(custAccountNoUrl).Result;
        if (response.IsSuccessStatusCode)
        {
            var stringResult = response.Content.ReadAsStringAsync().Result;
            responseResults = JsonSerializer.Deserialize<BalanceEnquiryResponse>(stringResult);
        }
        else
        {
            _logger.LogError((int)response.StatusCode, response.ReasonPhrase);
        }
        return responseResults;
    }
}

I want to map the response from the third party api to BalanceList (AccountBalances). That is AccountNumber, CurrentBalance, AvailableBalance, ...

I observed that responseResults is returning null. But IsSuccessStatusCode returns true.

Also when I viewed stringResult using Convert Json to C# Classes Online

I got this:

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);

public class AdditionalData
{
}

public class Root
{
    public string AccountNumber { get; set; }
    public string AccountName { get; set; }
    public int Rim { get; set; }
    public double CurrentBalance { get; set; }
    public double AvailableBalance { get; set; }
    public string Currency { get; set; }
    public string IsoCurrencyCode { get; set; }
    public object Email_Address { get; set; }
    public object Email_Address2 { get; set; }
    public object Mobile { get; set; }
    public string AccountType { get; set; }
    public object Date { get; set; }}
}

public class RsmInform
{
    public int RsmID { get; set; }
    public string Mobile { get; set; }
    public string EmailAddress { get; set; }
    public string Name { get; set; }
    public string Branch { get; set; }
    public object ImageUrl { get; set; }
    public string Key { get; set; }
    public string StaffID { get; set; }
}

What I really need is in public class Root

How do I re-write

responseResults = JsonSerializer.Deserialize(stringResult);

to map the content in Root of the third party api into BalanceList (AccountBalances)?

Thanks

Ayobamilaye
  • 1,099
  • 1
  • 17
  • 46
  • The response returned is `BalanceList` type. Work with: `JsonSerializer.Deserialize(stringResult);` Meanwhile, try not use `Task.Result` as may lead a deadlock. You may read [Is Task.Result the same as .GetAwaiter.GetResult()?](https://stackoverflow.com/a/38530225/8017690) – Yong Shun May 25 '22 at 11:55
  • @YongShun - How do I do that from the existing code? – Ayobamilaye May 25 '22 at 12:00
  • Do you mean the `Task.Result` or? If you are asking for `Task.Result`, change the method to asynchronous and use `await` instead of `Task.Result`. Or read the link that I shared. Thanks. – Yong Shun May 25 '22 at 12:09

0 Answers0