Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

django - Changing API Versioning Strategy

I'm working on an API, using django rest framework which is currently in production. The versioning strategy in use is namespace versioning. I would like to switch to acceptheader versioning as it seems to suit the project better.

Is there a way to make the change smoothly without breaking previous API versions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Versioning is very simple.
Create folder in your app and name it to `v1`
Like this image :
https://i.stack.imgur.com/O8FGC.png
yourApp > urls.py should be like this :
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('v1/', include('api.v1.urls')),
]

Then create `urls.py` in v1 folder
it should somthing like this :
from django.urls import path, include
from api.v1.classes.Plan import listPlan
from api.v1.classes.preInvoce import preInvoce    

urlpatterns = [
    path('plan/list', listPlan.as_view(), name="listPlans"),
    path('plan/buy', preInvoce.as_view(), name="preInvoice"),
]

If you want have version 2 you need to create new folder called v2 and have urls.py in it.
and your app urls.py should be like this:
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('v1/', include('api.v1.urls')),
    path('v2/', include('api.v2.urls')),
]

Put your own and new urls in v2/urls.py
Your final v1 urls like this :
localhost:8000/v1/planList

Your final v2 urls like this :
localhost:8000/v2/newUrlInV2

Both v1 and v2 urls should works correctly.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...