Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Developing a REST API - best practice for structure and routing?

$
0
0

Hi all,

I'm developing a simple RESTful API as a prototype.

At the moment I have only 3 repositories:

  1. Clients (/api/clients)
  2. Sites (/api/sites)
  3. Cables (/api/cables)

A client can have many sites (physical locations), and a site can have many cables.

I'm running through the use cases in my head, and I'm getting a little lost with what is considered best practice.

For example, say I'd like to retrieve all cables from a site. I immediately gravitate towards this sort of hierarchical structure:

/api/clients/{client_id}/sites/{site_id}/cables

But then I start to think well hey, what if I'd like to list all cables for a client, instead of a site? In my case, a Cable only knows what Site it belongs to - but not Client.

Would I have to create more routing methods for every use case, like this?

/api/clients/{client_id}/cables

From here I start getting a little lost, as I feel like APIs shouldn't be tightly bound to the relational structure. Should I have something more like this?

/api/cables/client/{client_id}

But this seems backwards to me.

Perhaps a query filter like this?

/api/cables?client_id=1 OR /api/cables/client_name=NASA

Anyway, I hope I'm being clear enough. I'm still getting my head around REST APIs, and as you can see I'm getting a little lost. Hopefully someone can bump me on the right track.

Looking forward to your replies, thanks in advance!


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>