ASP.NET Core Refit Client for RESTful API: How to divide clients

Viewed 1036

I'm currently building a client for a RESTful API with ASP.NET Core 5 and Refit (using HttpClientFactory). What I'm a little confused about, is how to divide the API interfaces (how many separate interfaces to write for different API endpoints/ressources).

Let's say we have an API with the following endpoints, each with a few subroutes (e.g. .../{id} or .../{id}/pets) and/or different HTTP verbs: http://myhost/api/customers and http://myhost/api/employees

What's the best practice here, writing one interface IMyHostApi which covers the whole API? Or is it better to divide this into something like IMyHostCustomersApi and IMyHostEmployeesApi and then add multiple Refit clients with the correspinding base addresses?

for context, the client(s) will be added like this:

services
    .AddRefitClient<IGitHubApi>()
    .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.github.com"));
1 Answers

In case if you don't want to have all endpoints in one huge interface you can use this Refit feature - https://github.com/reactiveui/refit#interface-inheritance.

In that case, your base interface will be derived from your splitted interfaces and you will have one entry point for your API. And all endpoints will be logically splitted into separate interfaces

Related