Django的REST的架构:API版本 [英] django-rest-framework: api versioning
问题描述
所以谷歌搜索周围出现了普遍的共识是,在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.
甚至因此有支持这一强大的支持者。结果
例如对于API版本最佳实践?
even on SO there are strong proponents supporting this.
e.g. Best practices for API versioning?
我的问题是如何完成使用accept头/内容协商在Django的REST的框架来实现这一目标的提出的解决方案。
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类型预期值。如果我开始使用接受自定义类型的头,我将失去框架受益。
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.
您还可以在您的接受头,如接受媒体类型的参数:应用程序/ JSON的;版本=测试版
,这将成功匹配 JSONRenderer
。然后,您可以code你以不同的表现取决于接受了媒体的类型,请参阅here.
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版本的传入。参见[此pull请求]
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.
(<一个href=\"https://github.com/tomchristie/django-rest-framework/pull/2285\">https://github.com/tomchristie/django-rest-framework/pull/2285)了解更多详情。
这篇关于Django的REST的架构:API版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!