With the demand for applications to consume services internal and external to their own systems and the flexibility offered in using a Service Oriented based Architecture we have found it helpful to have a base class that manages most of the work in connecting to a service. Allowing developers to focus on working with the methods and data within those services instead of communication protocols and error handling.
Our Web API Base class does just this and is available under an MIT open source license on Nuget under SoundPower.ServiceClient or at https://www.nuget.org/packages/SoundPower.ServiceClient/.
It provides a set of simple constructors to initiate the class and point it at the root of the targeted web service along with a simple method to use for adding headers to all calls made using that instance of the class and to set the UI language (ex : “en-US”) to communicate with the target service. From there it includes synchronous and asynchronous methods to make DELETE, GET, POST, and PUT calls to the targeted service. It also includes a special method for making a GET call that receives binary data using a special binary response class. With the exception of the binary GET method, all of these methods return the response as a string to be de-serialized per the developers needs. Serialization and deserialization utilities are also provided to make this a near cookie-cutter process.
Each method call is analyzed for a response. If a 500 response is returned, the header is scanned for a “LoggedErrorNumber”, and if it is found a special Service Fault Response Exception is thrown pointing to that error number for tracing. If a 402 response is returned with “IsSPSRsp” in the header, that header value is deserialized into one of our Response Base classes and a BadServiceRequest Exception is thrown with that response. Lastly, if a 429 response is received a TooManyRequests exception is thrown for making it easy to spot issues from overloading the target service. All other HTTP failure responses result in the standard HTTPException result.