Skip to content

Commit 682a982

Browse files
marioradu05arhimede
authored andcommitted
API evolution tutorial
Signed-off-by: arhimede <julian@dotkernel.com>
1 parent b83bb83 commit 682a982

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# API Evolution pattern
2+
3+
API evolution: Updating an API while keeping it compatible for existing consumers by adding new features, fixing bugs, planning and removing outdated features.
4+
5+
## How it works
6+
7+
In DotKernel API we can mark an entire endpoint or a single method as deprecated using attributes on handlers.
8+
We use response headers to inform the consumers about the future changes by using 2 new headers: **link** and **sunset**.
9+
10+
1) `Link` - it's a link to the official documentation pointing out the changes that will take place.
11+
2) `Sunset` - this header is a date, indicating when the change will roll out.
12+
13+
**Both headers are independent, you can use them separately.**
14+
15+
> Make sure you have the `DeprecationMiddleware:class` piped in your `pipeline` list. In our case it's `config/pipeline.php`.
16+
17+
### Marking an entire endpoint as deprecated
18+
19+
When you want to mark an entire resource as deprecated you have to use the ``ResourceDeprecation`` attribute.
20+
21+
```php
22+
...
23+
#[ResourceDeprecation(
24+
sunset: '2038-01-01',
25+
link: 'https://docs.dotkernel.org/api-documentation/v5/core-features/versioning',
26+
deprecationReason: 'Resource deprecation example.',
27+
rel: 'sunset',
28+
type: 'text/html'
29+
)]
30+
class HomeHandler implements RequestHandlerInterface
31+
{
32+
...
33+
```
34+
35+
In the example above, the ``ResourceDeprecation`` attribute is attached to the class, marking the entire `/` (home) endpoint as deprecated starting from `2038-01-01`.
36+
37+
Running the following curl will print out the response headers where we can see the **Sunset** and **Link** headers.
38+
39+
```shell
40+
curl --head -X GET http://0.0.0.0:8080 -H "Content-Type: application/json"
41+
```
42+
43+
```shell
44+
HTTP/1.1 200 OK
45+
Host: 0.0.0.0:8080
46+
Date: Mon, 24 Jun 2024 10:23:11 GMT
47+
Connection: close
48+
X-Powered-By: PHP/8.2.20
49+
Content-Type: application/json
50+
Permissions-Policy: interest-cohort=()
51+
Sunset: 2038-01-01
52+
Link: https://docs.dotkernel.org/api-documentation/v5/core-features/versioning;rel="sunset";type="text/html"
53+
Vary: Origin
54+
```
55+
56+
### Marking a method as deprecated
57+
58+
Most of the time you want to deprecate only an endpoint, so you will need to use the `MethodDeprecation` attribute which has the same parameters, but it attaches to a handler method.
59+
60+
```php
61+
...
62+
class HomeHandler implements RequestHandlerInterface
63+
{
64+
...
65+
66+
#[MethodDeprecation(
67+
sunset: '2038-01-01',
68+
link: 'https://docs.dotkernel.org/api-documentation/v5/core-features/versioning',
69+
deprecationReason: 'Method deprecation example.',
70+
rel: 'sunset',
71+
type: 'text/html'
72+
)]
73+
public function get(): ResponseInterface
74+
{
75+
...
76+
}
77+
}
78+
```
79+
80+
Attaching the `MethodDeprecation` can only be done to HTTP verb methods (`GET`, `POST`, `PUT`, `PATCH` and `DELETE`).
81+
82+
If you followed along you can run the below curl:
83+
84+
```shell
85+
curl --head -X GET http://0.0.0.0:8080 -H "Content-Type: application/json"
86+
```
87+
88+
```shell
89+
HTTP/1.1 200 OK
90+
Host: 0.0.0.0:8080
91+
Date: Mon, 24 Jun 2024 10:54:57 GMT
92+
Connection: close
93+
X-Powered-By: PHP/8.2.20
94+
Content-Type: application/json
95+
Permissions-Policy: interest-cohort=()
96+
Sunset: 2038-01-01
97+
Link: https://docs.dotkernel.org/api-documentation/v5/core-features/versioning;rel="sunset";type="text/html"
98+
Vary: Origin
99+
```
100+
101+
### NOTES
102+
103+
> If `Link` or `Sunset` do not have a value they will not appear in the response headers.
104+
105+
> `Sunset` has to be a **valid** date, otherwise it will throw an error.
106+
107+
> You **cannot** use both `ResourceDeprecation` and `MethodDeprecation` in the same handler.
108+
109+
> Deprecations can only be attached to handler classes that implement `RequestHandlerInterface`.
110+
111+
> The `rel` and `type` arguments are optional, they default to `sunset` and `text/html` if no value was provided and are `Link` related parts.

0 commit comments

Comments
 (0)