I'm looking for a way to make a generic set of methods in web api 2 that I can use with an http client to hook up to another server.
First, an explanation of my infrastructure. A simplified set up typically employed at my company (omitting database servers and other things) consists of two main servers. There is a web layer server that is used to host apps and sites, and an app layer server that contains services/apis etc.
House rules dictate that an application sitting on the web server cannot directly talk to anything on the app server without going through a proxy or broker service first.
My current solution to this problem is to create an SDK web api service which contains copies of all the routes in the api. The app (in this case an angular app) calls this SDK service via a REST api call, and the job of this intermediate service is to use the .NET HttpClient to translate this call into a call to the "actual" api service running on the other server.
This works great and I've had no problems with it so far, but I get the feeling that there might be a better way to do this sort of stuff. I've searched online for articles describing this kind of thing but have come up short, and I'm by no means an expert at using .NET or web api.
I'll give an example of the solution I have just now - a call to get all the clients that are currently stored in my database.
public class ClientsController: ApiController
{
// http client used to translate calls to api service
private HttpClient httpClient = new HttpClient()
{
BaseAddress = new Uri(ConfigurationManager.AppSettings["apiUrl"])
};
[Route("clients")]
[HttpGet]
public IHttpActionResult GetClients()
{
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json");
List<Client> clients = null;
try
{
HttpResponseMessage result = httpClient.GetAsync("clients").Result;
if (result.IsSuccessStatusCode)
{
clients = result.Content.ReadAsAsync<List<Client>>().Result;
return Content(result.StatusCode, clients);
}
// return the failed reason code
return Content(result.StatusCode, result.ReasonPhrase);
}
catch (Exception e)
{
return Content(HttpStatusCode.InternalServerError, e.Message);
}
}
}
This route is then implemented in my api service that is running on the other server, and retrieves the clients from the database, returns that list of clients back to this service to then return to my application to be displayed on the screen.
The pair of methods (one as shown above plus real implementation) are present for every route in the api.
Ideally, I'd like my sdk service to contain one method for each HTTP verb, where it takes the route that is passed to it, calls that route from the api service and handles the return of items or result codes back to it, similar to what it does now, except I would be able to add a new set of endpoints to the api without having to add them in two places.
And this is where I'm stuck. Any insight into either how this could be done or any alternatives that I may not have explored would be great.
Thank you for your time in reading this long question.