Translator Note: This article mainly describes several methods of API version control. The user can query the original API, or add a custom header file to receive a specific version. If the application receives a major revision, modify the URI to V2. When you make iterative improvements, you create endpoints that match the date of the change and allow the user to attach the date information. You can then choose to keep the old version for the time. And when designing and versioning APIs, you can apply many different ideas. The following is the translation
API design is a "Hot hot" topic! There are a number of excellent articles about the best structure and version of the API. In this article, we will delve into the areas of conflict between different API designs and, based on this, present our neutral perspective, and then show how we use fly to validate our neutral view.
API version Control
While there is no unified way to design the API, it is important to clarify a few key ideas that many developers agree on. A well-structured web API should be ...
1: Maintain an ongoing protocol with the client. The Protocol guarantees consistency and stability; The client should be able to use the API without worrying that it will suddenly break or disappear.
2: Backwards compatibility after change or upgrade. The old query for the new endpoint should still produce the expected return value.
3:restful (Internet application). It should be able to identify the related actions of http: Get,put,post,patch,delete and so on.
In order to best achieve these ideal requirements, in how to implement different versions of the API is the same, benevolent see. Let's take a look at three of them ...
URI version Control
Curl HTTPS://EXAMPLE.COM/API/V2/LISTS/3
By releasing the version number in the URI, the client can access/v1/or/V2/API. It is readable, adaptable, and can be inserted directly into the user's browser.
Header version Control
Curl HTTPS://EXAMPLE.COM/API/LISTS/3 \
-H ' Accept:application/vnd.example.v2+json '
The API URI remains the same. The header version control is mainly done by a custom accept HTTP header. The core URI remains the same, but it is best to represent the resources of the API, and the version changes are passed through the header and response type.
No version Control!
Curl HTTPS://EXAMPLE.COM/API/LISTS/3
What version do you need? Let's expand our API to accommodate new or adjusted cases! Discard the old frame, choose Build and expand.
Neutral View
Every method is reasonable, and if there are good design ideas, they can all show excellent API. In the end, we want them to actually be used to transfer information as quickly as possible. A green API without versioning can become confusing, so we'll get away from that. Instead, we will accept versioning in the URI and HTTP headers. As a twist, we're going to use a custom HTTP header.
In order to avoid the appearance that even we cannot understand why requests are so messy and lengthy, let's take a deeper look at why we want to use this approach. Our approach is based on the motto: As the sun rises, your application will change.
Let's look at an example.
Mightylist
We are building the API for the fictional application mightylist. In its humble early days, the Mighty list allowed users to create lists. It has an API provided from a URI as a mightyapp.com/api/v1/. You can use the API to request list information as follows:
Curl HTTPS://MIGHTYAPP.COM/API/V1/LISTS/3
...
{
"ListId": "3",
"Shopping": "Shoes, tie, umbrella, snorkel",
"Leisure": "skiing, surfing, snorkeling",
"Food": "Bananas, peanut butter, spinach",
"Cost": "One hundred dollars"
}
When developing our application, we can expect to change two types: Minor version adjustment and large version adjustment. Let's take a look at a small version of the tuning example, using our example response above, where the GET Request for List 3 returns the string value of shopping, leisure, food and cost. The development team wants to tweak the data model, and cost now becomes an integer instead of a string.
Curl HTTPS://MIGHTYAPP.COM/API/V1/LISTS/3
...
{
"ListId": "3",
"Shopping": "Shoes, tie, umbrella, snorkel",
"Leisure": "skiing, surfing, snorkeling",
"Food": "Bananas, peanut butter, spinach",
"Cost": 100
}
This change will break the backwards compatibility of our API! If someone applies the Mightylist API to their application, receiving an integer instead of a string may cause their application to break. To avoid this unexpected situation, any minor changes that disrupt our backward compatibility require bump version for the new release.
Another scenario to consider is the large version adjustment: Mightylist version 2 is in progress. Lists will become superlists, and we have added a lot of new resources: bots,ai--and many other exotic things. This major upgrade will certainly have a significant impact on compatibility; It's a whole new app!
Both examples reveal how difficult it is to keep URLs up to date when improving the API. Will minor revisions break our URL version? This will eventually be a lot of versions. We want to keep backwards compatibility, but it will become more and more difficult as the application continues to evolve.
Before we discuss this issue, let's define what a secure and backward-compatible API change should look like:
Add a new resource.
Add a new event type.
Adds a new property for the response.
Change the order of the attributes.
Adds an optional parameter to an existing method.
When adding new things through options or new resources or event parameters, you can have absolutely no concern with the protocol established with the client, because it is stable and does not change. They can continue to access the API resources and also get the expected response. If we change the existing resource or the expected response, we need to show this change in the API version.
To make the status of our application clear, we want to have a version in the URI that represents the base product version, and when your product changes fundamentally, the URI version will change. Mightylist V1 Use/api/v1/. Mightylist V2 Use/api/v2/.
To make the current version of our API clear, we will use a custom HTTP header to represent smaller revisions. This is similar to having the user request application version v2.x. X is the version of the API.
Versioning in two places is a practical practice; Your application will iterate, and it's always possible to become something bigger. Let's take a look at how fly can do this with ease.
Superfly API Version Control
In fly, each site is made up of multiple back ends. You might have a static page on GitHub, the application uses a kubernetes cluster or Heroku deployment, and it also uses one or more databases to manage the data.
When building an API, you can decouple the API and use it as your back-end host, so that the benefits of scalability and load balancing of the API will be improved. Decoupling the API also makes versioning relatively easy.
First, we will add a new backend for the initial api/api/v1/. We will specify the relevant path to match the/api/v1/. If there are multiple redundant instances that require load balancing, we can add them as separate backend, set the same path, and then adjust the priority. If you want to implement this idea, we can use fly middleware to route users to various API backend based on device or geographic information.
Above we have created a baseline API endpoint. We also want to create a backend for the latest version of API V1. We will end in 04-05-2017. After this, we can set the API routing rules.
Now, to configure the fly routing rules for the second backend, so that users with the custom header files attached can receive the latest version of the API. We will use the header name Api-version, because it allows users to see its meaning, so that users can easily remember, but also more clear. The apiapi-version header file will be in the form of a date or 04-05-2017. Doing so will allow us to create a public change log for the API. Users can get the endpoint they want by changing the date.
It's so exciting! We have set Api-version to 04-05-2017, and if this information is present in the header file, the user will be routed to the backend of API v1-04-05-2017. The user now has the option to request a specific point in time for the API while preserving the ability to get the original API V1 from the EXAMPLE.COM/API/V1 URI. You can view the original routing rules for our first API back end:
Summarize
Let's take a look: Different APIs have different endpoints. The user can query the original API, or add a custom header file to receive a specific version. If the application makes a large version of the adjustment, we will formally modify the URI to V2.
When we make iterative improvements, we also create an endpoint along with the modified date and allow the user to append the date header file information to it. We can then choose how long the old version can be retained, and after the free collection system can choose the right time to delete them without considering backwards compatibility and without affecting the user's use.
When designing the API version, you can apply many different ideas. Using the examples in this article, you can see these flexibility based on the fly header file or URI back-end routing, allowing you to implement the wildest dreams of the Pi Architecture and Versioning scheme.
API version Control