繁体   English   中英

ASP.NET Core Web API - 如何将指定字段作为有效负载传递给第三方 API

[英]ASP.NET Core Web API - How to pass specified fields as payload to third party API

我在 ASP.NET Core-6 Web API 中有这个第三方 api

http://api.thirdpartycompany.com:2233/api/EmployeeDetail

所以,我这样做了:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM