4

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 Answers1

-1

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

  • Thanks for the answer. Interface inheritance does not really apply to my question though. I don't have any API endpoints I want to share across multiple services/interfaces. My question is if it makes sense to define multiple interfaces for different API endpoints / REST resources with the same base URL. From the example in the link it seems like this is a practice though, so I think I'll split up my interfaces based on their usage. – Justin Hehli Dec 23 '21 at 07:40
  • This does not answer what the OP has asked in the first place – Vihanga Bandara Nov 07 '22 at 14:01