Card 10/ 11

ExerciseDifficulty: Advanced1 min

Practice: The Resources of a Ride-Sharing App

Riders, drivers, trips and ratings; name the resources, the paths, a cancellation and a search for nearby drivers.

Designing paths for a domain that looks nothing like a catalogue tests whether the method travels. A ride-sharing app has riders and drivers. A rider requests a trip, a driver accepts it, and after the trip each side may rate the other. Either may cancel a trip before it starts, and the rider's app shows available drivers within two kilometres.

Design the API's paths and methods for these operations:

  1. A rider requests a trip.
  2. Read one trip.
  3. A driver accepts a trip.
  4. A rider cancels a trip, giving a reason.
  5. A rider rates a trip.
  6. List a rider's past trips, newest first.
  7. Find available drivers near a location.
Show one good design
Each operation, its method and path, and why
OperationMethod and pathWhy
Request a tripPOST /tripsCreates a trip; the server assigns its id
Read a tripGET /trips/{id}One canonical path per trip
Driver acceptsPOST /trips/{id}/acceptanceRecords who accepted and when
Rider cancelsPOST /trips/{id}/cancellationKeeps the trip, and stores the reason
Rate a tripPOST /trips/{id}/ratingsA rating belongs to one trip
A rider's past tripsGET /trips?riderId=31&status=completed&sort=-endedAtA filtered view of trips, not a second collection
Nearby driversGET /drivers?status=available&near=40.41,-3.70&radiusKm=2Location and radius are filters on drivers

PATCH /trips/{id} with a new status is a fair alternative for acceptance if no details are kept. GET /riders/31/trips is a fair alternative for past trips, as a read-only view.