Handling responses from a third party API

I’d like to know what’s the recommended (or better than my current approach) way to deal with responses from third party APIs. or map them.

Given the following simple example where a successful call to the externalservice will return:

200 OK succeeded:1111111111

Where is 1111111111 is the ActivationCode in the example.

I’m using HTTP Utils to call the externalservice api with .GetResponseStatus().

Is there a way to return directly the third party response as a ResponseStatus in the SimpleResponse class?

Naively, something like return url.GetResponseStatus(); or better than my current approach?

Right now I’m checking the HttpStatusCode? for null and error code etc.

Here’s my current code:


    namespace SimpleApi.ServiceModel
    {
        [Route("/simple/{Id}/{User}")]
        public class Simple : IReturn<SimpleResponse>
        {
            public string Id { get; set; }
            
            public string User { get; set; }
    
            public string ActivationCode   { get; set; }
        }
        
        public class SimpleResponse
        {
            public string Result { get; set; }
    
            public ResponseStatus ResponseStatus { get; set; }
        }
    }
    
    namespace SimpleApi.ServiceInterface
    {
        public class SimpleServices: Service
        {
            public object Get(Simple request)
            {
                string url = $"https://externalservice.com/activation/{request.Id}/{request.User}?activationCode={request.ActivationCode}";
    
                HttpStatusCode? res = url.GetResponseStatus();
    
                if (statusCode == null)
                {
                    return new SimpleResponse {Result = $"{request.ActivationCode}", ResponseStatus = new ResponseStatus("The request couldn't be processed","Error")};
                }
    
                if (statusCode.IsErrorResponse())
                {
                    return new SimpleResponse {Result = $"{request.ActivationCode}", ResponseStatus = new ResponseStatus(statusCode.ToString(),"Error")};
                }
    
                return new SimpleResponse {Result = $"activated: {request.ActivationCode}"};
            }
        }
    }

Is there a reason why you’re not just returning the response from the 3rd party? e.g:

string url = $"https://externalservice.com/activation/{request.Id}/{request.User}?activationCode={request.ActivationCode}";
return url.GetStringFromUrl(responseFilter:res => Response.ContentType = res.ContentType);

Your Request DTO would then be:

public class Simple : IReturn<string> {}

As you’re not using a Response DTO any C# Exceptions are automatically returned in a generic ErrorResponse class.

Thanks for the quick reply!

My goal is to hide the underlying API as much as possible and make sure that nothing from that API filters through back to the customer or user of the ServiceStack webservice. The API in question is not well documented so I can’t be sure what’s returned when something goes wrong.

The underlying API doesn’t return a content type.

My goal is to strip any custom message (or anything specific to this API) that this API might be returning.

That’s why, initially, I didn’t want to return the response directly. I can easily parse the response in case of success (it’s always the same) and provide a generic success output my concern is when something goes wrong.

For example, if I provide an invalid ID currently the customer will receive this:

{“responseStatus”:{“errorCode”:“WebException”,“message”:“The remote server returned an error: (404) Not Found.”,“errors”:}}

I want to make sure that the message and errors are never populated with custom messages from the underlying API and also that the StatusDescription is always something generic.

Is there a simple way to accomplish this?

I don’t want to do this globally just for certain routes that are calling the underlying external API.

Maybe wrap the GetStringFromUrl() in a Try catch, catch any WebException and throw something generic instead?

Thanks.

Personally I’d only handle the success case in my Service impl, e.g:

string url = $"https://externalservice.com/activation/{request.Id}/{request.User}?activationCode={request.ActivationCode}";
url.GetStringFromUrl();
return new SimpleResponse {Result = $"activated: {request.ActivationCode}"};

Well I don’t see the point of returning the activation code that it was requested with and would personally only return an empty Response DTO, e.g:

return new SimpleResponse();

and let ServiceStack handle returning the Exception back to the API consumer, which is either going to return a 200 for successful response or throw an Exception for >=400 HTTP Responses.

If you want to specifically handle different Exceptions, the HTTP Utils extension methods have useful extension methods off Exception for inspecting the WebException Error response. But I wouldn’t go through that bespoke effort unless you need to.

You are right. Returning the activation code is useless.

I’ll check the extensions.

Thanks!