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