围绕现有 API 创建网络 api 包装器
creating a web api wrapper around an existing API
Authorize.NET 提供 very thorough SDK..
您只需将它安装在您的解决方案中即可:
Install-Package AuthorizeNet
我们需要服务来包装 AuthorizeNet API 的所有功能。
为简单起见,假设 API 公开了以下方法:
public bool Pay(TransactionModel trans);
public bool Decline(Guid id);
public bool Refund(Guid id);
我们可以从我们自己的解决方案控制器方法中轻松访问这些方法。例如:
[HttpPost]
public bool PayAuthNet([FromBody] AuthnetTransModel model)
{
TransactionModel localTransModel = CreateLocalAuthModel(model);
var authNet = new AuthorizeNet();
return authNet.Pay(localTransModel);
}
但是,API 库非常庞大,Authorize.NET 公开了:
假设我们想包装这些控制器,每个都包装到它自己的微服务中(希望得到对这种方法的反馈),是否有一种更简单的方法来包装这些 API 中的每一个, 强制客户端通过我们的包装器服务,而不是让他们直接点击 Authorize.NET?
这是一个简单的解释,因为评论太长了。
将使用伪代码演示包装 AuthorizeNet
使用 OP
中提供的示例
[HttpPost]
public bool PayAuthNet([FromBody] AuthnetTransModel model)
{
TransactionModel localTransModel = CreateLocalAuthModel(model);
var authNet = new AuthorizeNet();
return authNet.Pay(localTransModel);
}
首先是命名约定。类似于 MVC 如何根据命名约定查找控制器,例如 Name/Action
映射到 NameController.Action
PayAuthNet --> AuthorizeNet.Pay
DeclineAuthNet --> AuthorizeNet.Decline
RefundAuthNet --> AuthorizeNet.Refund
然后使用反射可以确定方法参数类型,并且类似于 AutoMapper 工作方式的映射函数会将提供的模型 AuthnetTransModel
转换为函数的预期 TransactionModel
参数。
假设您可以使用表达式树来摆脱魔法字符串
public class BillingConroller : ApiController {
[HttpPost]
public bool PayAuthNet([FromBody] AuthnetTransModel model) {
return authorizeNetWrapper.Execute<BillingConroller>(c => c.PayAuthNet(model));
}
}
它会在内部检查表达式树以提取映射所需的信息并执行包装的匹配 API
From expression tree:
Method being invoked: PayAuthNet
Argument provider: AuthnetTransModel
After applying convention:
Found matching method: AuthorizeNet.Pay
Expected argument: TransactionModel
Construct command
Create instance of TransactionModel and copy properties from provided model
Invoke => authNet.Pay(localTransModel)
Authorize.NET 提供 very thorough SDK..
您只需将它安装在您的解决方案中即可:
Install-Package AuthorizeNet
我们需要服务来包装 AuthorizeNet API 的所有功能。
为简单起见,假设 API 公开了以下方法:
public bool Pay(TransactionModel trans);
public bool Decline(Guid id);
public bool Refund(Guid id);
我们可以从我们自己的解决方案控制器方法中轻松访问这些方法。例如:
[HttpPost]
public bool PayAuthNet([FromBody] AuthnetTransModel model)
{
TransactionModel localTransModel = CreateLocalAuthModel(model);
var authNet = new AuthorizeNet();
return authNet.Pay(localTransModel);
}
但是,API 库非常庞大,Authorize.NET 公开了:
假设我们想包装这些控制器,每个都包装到它自己的微服务中(希望得到对这种方法的反馈),是否有一种更简单的方法来包装这些 API 中的每一个, 强制客户端通过我们的包装器服务,而不是让他们直接点击 Authorize.NET?
这是一个简单的解释,因为评论太长了。
将使用伪代码演示包装 AuthorizeNet
使用 OP
中提供的示例[HttpPost]
public bool PayAuthNet([FromBody] AuthnetTransModel model)
{
TransactionModel localTransModel = CreateLocalAuthModel(model);
var authNet = new AuthorizeNet();
return authNet.Pay(localTransModel);
}
首先是命名约定。类似于 MVC 如何根据命名约定查找控制器,例如 Name/Action
映射到 NameController.Action
PayAuthNet --> AuthorizeNet.Pay
DeclineAuthNet --> AuthorizeNet.Decline
RefundAuthNet --> AuthorizeNet.Refund
然后使用反射可以确定方法参数类型,并且类似于 AutoMapper 工作方式的映射函数会将提供的模型 AuthnetTransModel
转换为函数的预期 TransactionModel
参数。
假设您可以使用表达式树来摆脱魔法字符串
public class BillingConroller : ApiController {
[HttpPost]
public bool PayAuthNet([FromBody] AuthnetTransModel model) {
return authorizeNetWrapper.Execute<BillingConroller>(c => c.PayAuthNet(model));
}
}
它会在内部检查表达式树以提取映射所需的信息并执行包装的匹配 API
From expression tree:
Method being invoked: PayAuthNet
Argument provider: AuthnetTransModel
After applying convention:
Found matching method: AuthorizeNet.Pay
Expected argument: TransactionModel
Construct command
Create instance of TransactionModel and copy properties from provided model
Invoke => authNet.Pay(localTransModel)