About the user resources
- Singleton
on the path /users/[user_id]
you can expect several things to happen, for example:
- an unauthorized 401 response if you are not authenticated and only authenticated users are granted access to this user resource.
- a forbidden 403 response if you are not allowed to access the requested user resource.
- a not found 404 response if no user resource with [user_id] exists
- But on success 200 you should get a singleton resource representing the user resource with its identifier [user_id] field.
Each singleton is uniquely identified by its path and identifier and you use these to find the resource. It is not possible to use multiple paths for the same singleton.
- Collection
on the path /users
you will always get a collection of user resources returned.
You can query the path /users
with query parameters (GET
Parameters). This will return a collection with users that meet the requested criteria. The collection that is returned should contain the user resources, all with their identifying resource path in the response. Search parameters could be any field present in the resources of the collection; firstName
, lastName
, id
So for example:
/users?id=1
/users?firstName=John
/users?lastName=Doe
or even a combination of the above:
/users?firstName=John&lastName=Doe
The collection response will always be an array. The array can be empty (no matches), contain one item (conclusive) or several items (inconclusive).
About the email
The email can be either a resource or a property/field of the user resource.
- Email as property of user:
If the field is a property of user the user response would look something like this:
{
id: 1,
firstName: 'John'
lastName: 'Doe'
email: 'john.doe@example.com'
...
}
This means there is no special endpoint for emails, but you can now find a user by its email by sending following request:
/users?email=john.doe@example.com
.
Which (assuming emails are unique to users) would return a collection with one user item that matches the email.
- Email as a resource:
But if emails from users are also resources. Then you could make an API where /users/[user_id]/emails
returns a collection of email addresses for user with id user_id
. /users/[user_id]/emails/[email_id]
returns the email of user with user_id and ['email_id']. What you use as an identifier is up to you, but I would stick to an integer. You can delete an email from the user by sending a DELETE
request to the path that identifies the email you want to delete.
So for example DELETE
on /users/[user_id]/emails/[email_id]
will delete the email with email_id that is owned by user with user_id. Most likely only the user owning the resource is allowed to perform this delete operation. Other users will get a 403 response.
If a user can have only one email address you can stick to /users/[user_id]/email
This returns a singleton resource. The user can update his email address by PUT
ting the email address at that url.