I'm building a front end that interfaces with a 3rd party API, and my question is regarding the proper pattern to use with that third party API.
My existing code isn't very sophisticated, and I find that I'm writing the same code over and over again.
var httpClient = //dependency injection here
var response = await httpClient.GetAsync($"{httpClient.BaseAddress}/Web/Session/{_sessionKey}");
if (!response.IsSuccessStatusCode)
{
//throw exception
}
var result = await response.Content.ReadAsStringAsync();
var model = SessionModel.FromJson(result);
What's different from one response to the next is that it could be a POST, and/or the endpoint, with possibly different parameters. And of course the JSON objects that are returned also depend on what was sent. There is a 1-1 correspondence of endpoint to the JSON object getting returned (which I would then convert to a model).
I've looked around to see how to handle this in terms of a pattern, but most of what I find is directions and advice if you are the API server, not the API consumer. I did find this, but it's not exactly asking the same question, though the answer it gives is a bit closer to what I'm looking for.
So, is there an API consumer pattern that deals with sending a GET/POST, handles different endpoints and parameters, checks the response for validity/errors and returns a proper error response, and/or converts the (non-error) JSON to the appropriate model?
Thanks!