我正在实现一个RESTful API,该API调用第三方服务,该服务将返回XML响应,例如失败:
bind = '0.0.0.0:5000'
workers = 2
accesslog = '-'
loglevel = 'development'
capture_output = True
enable_stdio_inheritance = True
当第三方返回失败消息时,我想向我的API用户返回<Status>Failure</Status>
<ErrorContent>One or more do not exist</ErrorContent>
<ErrorNum>20</ErrorNum>
响应。最好的方法是什么?我总是可以检查400
是Status
还是Success
并相应地返回,但是似乎应该有一个更好的方法。
答案 0 :(得分:1)
我认为没有更好的方法可以做到这一点。 XML是您正在使用的服务的专有,因此我不知道任何“内置”的内容可以处理。您可以创建一个负责处理此问题的新类,或者实现一个基本的抽象类,该类可以由所有使用这种XML类型的API控制器继承。
public abstract class myApiController : ControllerBase
{
protected IActionResult Response(string xmlresponse, object responseObject)//your "responseobject", you could remove this and return OKResult instead of OKObjectResult
{
if (IsSuccessXML(xmlresponse))
return new OkObjectResult(responseObject);
return new BadRequestResult();
}
private bool IsSuccessXML(string yourxmlResponse)//you could use the xml object instead of string
{
//read your xml response and "treat the response accordingly", returning true or false; you could also use the response object to show the errors to the users consuming your api
return false;
答案 1 :(得分:0)
我同意其他答案。我会封装对第三方服务的调用,并返回成功或失败。然后,根据每种情况下要执行的操作,返回400或200,或者您需要什么。我认为您无法做任何魔术来改善流程。