| 1 |
[[resource_design]] |
| 2 |
== Designing a resource handler |
| 3 |
|
| 4 |
This chapter aims to provide you with a list of questions |
| 5 |
you must answer in order to write a good resource handler. |
| 6 |
It is meant to be usable as a step by step guide. |
| 7 |
|
| 8 |
=== The service |
| 9 |
|
| 10 |
Can the service become unavailable, and when it does, can |
| 11 |
we detect it? For example, database connectivity problems |
| 12 |
may be detected early. We may also have planned outages |
| 13 |
of all or parts of the system. Implement the |
| 14 |
`service_available` callback. |
| 15 |
|
| 16 |
What HTTP methods does the service implement? Do we need |
| 17 |
more than the standard OPTIONS, HEAD, GET, PUT, POST, |
| 18 |
PATCH and DELETE? Are we not using one of those at all? |
| 19 |
Implement the `known_methods` callback. |
| 20 |
|
| 21 |
=== Type of resource handler |
| 22 |
|
| 23 |
Am I writing a handler for a collection of resources, |
| 24 |
or for a single resource? |
| 25 |
|
| 26 |
The semantics for each of these are quite different. |
| 27 |
You should not mix collection and single resource in |
| 28 |
the same handler. |
| 29 |
|
| 30 |
=== Collection handler |
| 31 |
|
| 32 |
Skip this section if you are not doing a collection. |
| 33 |
|
| 34 |
Is the collection hardcoded or dynamic? For example, |
| 35 |
if you use the route `/users` for the collection of |
| 36 |
users then the collection is hardcoded; if you use |
| 37 |
`/forums/:category` for the collection of threads |
| 38 |
then it isn't. When the collection is hardcoded you |
| 39 |
can safely assume the resource always exists. |
| 40 |
|
| 41 |
What methods should I implement? |
| 42 |
|
| 43 |
OPTIONS is used to get some information about the |
| 44 |
collection. It is recommended to allow it even if you |
| 45 |
do not implement it, as Cowboy has a default |
| 46 |
implementation built-in. |
| 47 |
|
| 48 |
HEAD and GET are used to retrieve the collection. |
| 49 |
If you allow GET, also allow HEAD as there's no extra |
| 50 |
work required to make it work. |
| 51 |
|
| 52 |
POST is used to create a new resource inside the |
| 53 |
collection. Creating a resource by using POST on |
| 54 |
the collection is useful when resources may be |
| 55 |
created before knowing their URI, usually because |
| 56 |
parts of it are generated dynamically. A common |
| 57 |
case is some kind of auto incremented integer |
| 58 |
identifier. |
| 59 |
|
| 60 |
The next methods are more rarely allowed. |
| 61 |
|
| 62 |
PUT is used to create a new collection (when |
| 63 |
the collection isn't hardcoded), or replace |
| 64 |
the entire collection. |
| 65 |
|
| 66 |
DELETE is used to delete the entire collection. |
| 67 |
|
| 68 |
PATCH is used to modify the collection using |
| 69 |
instructions given in the request body. A PATCH |
| 70 |
operation is atomic. The PATCH operation may |
| 71 |
be used for such things as reordering; adding, |
| 72 |
modifying or deleting parts of the collection. |
| 73 |
|
| 74 |
=== Single resource handler |
| 75 |
|
| 76 |
Skip this section if you are doing a collection. |
| 77 |
|
| 78 |
What methods should I implement? |
| 79 |
|
| 80 |
OPTIONS is used to get some information about the |
| 81 |
resource. It is recommended to allow it even if you |
| 82 |
do not implement it, as Cowboy has a default |
| 83 |
implementation built-in. |
| 84 |
|
| 85 |
HEAD and GET are used to retrieve the resource. |
| 86 |
If you allow GET, also allow HEAD as there's no extra |
| 87 |
work required to make it work. |
| 88 |
|
| 89 |
POST is used to update the resource. |
| 90 |
|
| 91 |
PUT is used to create a new resource (when it doesn't |
| 92 |
already exist) or replace the resource. |
| 93 |
|
| 94 |
DELETE is used to delete the resource. |
| 95 |
|
| 96 |
PATCH is used to modify the resource using |
| 97 |
instructions given in the request body. A PATCH |
| 98 |
operation is atomic. The PATCH operation may |
| 99 |
be used for adding, removing or modifying specific |
| 100 |
values in the resource. |
| 101 |
|
| 102 |
=== The resource |
| 103 |
|
| 104 |
Following the above discussion, implement the |
| 105 |
`allowed_methods` callback. |
| 106 |
|
| 107 |
Does the resource always exist? If it may not, implement |
| 108 |
the `resource_exists` callback. |
| 109 |
|
| 110 |
Do I need to authenticate the client before they can |
| 111 |
access the resource? What authentication mechanisms |
| 112 |
should I provide? This may include form-based, token-based |
| 113 |
(in the URL or a cookie), HTTP basic, HTTP digest, |
| 114 |
SSL certificate or any other form of authentication. |
| 115 |
Implement the `is_authorized` callback. |
| 116 |
|
| 117 |
Do I need fine-grained access control? How do I determine |
| 118 |
that they are authorized access? Handle that in your |
| 119 |
`is_authorized` callback. |
| 120 |
|
| 121 |
Can access to a resource be forbidden regardless of access |
| 122 |
being authorized? A simple example of that is censorship |
| 123 |
of a resource. Implement the `forbidden` callback. |
| 124 |
|
| 125 |
Can access be rate-limited for authenticated users? Use the |
| 126 |
`rate_limited` callback. |
| 127 |
|
| 128 |
Are there any constraints on the length of the resource URI? |
| 129 |
For example, the URI may be used as a key in storage and may |
| 130 |
have a limit in length. Implement `uri_too_long`. |
| 131 |
|
| 132 |
=== Representations |
| 133 |
|
| 134 |
What media types do I provide? If text based, what charsets |
| 135 |
are provided? What languages do I provide? |
| 136 |
|
| 137 |
Implement the mandatory `content_types_provided`. Prefix |
| 138 |
the callbacks with `to_` for clarity. For example, `to_html` |
| 139 |
or `to_text`. For resources that don't implement methods |
| 140 |
GET or HEAD, you must still accept at least one media type, |
| 141 |
but you can leave the callback as `undefined` since it will |
| 142 |
never be called. |
| 143 |
|
| 144 |
Implement the `languages_provided` or `charsets_provided` |
| 145 |
callbacks if applicable. |
| 146 |
|
| 147 |
Does the resource accept ranged requests? If it does, |
| 148 |
implement the `ranges_provided` callback. Resources that |
| 149 |
only accept `bytes` units can use the callback name |
| 150 |
`auto` and let Cowboy automatically do ranged responses. |
| 151 |
Other callbacks should have a name prefix of `ranged_` |
| 152 |
for clarity. For example, `ranged_bytes` or `ranged_pages`. |
| 153 |
If the resource needs to perform additional checks before |
| 154 |
accepting to do a ranged responses, implement the |
| 155 |
`range_satisfiable` callback. |
| 156 |
|
| 157 |
Is there any other header that may make the representation |
| 158 |
of the resource vary? Implement the `variances` callback. |
| 159 |
|
| 160 |
Depending on your choices for caching content, you may |
| 161 |
want to implement one or more of the `generate_etag`, |
| 162 |
`last_modified` and `expires` callbacks. |
| 163 |
|
| 164 |
Do I want the user or user agent to actively choose a |
| 165 |
representation available? Send a list of available |
| 166 |
representations in the response body and implement |
| 167 |
the `multiple_choices` callback. |
| 168 |
|
| 169 |
=== Redirections |
| 170 |
|
| 171 |
Do I need to keep track of what resources were deleted? |
| 172 |
For example, you may have a mechanism where moving a |
| 173 |
resource leaves a redirect link to its new location. |
| 174 |
Implement the `previously_existed` callback. |
| 175 |
|
| 176 |
Was the resource moved, and is the move temporary? If |
| 177 |
it is explicitly temporary, for example due to maintenance, |
| 178 |
implement the `moved_temporarily` callback. Otherwise, |
| 179 |
implement the `moved_permanently` callback. |
| 180 |
|
| 181 |
=== The request |
| 182 |
|
| 183 |
Do you need to read the query string? Individual headers? |
| 184 |
Implement `malformed_request` and do all the parsing and |
| 185 |
validation in this function. Note that the body should not |
| 186 |
be read at this point. |
| 187 |
|
| 188 |
May there be a request body? Will I know its size? |
| 189 |
What's the maximum size of the request body I'm willing |
| 190 |
to accept? Implement `valid_entity_length`. |
| 191 |
|
| 192 |
Finally, take a look at the sections corresponding to the |
| 193 |
methods you are implementing. |
| 194 |
|
| 195 |
=== OPTIONS method |
| 196 |
|
| 197 |
Cowboy by default will send back a list of allowed methods. |
| 198 |
Do I need to add more information to the response? Implement |
| 199 |
the `options` method. |
| 200 |
|
| 201 |
=== GET and HEAD methods |
| 202 |
|
| 203 |
If you implement the methods GET and/or HEAD, you must |
| 204 |
implement one `ProvideCallback` callback for each |
| 205 |
content-type returned by the `content_types_provided` |
| 206 |
callback. |
| 207 |
|
| 208 |
When range requests are accepted, you must implement one |
| 209 |
`RangeCallback` for each range unit returned by |
| 210 |
`ranges_provided` (unless `auto` was used). This is |
| 211 |
in addition to the `ProvideCallback` callback. |
| 212 |
|
| 213 |
=== PUT, POST and PATCH methods |
| 214 |
|
| 215 |
If you implement the methods PUT, POST and/or PATCH, |
| 216 |
you must implement the `content_types_accepted` callback, |
| 217 |
and one `AcceptCallback` callback for each content-type |
| 218 |
it returns. Prefix the `AcceptCallback` callback names |
| 219 |
with `from_` for clarity. For example, `from_html` or |
| 220 |
`from_json`. |
| 221 |
|
| 222 |
Do we want to allow the POST method to create individual |
| 223 |
resources directly through their URI (like PUT)? Implement |
| 224 |
the `allow_missing_post` callback. It is recommended to |
| 225 |
explicitly use PUT in these cases instead. |
| 226 |
|
| 227 |
May there be conflicts when using PUT to create or replace |
| 228 |
a resource? Do we want to make sure that two updates around |
| 229 |
the same time are not cancelling one another? Implement the |
| 230 |
`is_conflict` callback. |
| 231 |
|
| 232 |
=== DELETE methods |
| 233 |
|
| 234 |
If you implement the method DELETE, you must implement |
| 235 |
the `delete_resource` callback. |
| 236 |
|
| 237 |
When `delete_resource` returns, is the resource completely |
| 238 |
removed from the server, including from any caching service? |
| 239 |
If not, and/or if the deletion is asynchronous and we have |
| 240 |
no way of knowing it has been completed yet, implement the |
| 241 |
`delete_completed` callback. |