Skip to content

Commit a9c1abc

Browse files
committed
Clarify chapter 2 about API versionning
1 parent 3b31f6b commit a9c1abc

File tree

1 file changed

+33
-60
lines changed

1 file changed

+33
-60
lines changed

rails6/en/chapter02-api.adoc

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -131,71 +131,17 @@ Rails.application.routes.draw do
131131
end
132132
----
133133

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:
162135

163136
[source,bash]
164137
----
165138
$ git add config/routes.rb
166139
$ git commit -m "Set the routes constraints for the api"
167140
----
168141

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-
196142
== Api versioning
197143

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:
199145

200146
[source,ruby]
201147
.config/routes.rb
@@ -210,29 +156,56 @@ end
210156

211157
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.
212158

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:
214160

215161
[source,bash]
216162
----
217163
$ mkdir app/controllers/api/v1
218164
----
219165

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:
221167

222168
[source,ruby]
223169
.config/routes.rb
224170
----
225171
Rails.application.routes.draw do
226172
# Api definition
227173
namespace :api, defaults: { format: :json } do
228-
scope module: :v1 do
174+
namespace: :v1 do
229175
# We are going to list our resources here
230176
end
231177
end
232178
end
233179
----
234180

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`:
203+
204+
[source,bash]
205+
----
206+
$ git checkout master
207+
$ git merge chapter02
208+
----
236209

237210
== Conclusion
238211

0 commit comments

Comments
 (0)