![](/img/trans.png)
[英]How to fetch data from a third party API in ASP.NET Core 6 Web API
[英]ASP.NET Core Web API - How to pass specified fields as payload to third party API
我在 ASP.NET Core-6 Web API 中有這個第三方 api
所以,我這樣做了:
appsettings.json:
"Endpoints": {
"EmployeeUrl": "http://api.thirdpartycompany.com:2233/api/EmployeeDetail"
}
DTO:
public class EmployeeDataRequest
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmployeeCode { get; set; }
public decimal Salary { get; set; }
public string Location { get; set; }
public string Email { get; set; }
}
服務:
public class EmployeeService : IEmployeeService
{
private readonly HttpClient _httpClient;
private readonly HttpHelper _httpHelper;
private readonly IConfiguration _config;
private readonly IUnitOfWork _unitOfWork;
private readonly string baseUrl;
public EmployeeService(
HttpClient httpClient,
HttpHelper httpHelper,
IUnitOfWork _unitofwork,
)
{
_httpClient = httpClient;
_httpHelper = httpHelper;
_config = config;
_unitOfWork = _unitofwork;
baseUrl = config.GetSection("Endpoints").GetValue<string>("baseUrl");
}
public async Task<BaseResponse> EmployeeDetail(EmployeeDataRequest payload)
{
var response = new BaseResponse();
using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
try
{
var employee = new Employee
{
FirstName = payload.FirstName,
LastName = payload.LastName,
EmployeeCode = payload.EmployeeCode,
Salary = payload.Salary
Location = payload.Location,
Salary = payload.Salary,
Email = payload.Email
};
await _unitOfWork.Employees.InsertAsync(employee);
await _unitOfWork.Save();
var headers = new Dictionary<string, string>();
headers.Add("Authorization", $"Bearer {token.access_token}");
var employeeEndPoint = baseUrl + _config.GetSection("Endpoints").GetValue<string>("EmployeeUrl");
var httpResponse = _httpHelper.PostOrPutRequest(uri: employeeEndPoint, methodType: HttpMethod.Post, model: payload, headers: headers).Result;
if (httpResponse != null)
{
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
response = JsonConvert.DeserializeObject<EmployeeDataResponse>(content);
}
else
{
response = new BaseResponse { ResponseCode = httpResponse.StatusCode.ToString(), ResponseDescription = httpResponse.ReasonPhrase };
}
}
transaction.Complete();
}
catch (Exception ex)
{
response = new BaseResponse { response_code = "96", response_description = "Error occured, contact admin" };
}
return response;
}
}
}
我想將數據插入數據庫(員工表),然后將一些字段作為有效負載傳遞給第三方 API(員工Url)。
從上面的EmployeeDetail服務中,我成功地將所有字段插入到 Employee 表中,並將 EmployeeDataRequest 中的所有字段作為有效負載傳遞給第三方 api,就像在httpResponse作為有效負載所做的那樣
但是,我想將這些字段(工資和位置)排除在第三方 api 中(我只想將它們插入到 Employee 表中)。 但傳遞其他字段(名字、姓氏、員工代碼和電子郵件)。
我如何實現這一目標?
謝謝你。
如果在調用EmployeeService
的方法時可以假定EmployeeDataRequest
是在應用程序中用作內部服務調用的 DTO,則不應將其傳遞給外部方。
而是從EmployeeDataRequest
DTO 映射到ThirdPartyEmployeeDetail
DTO。 這是為了解耦每個 DTO 的使用。
因此,如果將來EmployeeDataRequest
DTO 需要一個新的EmployeePosition
屬性,它根本不會影響您的第 3 方 API 調用。
創建一個類:
public class ThirdPartyEmployeeDetail
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmployeeCode { get; set; }
public string Email { get; set; }
}
然后,映射並調用您的第 3 方 API。
var apiPayLoad = new ThirdPartyEmployeeDetail()
{
FirstName = payload.FirstName,
LastName = payload.LastName,
EmployeeCode = payload.EmployeeCode,
Email = payload.Email
};
var httpResponse = _httpHelper.PostOrPutRequest(uri: employeeEndPoint, methodType: HttpMethod.Post, model: apiPayLoad , headers: headers).Result;
映射本身,如果要在您對第三方服務的其他 API 調用中重復,您可以將邏輯提取到另一個類中,以便在將EmployeeDataRequest
作為參數傳遞時轉換並返回ThirdPartyEmployeeDetail
。
您創建一個包含與第 3 方關聯的屬性的接口
public interface IEmployeeDataRequestForThirdParty
{
string FirstName { get; set; }
string LastName { get; set; }
string EmployeeCode { get; set; }
string Email { get; set; }
}
public class EmployeeDataRequest : IEmployeeDataRequestForThirdParty
{
public string FirstName { get; set; };
public string LastName { get; set; };
public string EmployeeCode { get; set; };
public decimal Salary { get; set; };
public string Location { get; set; };
public string Email { get; set; };
}
在將其發送到您的帖子之前,您可以投射
_httpHelper.PostOrPutRequest(uri: employeeEndPoint, methodType: HttpMethod.Post, model: (IEmployeeDataRequestForThirdParty) payload, headers: headers).Result;
因此該對象將僅使用接口中公開的屬性進行序列化。
我沒有對此進行測試,但是這種方法應該可以工作,另一種選擇是您可以創建一個 3rd 方的基類,然后您可以從 EmployeeDataRequest 繼承
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.