You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rails6/en/chapter02-api.adoc
+33-60Lines changed: 33 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -131,71 +131,17 @@ Rails.application.routes.draw do
131
131
end
132
132
----
133
133
134
-
Up to this point we have not made anything crazy. What we want to generate is a _base_uri_ wich include the API version like this: http://localhost:3000/api/v1.
135
-
136
-
NOTE: Setting the API under a subdomain is a good practice because it allows the application to be adapted to a DNS level. But we will simplify things for now in our case.
137
-
138
-
You should be concerned about versioning your application from the beginning as this will give your API a *better structure*. So when changes occur on your API you can thus propose to developers to adapt to the new features while the old ones are depreciated.
139
-
140
-
[source,ruby]
141
-
.config/routes.rb
142
-
----
143
-
Rails.application.routes.draw do
144
-
namespace :api, defaults: { format: :json } do
145
-
namespace :v1 do
146
-
# We are going to list our resources here
147
-
end
148
-
end
149
-
end
150
-
----
151
-
152
-
.Common API patterns
153
-
****
154
-
You can find many approaches to set up the _base_uri_ when building an api following different patterns, assuming we are versioning our api:
155
-
156
-
* `api.example.com/`: In my opinion this is the way to go, gives you a better interface and isolation, and in the long term can help you to http://www.makeuseof.com/tag/optimize-your-dns-for-faster-internet/[quickly scalate]
157
-
* `example.com/api/`: This pattern is very common, and it is actually a good way to go when you don’t want to namespace your api under a subdomain
158
-
* `example.com/api/v1`: it seems like a good idea, by setting the version of the api through the URL seems like a more descriptive pattern, but this way you enforce the version to be included on URL on each request, so if you ever decide to change this pattern, this becomes a problem of maintenance in the long-term
159
-
****
160
-
161
-
Time to commit:
134
+
Up to this point we have not made anything crazy. What we want to generate is a _base_uri_ wich include the API version. But let's make a comit before go to next section:
162
135
163
136
[source,bash]
164
137
----
165
138
$ git add config/routes.rb
166
139
$ git commit -m "Set the routes constraints for the api"
167
140
----
168
141
169
-
In order to define the API version, we must first add another directory under the `api/` folder we created:
170
-
171
-
[source,bash]
172
-
----
173
-
$ mkdir app/controllers/api/v1
174
-
----
175
-
176
-
The API is now _scoped_ via the URL. For example, with the current configuration, the recovery of a product via the API would be done with this url: http://localhost:3000/v1/products/1.
177
-
178
-
Don't worry we'll get more details about the versioning later. It is time to _commit_:
179
-
180
-
[source,bash]
181
-
----
182
-
$ git commit -am "Set the routes namespaces for the api"
183
-
----
184
-
185
-
NOTE: There are some practices in API building that recommend not to version the API via the URL. That's true. The developer should not be aware of the version he's using. For the sake of simplicity, I have chosen to set aside this convention, which we will be able to apply in a second phase.
186
-
187
-
We are at the end of our chapter. It is therefore time to apply all our modifications to the master branch by making a _merge_. To do this, we place ourselves on the `master` branch and we _merge_ `chapter02`:
188
-
189
-
[source,bash]
190
-
----
191
-
$ git checkout master
192
-
$ git merge chapter02
193
-
----
194
-
195
-
196
142
== Api versioning
197
143
198
-
At this point we should have a nice routes mapping using a namespace. Your _routes.rb_ file should look like this:
144
+
At this point we should have a nice routes mapping using a namespace. Your `routes.rb` file should look like this:
199
145
200
146
[source,ruby]
201
147
.config/routes.rb
@@ -210,29 +156,56 @@ end
210
156
211
157
Now it is time to set up some other constraints for versioning purposes. You should care about versioning your application from the beginning since this will give a better structure to your api, and when changes need to be done, you can give developers who are consuming your api the opportunity to adapt for the new features while the old ones are being deprecated. There is an excellent http://railscasts.com/episodes/350-rest-api-versioning[railscast] explaining this.
212
158
213
-
In order to set the version for the api, we first need to add another directory under the `api` we created
159
+
In order to set the version for the API, we first need to add another directory under the `api` we created:
214
160
215
161
[source,bash]
216
162
----
217
163
$ mkdir app/controllers/api/v1
218
164
----
219
165
220
-
This way we can scope our api into different versions very easily, now we just need to add the necessary code to the `routes.rb` file
166
+
This way we can namespace our api into different versions very easily, now we just need to add the necessary code to the `routes.rb` file:
221
167
222
168
[source,ruby]
223
169
.config/routes.rb
224
170
----
225
171
Rails.application.routes.draw do
226
172
# Api definition
227
173
namespace :api, defaults: { format: :json } do
228
-
scope module: :v1 do
174
+
namespace: :v1 do
229
175
# We are going to list our resources here
230
176
end
231
177
end
232
178
end
233
179
----
234
180
235
-
By this point the API is now scoped via the URL. For example with the current configuration an end point for retrieving a product would be like: http://localhost:3000/v1/products/1.
181
+
By this point the API is now scoped via the URL. For example with the current configuration an end point for retrieving a product would be like: <http://localhost:3000/v1/products/1> .
182
+
183
+
184
+
.Common API patterns
185
+
****
186
+
You can find many approaches to set up the _base_uri_ when building an api following different patterns, assuming we are versioning our api:
187
+
188
+
* `api.example.com/`: In my opinion this is the way to go, gives you a better interface and isolation, and in the long term can help you to http://www.makeuseof.com/tag/optimize-your-dns-for-faster-internet/[quickly scalate]
189
+
* `example.com/api/`: This pattern is very common, and it is actually a good way to go when you don’t want to namespace your api under a subdomain
190
+
* `example.com/api/v1`: it seems like a good idea, by setting the version of the api through the URL seems like a more descriptive pattern, but this way you enforce the version to be included on URL on each request, so if you ever decide to change this pattern, this becomes a problem of maintenance in the long-term
191
+
192
+
There are some practices in API building that recommend not to version the API via the URL. That's true. The developer should not be aware of the version he's using. For the sake of simplicity, I have chosen to set aside this convention, which we will be able to apply in a second phase.
193
+
****
194
+
195
+
It is time to _commit_:
196
+
197
+
[source,bash]
198
+
----
199
+
$ git commit -am "Set the versioning namespaces for API"
200
+
----
201
+
202
+
We are at the end of our chapter. It is therefore time to apply all our modifications to the master branch by making a _merge_. To do this, we place ourselves on the `master` branch and we _merge_ `chapter02`:
0 commit comments