|
| 1 | +# Custom operations |
| 2 | + |
| 3 | +Services use custom operations to provide a means to express arbitrary actions |
| 4 | +that are difficult to model using only the standard operations. Custom |
| 5 | +operations are important because they provide a means for an API's vocabulary |
| 6 | +to adhere to user intent. |
| 7 | + |
| 8 | +## Guidance |
| 9 | + |
| 10 | +Custom operations **should** only be used for functionality that can not be |
| 11 | +easily expressed via standard operations; prefer standard operations if |
| 12 | +possible, due to their consistent semantics. (Of course, this only applies if |
| 13 | +the functionality in question actually conforms to the normal semantics; it is |
| 14 | +_not_ a good idea to contort things to endeavor to make the standard operations |
| 15 | +"sort of work".) |
| 16 | + |
| 17 | +While custom operations vary widely in how they are designed, many principles |
| 18 | +apply consistently: |
| 19 | + |
| 20 | +{% tab proto %} |
| 21 | + |
| 22 | +{% sample 'library.proto', 'rpc ArchiveBook' %} |
| 23 | + |
| 24 | +- The name of the RPC **should** be a verb followed by a noun. |
| 25 | + - The name **must not** contain prepositions ("for", "with", etc.). |
| 26 | +- The HTTP method for custom operations **should** usually be `POST`, unless |
| 27 | + the custom method maps more strongly to another HTTP verb. |
| 28 | + - Custom operations that serve as an alternative to get or list operations |
| 29 | + (such as `Search`) **should** use `GET`. These operations **must** be |
| 30 | + idempotent and have no state changes or side effects (they should be safe |
| 31 | + as defined in [RFC 7231][]). |
| 32 | + - Custom operations **should not** use `PATCH` or `DELETE`. |
| 33 | +- The HTTP URI **must** use a `:` character followed by the custom verb |
| 34 | + (`:archive` in the above example), and the verb in the URI **must** match the |
| 35 | + verb in the name of the RPC. |
| 36 | + - If word separation is required, `camelCase` **must** be used. |
| 37 | +- The `body` clause in the `google.api.http` annotation **should** be `"*"`. |
| 38 | + - However, if using `GET` or `DELETE`, the `body` clause **must** be absent. |
| 39 | +- Custom operations **should** usually take a request message matching the RPC |
| 40 | + name, with a -`Request` suffix. |
| 41 | +- Custom operations **should** usually return a response message matching the |
| 42 | + RPC name, with a -`Response` suffix. |
| 43 | + - When operating on a specific resource, a custom method **may** return the |
| 44 | + resource itself. |
| 45 | + |
| 46 | +{% tab oas %} |
| 47 | + |
| 48 | +{% sample 'library.oas.yaml', '/publishers/{publisherId}/books/{bookId}:archive' %} |
| 49 | + |
| 50 | +- The `operationId` **should** be a verb followed by a noun. |
| 51 | + - The `operationId` **must not** contain prepositions ("for", "with", etc.). |
| 52 | +- The HTTP method for custom operations **should** usually be `POST`, unless |
| 53 | + the custom method maps more strongly to another HTTP verb. |
| 54 | + - Custom operations that serve as an alternative to get or list operations |
| 55 | + (such as `Search`) **should** use `GET`, and require no request body. These |
| 56 | + operations **must** be idempotent and have no state changes or side effects |
| 57 | + (they should be safe as defined in [RFC 7231][]). |
| 58 | + - Custom operations **should not** use `PATCH` or `DELETE`. |
| 59 | +- The HTTP URI **must** use a `:` character followed by the custom verb |
| 60 | + (`:archive` in the above example), and the verb in the URI **must** match the |
| 61 | + verb in the `operationId`. |
| 62 | + - If word separation is required, `camelCase` **must** be used. |
| 63 | + |
| 64 | +{% endtabs %} |
| 65 | + |
| 66 | +**Note:** The pattern above shows a custom method that operates on a specific |
| 67 | +resource. Custom operations can be associated with resources, collections, or |
| 68 | +services. |
| 69 | + |
| 70 | +### Collection-based custom operations |
| 71 | + |
| 72 | +While most custom operations operate on a single resource, some custom |
| 73 | +operations **may** operate on a collection instead: |
| 74 | + |
| 75 | +{% tab proto %} |
| 76 | + |
| 77 | +{% sample 'library.proto', 'rpc SortBooks' %} |
| 78 | + |
| 79 | +{% tab oas %} |
| 80 | + |
| 81 | +{% sample 'library.oas.yaml', '/publishers/{publisherId}/books:sort' %} |
| 82 | + |
| 83 | +{% endtabs %} |
| 84 | + |
| 85 | +- If the collection has a parent, the field name in the request message |
| 86 | + **should** be the target resource's singular noun (`publisher` in the above |
| 87 | + example). If word separators are necessary, `snake_case` **must** be used. |
| 88 | +- The collection key (`books` in the above example) **must** be literal. |
| 89 | + |
| 90 | +### Stateless operations |
| 91 | + |
| 92 | +Some custom operations are not attached to resources at all. These operations |
| 93 | +are generally _stateless_: they accept a request and return a response, and |
| 94 | +have no permanent effect on data within the API. |
| 95 | + |
| 96 | +{% tab proto %} |
| 97 | + |
| 98 | +{% sample 'translate.proto', 'rpc TranslateText' %} |
| 99 | + |
| 100 | +{% tab oas %} |
| 101 | + |
| 102 | +{% sample 'translate.oas.yaml', '/projects/{projectId}:translateText' %} |
| 103 | + |
| 104 | +{% endtabs %} |
| 105 | + |
| 106 | +- If the method runs in a particular scope (such as a project, as in the above |
| 107 | + example), the field name in the request message **should** be the name of the |
| 108 | + scope resource. If word separators are necessary, `snake_case` **must** be |
| 109 | + used. |
| 110 | +- The URI **should** place both the verb and noun after the `:` separator |
| 111 | + (avoid a "faux collection key" in the URI in this case, as there is no |
| 112 | + collection). For example, `:translateText` is preferable to `text:translate`. |
| 113 | +- Stateless operations **must** use `POST` if they involve billing. |
| 114 | + |
| 115 | +### Declarative-friendly resources |
| 116 | + |
| 117 | +Declarative-friendly resources usually **should not** employ custom operations |
| 118 | +(except specific declarative-friendly custom operations discussed in other |
| 119 | +AIPs), because declarative-friendly tools are unable to automatically determine |
| 120 | +what to do with them. |
| 121 | + |
| 122 | +An exception to this is for rarely-used, fundamentally imperative operations, |
| 123 | +such as a `Move`, `Rename`, or `Restart` operation, for which there would not |
| 124 | +be an expectation of declarative support. |
| 125 | + |
| 126 | +[rfc 7231]: https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.1 |
0 commit comments