Mark successful siesta response as error


Mark successful siesta response as error



I'm working with a really strange (and nasty) API that I have no control over, and unfortunately when an invalid request is made, instead of responding with a 4xx status, it responds with a 200 status instead.



With this response, it also changes the response body from the usual XML response to plain text, but does not change the content type header. You can imagine how annoying this is!



I've got Siesta working with the API and the fact that it is no actually RESTful in the slightest, but I'm unsure how to get the next part working - handling the unsuccessful requests.



How do I go about transforming a technically valid and successful 200 response, into an error response? Right now I have the following setup:


configure("/endpoint") {
$0.mutateRequests { req in
... perform some mutation to request ...
}
$0.pipeline[.parsing].add(self.XMLTransformer)
}

configureTransformer("/endpoint") {
($0.content as APIResponse)
.data()
.map(Resource.init)
}



This is working just fine when the response actually is XML, however in the scenario where the response is an error, I receive the following:


bad api request: invalid api key



or something similar to this. The XMLParser class is already handling this, and in turn marks itself as having come across an error, however I don't know how to make Siesta realise that there is an error, and to not call my transformer but instead mark the request as failed to I can handle the error elsewhere.



How can I achieve what I'm after?




1 Answer
1



configureTransformer is just a common-case shortcut for the full-featured (but more verbose) arbitrary transformers Siesta’s pipeline supports. Full transformers can arbitrarily convert any response to any other, including success → failure and failure → success. The user guide discusses this a bit.


configureTransformer



You can see this in action in the example project, which has a customer transformer that does something very similar to what you want, turning a 404 failure into a success with the content false. It is configured here and defined here. That example does a failure → success transformation, but you should find the code adaptable for your success → failure purposes.


false





Thank you! Think I've managed to get it working :D
– PaReeOhNos
Oct 20 '17 at 21:50






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?