HTTP REST APIs
URL
Example
The following sections present a few real world examples on how to apply resource-oriented API design to large scale services. You can find more examples in the Google APIs repository.
In these examples, the asterisk indicates one specific resource out of the list.
Gmail API
The Gmail API service implements the Gmail API and exposes most of Gmail functionality. It has the following resource model:
API service:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- A collection of users:```users/*```. Each user has the following resources.
- A collection of messages: `users/*/messages/*`.
- A collection of threads: `users/*/threads/*`.
- A collection of labels: `users/*/labels/*`.
- A collection of change history: `users/*/history/*`.
- A resource representing the user profile: `users/*/profile`.
- A resource representing user settings: `users/*/settings`.
#### Cloud Pub/Sub API
The `pubsub.googleapis.com` service implements the [Cloud Pub/Sub API](https://cloud.google.com/pubsub), which defines the following resource model:
- API service:`pubsub.googleapis.com`
- A collection of topics: `projects/*/topics/*`.
- A collection of subscriptions: `projects/*/subscriptions/*`.
**NOTE:** Other implementations of the Pub/Sub API may choose different resource naming schemes.
#### Cloud Spanner API
The `spanner.googleapis.com` service implements the [Cloud Spanner API](https://cloud.google.com/spanner), which defines the following resource model:
- API service:```spanner.googleapis.com- A collection of instances:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68- A collection of instance operations: `projects/*/instances/*/operations/*`.
- A collection of databases: `projects/*/instances/*/databases/*`.
- A collection of database operations: `projects/*/instances/*/databases/*/operations/*`.
- A collection of database sessions: `projects/*/instances/*/databases/*/sessions/*`.
<!-- more -->
## Methods
The following table describes how to map standard methods to HTTP methods:
| Standard Method | HTTP Mapping | HTTP Request Body | HTTP Response Body |
| :----------------------------------------------------------- | :-------------- | :---------------- | :------------------------ |
| [`List`](https://cloud.google.com/apis/design/standard_methods#list) | `GET ` | N/A | Resource* list |
| [`Get`](https://cloud.google.com/apis/design/standard_methods#get) | `GET ` | N/A | Resource* |
| [`Create`](https://cloud.google.com/apis/design/standard_methods#create) | `POST ` | Resource | Resource* |
| [`Update`](https://cloud.google.com/apis/design/standard_methods#update) | `PUT or PATCH ` | Resource | Resource* |
| [`Delete`](https://cloud.google.com/apis/design/standard_methods#delete) | `DELETE ` | N/A | `google.protobuf.Empty`** |
*The resource returned from `List`, `Get`, `Create`, and `Update` methods **may** contain partial data if the methods support response field masks, which specify a subset of fields to be returned. In some cases, the API platform natively supports field masks for all methods.
### List
The `List` method takes a collection name and zero or more parameters as input, and returns a list of resources that match the input.
`List` is commonly used to search for resources. `List` is suited to data from a single collection that is bounded in size and not cached. For broader cases, the [custom method](https://cloud.google.com/apis/design/custom_methods) `Search` **should** be used.
A batch get (such as a method that takes multiple resource IDs and returns an object for each of those IDs) **should** be implemented as a custom `BatchGet` method, rather than a `List`. However, if you have an already-existing `List` method that provides the same functionality, you **may** reuse the `List` method for this purpose instead. If you are using a custom `BatchGet` method, it **should** be mapped to `HTTP GET`.
HTTP mapping:
- The `List` method **must** use an HTTP `GET` verb.
- The request message field(s) receiving the name of the collection whose resources are being listed **should** map to the URL path. If the collection name maps to the URL path, the last segment of the URL template (the [collection ID](https://cloud.google.com/apis/design/resource_names#CollectionId)) **must** be literal.
- All remaining request message fields **shall** map to the URL query parameters.
- There is no request body; the API configuration **must not** declare a `body` clause.
- The response body **should** contain a list of resources along with optional metadata.
### Get
The `Get` method takes a resource name, zero or more parameters, and returns the specified resource.
HTTP mapping:
- The `Get` method **must** use an HTTP `GET` verb.
- The request message field(s) receiving the resource name **should** map to the URL path.
- All remaining request message fields **shall** map to the URL query parameters.
- There is no request body; the API configuration **must not** declare a `body` clause.
- The returned resource **shall** map to the entire response body.
### Create
### Update
### Delete
### Custom Methods
Custom methods refer to API methods besides the 5 standard methods. They **should** only be used for functionality that cannot be easily expressed via standard methods. In general, API designers **should** choose standard methods over custom methods whenever feasible. Standard Methods have simpler and well-defined semantics that most developers are familiar with, so they are easier to use and less error prone. Another advantage of standard methods is the API platform has better understanding and support for standard methods, such as billing, error handling, logging, monitoring.
A custom method can be associated with a resource, a collection, or a service. It **may** take an arbitrary request and return an arbitrary response, and also supports streaming request and response.
#### URL
For custom methods, they **should** use the following generic HTTP mapping:
- A collection of instances:
https://service.name/v1/some/resource/name:customVerb
1 |
|
Simple request
POST /api/order/get_list
{“orders”:[100001, 100002]}
1 |
Complex request
POST /api/item/get_list
{“items”:[{“shopid”:1000,”itemid”:100001},{“shopid”:1000,”itemid”:100002}]}
# Reference
- https://cloud.google.com/apis/design/