django-rest-framework:api 版本控制 [英] django-rest-framework: api versioning

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

问题描述

在谷歌上搜索似乎普遍的共识是在 REST URI 中嵌入版本号是一种不好的做法和坏主意.

so googling around it appears that the general consensus is that embedding version numbers in REST URIs is a bad practice and a bad idea.

即使在 SO 上也有强烈的支持者支持这一点.
例如API 版本控制的最佳实践?

even on SO there are strong proponents supporting this.
e.g. Best practices for API versioning?

我的问题是关于如何使用 django-rest-framework 中的接受标头/内容协商来完成这个提议的解决方案.

My question is about how to accomplish the proposed solution of using the accept header / content negotiation in the django-rest-framework to accomplish this.

看起来像框架中的内容协商,
http://django-rest-framework.org/api-guide/content-协商/已配置为根据接受的 MIME 类型自动返回预期值.如果我开始对自定义类型使用 Accept 标头,我将失去框架的这一优势.

It looks like content negotiation in the framework,
http://django-rest-framework.org/api-guide/content-negotiation/ is already configured to automatically return intended values based on accepted MIME types. If I start using the Accept header for custom types, I'll lose this benefit of the framework.

在框架中是否有更好的方法来实现这一点?

Is there a better way to accomplish this in the framework?

推荐答案

一种方法是将版本控制指定为媒体类型的一部分.

One way of doing this is to have the versioning specified as part of the media type.

这就是 GitHub 目前为他们的 API 所做的.

This is what GitHub currently do for their API.

您还可以在接受标头中包含媒体类型参数,例如 Accept: application/json;version=beta,它将成功匹配 JSONRenderer.然后,您可以根据接受的媒体类型对视图进行编码以使其行为不同,请参阅 此处.

You can also include media type parameters in your accept headers, eg Accept: application/json; version=beta, which will successfully match against JSONRenderer. You can then code your view to behave differently depending on the accepted media type, see here.

API 中的版本控制有很多不同的模式,我认为目前还没有就正确的方法达成任何共识,但这是一种合理的可能性.

There's lots of different patterns for versioning in APIs, and I wouldn't say there's any great consensus around the right approach yet, but that'd be one reasonable possibility.

2015 年 1 月更新:3.1.0 版本将提供更好的版本支持.请参阅[此拉取请求]

Update Jan 2015: Better versioning support will be incoming in the 3.1.0 release. See [this pull request]

2015 年 3 月更新:版本控制 API 的文档现已可用.

Update March 2015: Docs for the versioning API are now available.

(https://github.com/tomchristie/django-rest-framework/pull/2285) 了解更多详情.

这篇关于django-rest-framework:api 版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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