0

I'm designing a REST API where you can search for data in different countries, but since you can search for the same thing, at the same time, in different countries (max 4), am I unsure of the best/correct way to do it.

This would work to start with to get data (I'm using cars as an example):

/api/uk,us,nl/car/123

That request could return different ids for the different countries (uk=1,us=2,nl=3), so what do I do when data is requested for those 3 countries? For a nice structure I could get the data one at the time:

/api/uk/car/1
/api/us/car/2
/api/nl/car/3

But that is not very efficient since it hits the backend 3 times.

I could do this:

/api/car/?uk=1&us=2&nl=3

But that doesn't work very well if I want to add to that path:

/api/uk/car/1/owner

Because that would then turn into:

/api/car/owner/?uk=1&us=2&nl=3

Which doesn't look good.

Anyone got suggestions on how to structure this in a good way?

AxAn
  • 143
  • 12

1 Answers1

1

I answered a similar question before, so I will stick to that idea: You have a set of elements -cars- and you want to filter it in some way. My advice is add any filter as a field. If the field is not present, then choose one country based on the locale of the client:

mydomain.com/api/v1/car?countries=uk,us,nl

This field should dissapear when you look for a specific car or its owner

mydomain.com/api/v1/car/1/owner

because the country is not needed (unless the car ID 1 is reused for each country)

Update:

I really did not expect the id of the car can be shared by several cars, an ID should be unique (like a primary key in a database). Then, it makes sense to keep the country parameter with the owner's search:

mydomain.com/api/v1/car/1/owner?countries=uk,us

This should return a list of people who own a car with the id 1... but for me this makes little sense as a functionality, in this search I'll only allow one country:

mydomain.com/api/v1/car/1/owner?country=uk
Community
  • 1
  • 1
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
  • The problem is that the car id is reused for countries. For a better example you can say that the ID is the registration of the car and "ABC123" can exist in all countries. – AxAn Nov 10 '16 at 09:40
  • @AxAn that's arguably a design error then - how would you deal with a result where cars from different countries have the same ID? – Pekka Nov 10 '16 at 09:59
  • @AnAn Can you clarify what `/api/car/owner/?uk=1&us=2&nl=3` would do exactly, ignoring the aesthetics for a moment? Is it a *search*, or a *lookup* of one or several data records you know exist? – Pekka Nov 10 '16 at 10:01
  • The problem I have is how to deal with cars from different countries with the same id. `/api/car/owner/?uk=1&us=2&nl=3` would allow me to know for which country to search for the car id. Searching for the same car (licenseplate) in all countries would then be: `/api/car/owner/?uk=abc123&us=abc123&nl=abc123` – AxAn Nov 10 '16 at 10:16
  • If I do `/api/v1/car/1/owner?country=uk` then I cannot use different ids for different countries. In this example uk has id 1 but us could have id 2. – AxAn Nov 10 '16 at 10:23