Rails路线的API版本控制 [英] API Versioning for Rails Routes

查看:78
本文介绍了Rails路线的API版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像Stripe一样对我的API进行版本控制。下面给出的最新API版本是2。

I'm trying to version my API like Stripe has. Below is given the latest API version is 2.

/ api / users 返回301到 / api / v2 / users

/ api / v1 / users 返回版本1的200个用户索引

/api/v1/users returns a 200 of users index at version 1

/ api / v3 / users 返回301到 / api / v2 / users

/ api / asdf / users 返回301到 / api / v2 / users

所以基本上没有指定版本的任何内容都链接到

So that basically anything that doesn't specify the version links to the latest unless the specified version exists then redirect to it.

到目前为止,这是我所拥有的:

This is what I have so far:

scope 'api', :format => :json do
  scope 'v:api_version', :api_version => /[12]/ do
    resources :users
  end

  match '/*path', :to => redirect { |params| "/api/v2/#{params[:path]}" }
end


推荐答案

此答案的原始形式有很大不同,可以在此处找到。只是证明有多种方法可以给猫咪剥皮。

The original form of this answer is wildly different, and can be found here. Just proof that there's more than one way to skin a cat.

自从我更新了答案以来,使用了名称空间并使用301重定向-而不是默认的302.感谢pixeltrix和Bo Jeanes对这些事情的提示。

I've updated the answer since to use namespaces and to use 301 redirects -- rather than the default of 302. Thanks to pixeltrix and Bo Jeanes for the prompting on those things.

您可能希望戴上确实坚固的头盔,因为这会打动您

You might want to wear a really strong helmet because this is going to blow your mind.

Rails 3路由API非常邪恶。要按照上面的要求为您的API编写路由,您只需要这样:

The Rails 3 routing API is super wicked. To write the routes for your API, as per your requirements above, you need just this:

namespace :api do
  namespace :v1 do
    resources :users
  end

  namespace :v2 do
    resources :users
  end
  match 'v:api/*path', :to => redirect("/api/v2/%{path}")
  match '*path', :to => redirect("/api/v2/%{path}")
end

如果您的

首先,我们将 namespace 称为超级方便当您希望将一堆路由作用于特定名称的特定路径和模块时。在这种情况下,我们希望命名空间的块内所有路由都被限定为 Api 模块中的控制器,并且该路由内路径的所有请求都将以 api 为前缀。诸如 / api / v2 / users 这样的请求,您知道吗?

First, we call namespace which is super handy for when you want a bunch of routes scoped to a specific path and module that are similarly named. In this case, we want all routes inside the block for our namespace to be scoped to controllers within the Api module and all requests to paths inside this route will be prefixed with api. Requests such as /api/v2/users, ya know?

在名称空间内,我们定义了两个以上的名称空间(哇!)。这次我们定义 v1命名空间,因此控制器的所有路由都将在 Api V1 模块内c $ c>模块: Api :: V1 。通过在此路由内定义 resources:users ,控制器将位于 Api :: V1 :: UsersController 。这是版本1,您可以通过发出 / api / v1 / users 之类的请求到达那里。

Inside the namespace, we define two more namespaces (woah!). This time we're defining the "v1" namespace, so all routes for the controllers here will be inside the V1 module inside the Api module: Api::V1. By defining resources :users inside this route, the controller will be located at Api::V1::UsersController. This is version 1, and you get there by making requests like /api/v1/users.

版本2只是微小有点不同。现在不再是位于$code> Api :: V1 :: UsersController 的控制器,而是现在位于 Api :: V2 :: UsersController 。您可以通过发出 / api / v2 / users 之类的请求到达那里。

Version 2 is only a tiny bit different. Instead of the controller serving it being at Api::V1::UsersController, it's now at Api::V2::UsersController. You get there by making requests like /api/v2/users.

接下来,是一个 match 。这将匹配所有用于 / api / v3 / users 之类的API路由。

Next, a match is used. This will match all API routes that go to things like /api/v3/users.

这是一部分我不得不抬头。 :to => 选项允许您指定应将特定请求重定向到其他地方-我知道很多-但我不知道如何获取

This is the part I had to look up. The :to => option allows you to specify that a specific request should be redirected somewhere else -- I knew that much -- but I didn't know how to get it to redirect to somewhere else and pass in a piece of the original request along with it.

为此,我们称为 redirect 方法,并将其传递给带有特殊内插的%{path} 参数的字符串。当传入的请求与此最终匹配匹配时,它将把 path 参数插入到<$ c的位置$ c>%{path} 放在字符串中,并将用户重定向到他们需要去的地方。

To do this, we call the redirect method and pass it a string with a special-interpolated %{path} parameter. When a request comes in that matches this final match, it will interpolate the path parameter into the location of %{path} inside the string and redirect the user to where they need to go.

最后,我们使用另一个 match 将所有剩余的前缀为 / api 的路径路由,并将其重定向到 / api / v2 /% {path} 。这意味着像 / api / users 这样的请求将转到 / api / v2 / users

Finally, we use another match to route all remaining paths prefixed with /api and redirect them to /api/v2/%{path}. This means requests like /api/users will go to /api/v2/users.

我不知道如何获取 / api / asdf / users 进行匹配,因为您如何确定是否应该这样做是对 / api /< resource> /< identifier> / api /< version> /< resource>

I couldn't figure out how to get /api/asdf/users to match, because how do you determine if that is supposed to be a request to /api/<resource>/<identifier> or /api/<version>/<resource>?

无论如何,这很有趣,希望对您有所帮助!

Anyway, this was fun to research and I hope it helps you!

这篇关于Rails路线的API版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆