2017-07-12 4 views
1

Ich habe zwei Entitäten 'Personen' und 'Unternehmen' und beide eine Unterressource (zB Standorte)Filterparameter in Rest-API

So habe ich Endpunkte:

GET /persons/{id}/locations 
GET /businesses/{id}/locations 

I Now möchte, dass ein Endpunkt Standorte einer Hauptressource filtert. Wenn ich tun:

GET /persons/{id}/locations?country={...} 
GET /businesses/{id}/locations?country={...} 

werde ich Orte einer bestimmten Person/Unternehmen suchen.

Was ist die beste Praxis zu filtern Standorte aller Personen ich einige Ideen:

1. GET /persons/locations?country={...} 
2. GET /locations?entity=persons&country={...} 

aber nicht sicher, ob diese in Ordnung sind.

Antwort

0

Sie können mit dem Erweiterungskonzept arbeiten, in dem Sie die Beziehung zwischen den Entitäten verwenden können.

GET /persons 

{ 
    "data": [ 
     {"person_id": 1}, 
     {"person_id": 2} 
], 
"links": [ 
    { 
     "rel": "locations", 
     "href": "/person/1/locations" 
    }, 
    { 
     "rel": "locations", 
     "href": "/person/2/locations" 
    } 
    ] 
} 

GET /persons?expand=locations&country={...} 

{ 
    "data": [ 
     {"person_id": 1, "locations": [...]}, 
     {"person_id": 2, "locations": [...]} 
    ] 
} 
Verwandte Themen