There are a few things you can do.
- Archiving APIs from the start. There are two general approaches I've seen for this with the REST API: placing the URL prefix as
/v1 , /v2 , etc. Before all endpoints of a REST resource or using the HTTP Accepts to negotiate versions. There are religious wars in which man is right. This is your API. Do what you think is right. - Extract business logic from the API endpoint code in your source code. That way you can have endpoints v1 and v2 that reuse common code at a lower level of service level. This is something you do not need to do from the very beginning. You can wait until the v2 API begins to separate things.
- Automatically testing each build against existing versions of the API (and any new version you build, but regression testing is the key point I am doing).
- Forced application updates, or at least tracking the use of the application version, allows you to remove / clear any code that supports legacy versions.
I am working on a similar project, creating a new REST API for a new mobile application. I am sharing the URL space by version, so https: //api.blahblahblah/v1.0/resource .
At the moment, I have my business logic built right into the code that accepts HTTP requests because there is no other benefit to such logic. However, when I need to create a new version, I reorganize the API v1 code to separate everything that is not v1-specific into a more reusable module, which can then be reused.
Depending on how structurally your versions differ, you may need some reduction to keep your API separate. For example, you might need a generic UserEntity object to represent user information from your database, but it requires a UserV1Resource and UserV2Resource object for separate versions with adapters or another design pattern to alert you of the different types that become serialized for JSON or XML.
Having received automated API tests, I can freely perform any of these refactoring, separating them from myself, knowing that the moment I break any backward compatibility, my tests will yell at me.
A good advantage of the API that is still being used by our mobile application is that we only need to worry about compatibility with supported versions of applications. If we can make sure that our end users regularly update their application, we can remove older versions, which will help minimize our technical debt.
Brandon
source share