mirror of
https://github.com/adidas/api-guidelines.git
synced 2025-10-25 15:19:19 +00:00
Changes related to Apiary and Mashery. Also related to LRTs cases and mentions to SwaggerHub.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
## Processing Similar Resources
|
||||
|
||||
An operation that needs to process several related resources in bulk **SHOULD** use a collection resource with the appropriate HTTP Request Method. When processing existing resource the request message body **MUST** contain the URLs of the respective resources being processed.
|
||||
An operation that needs to process several related resources in bulk **SHOULD** uses a collection resource with the appropriate HTTP Request Method. When processing existing resource the request message body **MUST** contain the URLs of the respective resources being processed.
|
||||
|
||||
### Example
|
||||
|
||||
|
||||
@@ -2,15 +2,43 @@
|
||||
|
||||
Every API implementation **SHOULD** return both the cache expiry information \([`Cache-Control` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control)\) and specific resource version information \([`ETag` HTTP Header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)\).
|
||||
|
||||
## Cache-Control
|
||||
The adidas API Gateway – [Kong](https://konghq.com/kong/) offers the cache feature to APIs to be applied to 1 or N endpoints or Consumer. So, cache fetures can be implemented at API Gateway level, upstream/backend service level or both.
|
||||
|
||||
## Cache-Control
|
||||
|
||||
Every API implementation's response **SHOULD** include information about cache-ability and cache expiration of the response. For HTTP 1.1 this is achieved using the `Cache-Control` header.
|
||||
|
||||
|
||||
### Settings
|
||||
|
||||
#### adidas API Gateway
|
||||
The configuration of cache in the adidas API Gateway is mainly based on:
|
||||
|
||||
- Cacheable HTTP methods
|
||||
- When to cache. Response content types, headers to be considered for the cache key, relevant query parameters, etc.
|
||||
- Expiration time, meaning the number of seconds to keep resources in the storage backend.
|
||||
- Strategy. This means, which is the backing data store in which to hold cache entities. The only accepted values are `memory` and `redis`.
|
||||
|
||||
> A complete reference for configuration can be seen [here](https://docs.konghq.com/hub/kong-inc/proxy-cache/).
|
||||
|
||||
#### API Consumer
|
||||
Clients **SHOULD** be capable of using `max-age` and `max-stale` headers to exclude the entity from being cached entirely or request stale copies of data if necessary.
|
||||
|
||||
|
||||
|
||||
### Common Cache-Control Scenarios
|
||||
|
||||
Two, most common scenarios for controlling the cache-ability of a response includes \(1\) Setting expiration and revalidation and \(2\) disabling the caching of a response. Refer to the [Cache-Control Documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) for additional controls.
|
||||
|
||||
> Remember the adidas API Gateway identifies the status of the request’s proxy cache behavior via the `X-Cache-Status` header. There are several possible values for this header:
|
||||
|
||||
- `Miss` The request could be satisfied in cache, but an entry for the resource was not found in cache, and the request was proxied upstream.
|
||||
- `Hit` The request was satisfied and served from cache.
|
||||
- `Refresh` The resource was found in cache, but could not satisfy the request, due to Cache-Control behaviors or reaching its hard-coded cache_ttl threshold.
|
||||
- `Bypass` The request could not be satisfied from cache based on plugin configuration.
|
||||
|
||||
#### 1. Cache Expiration & Revalidation
|
||||
Cache revalidation is not yet supported at API Gateway level.
|
||||
|
||||
The common scenario to set cache expiration and revalidation policy is to use the `max-age` and `must-revalidate` directives:
|
||||
|
||||
@@ -24,9 +52,11 @@ Content-Type: application/hal+json; charset=UTF-8
|
||||
...
|
||||
```
|
||||
|
||||
That means, `max-age` is the oldest that a response can be, as long as the Cache-Control from the origin server indicates that it is still fresh.
|
||||
|
||||
#### 2. Disabling Cache
|
||||
|
||||
To disable caching completely API implementation **SHOULD** use the `no-cache` and `no-store` directives:
|
||||
At API Gateway and upstream/backend levels, to disable caching completely API implementation **SHOULD** use the `no-cache` directives:
|
||||
|
||||
```text
|
||||
HTTP/1.1 200 OK
|
||||
|
||||
10
rest-api-guidelines/execution/long-running-tasks/README.md
Normal file
10
rest-api-guidelines/execution/long-running-tasks/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Long Running Tasks
|
||||
|
||||
This section includes the recommended approaches to handling long runnint tasks (LRTs) in REST APIs.
|
||||
|
||||
You can identify a LRT quite easily. The main factor to consider are the metrics from latency of the endpoint. If it requiress tens of seconds even minutes we are facing a problem related to LRTs.
|
||||
|
||||
LRTs cannot be handled in a regular straight synchronous call. The amount of commited recources at the network, client and server levels are huge when connections are blocked for several minutes.
|
||||
|
||||
It is strongly recommended to follow a non-blocking solution as it is proposed in this section.
|
||||
|
||||
136
rest-api-guidelines/execution/long-running-tasks/callback.md
Normal file
136
rest-api-guidelines/execution/long-running-tasks/callback.md
Normal file
@@ -0,0 +1,136 @@
|
||||
# Callback
|
||||
|
||||
Callback or Webhooks are another way of handling long running tasks (LRTs). Callbacks are based on the subscription principle, whereas the API notifies the API Consumer in a different connection. This pattern is also applicable to the subscription to any kind of events to get notifications from your API.
|
||||
|
||||
The roles are:
|
||||
|
||||
- API Consumer / Subscriber
|
||||
- API Producer / Publisher
|
||||
|
||||
If the chosen way is based on using callbacks, the response to such an asynchronous operation **MUST** return, in the case of success, the **202 Accepted** status code together with an `application/hal+json` representation of a new **task-tracking resource**.
|
||||
|
||||
> This pattern is described by [OAS v3.0.x](https://swagger.io/docs/specification/callbacks/).
|
||||
|
||||
|
||||
## Subscription
|
||||
|
||||
The subscriber enrolls to specfic notifications. The subscriber resource **MUST** provide the information about the callback URL. Any data needed to require the execution of a task **MUST** be included in the request body.
|
||||
|
||||
The subscription is created by using the HTTP POST Request Method. It **SHOULD** be designed as follows:
|
||||
|
||||
1. Subscription is accepted
|
||||
|
||||
Return **201 Created** and representation of the current status. Content type: `application/hal+json`
|
||||
The publisher resource **MUST** provide a UUID to identify the subscription.
|
||||
|
||||
2. Subscription is not accepted
|
||||
|
||||
Return **403 Forbidden** . Content type: `application/problem+json` with the problem detail information.
|
||||
|
||||
|
||||
## Notification
|
||||
|
||||
The publisher resource **MUST** use callback URL provided by the subscriber. Any data with the output of the requested task **SHOULD** be sent to the subscriber in this request.
|
||||
|
||||
The callback request has to use the HTTP POST Request Method **SHOULD** as follows:
|
||||
|
||||
1. The subscriber accepts the callback. Content type: `application/hal+json`
|
||||
|
||||
Return **200 OK**.
|
||||
|
||||
2. The subscriber does not accept the callback
|
||||
|
||||
Return **403 Forbidden** . Content type: `application/problem+json` with the problem detail information.
|
||||
|
||||
|
||||
## Cancel Subscription
|
||||
|
||||
The subscriber resource **MUST** include the UUID to identify the subscription.
|
||||
|
||||
It has to be used the HTTP PUT Request Method **SHOULD** as follows:
|
||||
|
||||
1. Subscription is accepted
|
||||
|
||||
Return **202 Accepted**. Content type: `application/hal+json`
|
||||
|
||||
2. Subscription is not accepted
|
||||
|
||||
Return **403 Forbidden** . Content type: `application/problem+json` with the problem detail information.
|
||||
|
||||
|
||||
## Design Note
|
||||
|
||||
- The subscription pattern supports two main approaches:
|
||||
- On one side, it can be **only-once**. The callback will be invoked only once by the publisher and it will be cancelled automatically after.
|
||||
- On the other side, it can be **continuous**. In this case the subscription **MUST** be explicitly cancelled. Regarding the subscriber, its API is also the subject of the adidas API guidelines.
|
||||
|
||||
- The callback can be based on an Asynchronous/Streaming API topic. In this case the subscription is made as mentioned above but with the following differences in the workflow:
|
||||
- The API Consumer does not send a callback URL in the initial request.
|
||||
- The API Producer **SHOULD** provide the name of the topic and the UID of the task to correlate the input.
|
||||
- It is up to the API Consumer to subscribe to the Asynchronous/Streaming API topic to receive the input from the provider. Please read the Asynchronous/Streaming API section.
|
||||
|
||||
### Example
|
||||
|
||||
1. **Settle the subscription**
|
||||
|
||||
```
|
||||
POST /items/tasks/ HTTP/1.1
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"callbackUrl": "https://myserver.com/send/callback/here"
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
HTTP/1.1 201 Created
|
||||
Content-Type: application/hal+json
|
||||
|
||||
{
|
||||
"_links": {
|
||||
"self": { "href": "/items/tasks/4746" }
|
||||
},
|
||||
"message": "Your request to subscribe to the progress of the task has been accepted.",
|
||||
"UUID": "4746"
|
||||
}
|
||||
```
|
||||
|
||||
2. **The Publisher sends the callback**
|
||||
|
||||
```
|
||||
POST https://myserver.com/send/callback/here HTTP/1.1
|
||||
|
||||
{
|
||||
"_links": {
|
||||
"self": { "href": "/items/tasks/4746" }
|
||||
},
|
||||
"UUID": "4746",
|
||||
{
|
||||
<Data with the callback>
|
||||
}
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
HTTP/1.1 200 Ok
|
||||
Content-Type: application/hal+json
|
||||
|
||||
```
|
||||
|
||||
3. **Eventually the subscriber cancels the subscription**
|
||||
|
||||
```
|
||||
PUT /feeds/tasks/1 HTTP/1.1
|
||||
...
|
||||
|
||||
HTTP/1.1 202 Accepted
|
||||
Content-Type: application/hal+json
|
||||
|
||||
{
|
||||
"_links": {
|
||||
"self": { "href": "/feeds/tasks/4746" }
|
||||
},
|
||||
"message": "Your subscription is cancelled."
|
||||
}
|
||||
```
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
# Files Upload
|
||||
|
||||
The upload of files using a REST API endpoint is a common practice. It implies certain concerns taht have to be addressed in the design phase of the API.
|
||||
|
||||
The API Consumer performs a key role in this case. The MIME type in the Content-Type header of the request is an important factor for a successful operation. An operation that needs to upload binary files **SHOULD** uses a collection resource with the POST HTTP Request Method. When processing an existing resource the request message body **MUST** contain the right MIME type of the resources being processed.
|
||||
|
||||
|
||||
## Main Issues
|
||||
|
||||
- Too long time periods in timeout settings, blocking open HTTP connections for too long. It makes the API less reliable and more error-prone as it is more vulnerable to network-related issues.
|
||||
- Interrupted connections that can result into corrupted files and false response status to the API Consumer.
|
||||
- No size limit in server can suppose an unacceptable load to the API operation in terms of resources, security and robustness as well as a huge increase in operational cost.
|
||||
|
||||
## Checklist in File Upload Operations
|
||||
|
||||
### Use the right MIME Type in the API Consumer Side
|
||||
|
||||
It is a common practice to use
|
||||
[IANA](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) distinguishes between two main generic types, **discrete** and **multipart**:
|
||||
|
||||
- Discrete types are types which represent a single file or medium, such as a single text, video, or music file.
|
||||
- Multipart type represents a document that is comprised of multiple component parts, each of which may have its own individual MIME type. It can also encapsulate multiple files being sent together in one single transaction.
|
||||
|
||||
#### Using a Multipart Type
|
||||
|
||||
- multipart/form-data
|
||||
- multipart/byteranges
|
||||
|
||||
Frameworks like Spring offfer support for multipart files sending like the [MultipartFile](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html) interface.
|
||||
|
||||
#### Using a Discrete Type
|
||||
It is recommended to upload the file alone, with no other content in the request. This approach allows to include the MIME type corresponding to the specific type of file. For instance:
|
||||
|
||||
- Graphical file -> image/jpeg, image/gif, image/bmp, etc.
|
||||
- Data file -> text/csv,
|
||||
- Text file -> text/plain
|
||||
- PDF -> application/pdf
|
||||
etc.
|
||||
|
||||
It is also recommended to compress the file to be uploaded, then using these MIM types (examples):
|
||||
|
||||
- gzip -> application/gzip
|
||||
- zip -> application/zip
|
||||
- 7z -> application/x-7z-compressed
|
||||
- tar -> application/x-tar
|
||||
etc.
|
||||
|
||||
> You can find a complete reference about the MIME types [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types).
|
||||
|
||||
### Set Maximum Size Limit
|
||||
|
||||
The maximum size of the whole file **MUST** be set for the given endpoint/s in the APi upstream/backend service side.
|
||||
|
||||
The maximum size value depends on the use case and the expected payload in upload requests.
|
||||
|
||||
The settings **MUST** included in the upstream/backend service as a part of the configuration.
|
||||
|
||||
Otherwise, the API Gateway (Kong) **COULD** be configured enabling a maximum size of the payload for specific endpoint/s.
|
||||
|
||||
Frameworks like Spring includes configuration settings for multipart file uploading. The operation **SHOULD** be constrained as follows:
|
||||
|
||||
```
|
||||
spring.servlet.multipart.enabled=true # enables multipart uploads
|
||||
spring.servlet.multipart.file-size-threshold=2KB # the threshold after which files are written to disk.
|
||||
spring.http.multipart.max-file-size=128KB # the total file size cannot exceed the amount o.
|
||||
spring.http.multipart.max-request-size=128KB # the total request size for a multipart/form-data cannot exceed 128KB.
|
||||
```
|
||||
|
||||
|
||||
### Configure Properly all the Components
|
||||
|
||||
Load tests should give you metrics about the average latency of the operations. Use these metrics to calcuate the best value for the timeout settings in the upstream/backend service.
|
||||
|
||||
The API Gateway timeout settings have to be considered for the expected timeout values, aligned with the values in the upstream/backend service. Al other components in the infrastructure **MUST** be considered for the calculation of the final metrics.
|
||||
|
||||
```
|
||||
|API Consumer/Client Timeout| ---> |External Load Balancer| ---> |API Gateway Timeout| ---> |Internal Load Balancer| ---> |Upstream/Backend Service Tiemout|
|
||||
```
|
||||
|
||||
The approach based on too long timeout values is not acceptable. You **MUST** follow a fast-fail approach with a expected duration of the upload. If this time is exceeded a timeout error **SHOULD** be sent to the API Consumer. The maximum size limit **SHOULD** be consistent with the timeout value.
|
||||
|
||||
> Please also consider the client and API Gateway Timeout settings. In this case the lack of retrieval of a response during a too long upload operation can trigger a timeout error.
|
||||
@@ -1,6 +1,6 @@
|
||||
# Asynchronous Tasks
|
||||
# Polling
|
||||
|
||||
If an API operation is asynchronous, but a client could track its progress, the response to such an asynchronous operation **MUST** return, in the case of success, the **202 Accepted** status code together with an `application/hal+json` representation of a new **task-tracking resource**.
|
||||
If an API operation can be considered as a long running task and the API Consumer can track its progress, the response to the LRT **MUST** return, in the case of success, the **202 Accepted** status code together with an `application/hal+json` representation of a new **task-tracking resource**.
|
||||
|
||||
## Task Tracking Resource
|
||||
|
||||
@@ -22,13 +22,16 @@ Retrieval of such a resource using the HTTP GET Request Method **SHOULD** be des
|
||||
|
||||
## Design Note
|
||||
|
||||
The asynchronous operation task-tracking resource can be either **polled** by client or the client might initially provide a **callback** to be executed when the operation finishes.
|
||||
The polling (task-tracking) operation requires a clear adaptation on the API Consumer side:
|
||||
|
||||
- Polling requests frequency depend on the type of operation and specific latency of thre resource.
|
||||
- The identification of the resource has to be correlated along the series of polling requests. The API Consumer has to be able to save this ID and the API Producer has to be able to identify the progress of the operation with that ID.
|
||||
- A security problem can be raised if a non-authorized client retrieves the response for a different resource ID. The authorization data and tasks in progress have to be strongly correlated and controlled to avoid consistency issues.
|
||||
|
||||
In the case of callback, the API and its client MUST agree on what HTTP method and request format is used for the callback invitation. If built within adidas, the "client" API is also the subject of the adidas API guidelines.
|
||||
|
||||
### Example
|
||||
|
||||
1. **Initiate the asynchronous task**
|
||||
1. **Initiate the polling task**
|
||||
|
||||
```
|
||||
POST /feeds/tasks/ HTTP/1.1
|
||||
@@ -1,69 +1,43 @@
|
||||
# Rate Limiting
|
||||
|
||||
The API rate limiting is provided by the selected adidas API management platform – Mashery.
|
||||
Rate limit means how many HTTP requests can be made in a given period of time.
|
||||
|
||||
Rate limit information is provided in the for of HTTP headers. There are two types of rate limits: **Quota** and **Throttle**. The quota is a limit enforced per a longer period \(typically a day\). The throttle is the limit of calls per second.
|
||||
The API rate limiting is provided by the selected adidas API Gateway – [Kong](https://konghq.com/kong/). It can be applied to 1 or more endpoints or to the whole API.
|
||||
|
||||
## Quota Limit
|
||||
Rate limit information is provided in the for of HTTP headers.
|
||||
|
||||
The limit on the number of calls per a period \(day\). The default quota limit is 5000 calls per day.
|
||||
## Settings (adidas API Gateway)
|
||||
|
||||
### Example
|
||||
The limit on the number of calls per a time period \(second, minute, hour, day, month, year\). The configuration settings have to be obtained from the Non-Functional Requirements of the API to be included as part of the settings of the API Gateway.
|
||||
|
||||
Example response to a request over the quota limit:
|
||||
A complete reference for configuration can be seen [here](https://adidas.gitbook.io/api-guidelines/rest-api-guidelines/execution/rate-limiting).
|
||||
|
||||
|
||||
## Rate Limit
|
||||
|
||||
When this feature is enabled, the API Gateway will send some additional headers back to the client telling what are the limits allowed, how many requests are available and how long it will take until the quota will be restored. For instance (successful response):
|
||||
|
||||
```text
|
||||
HTTP/1.1 403 Forbidden
|
||||
Content-Type: application/problem+json
|
||||
|
||||
X-Error-Detail-Header: Account Over Rate Limit
|
||||
X-Mashery-Error-Code: ERR_403_DEVELOPER_OVER_RATE
|
||||
|
||||
{
|
||||
"title": "Rate Limit Exceeded",
|
||||
"detail": "Account Over Rate Limit"
|
||||
}
|
||||
RateLimit-Limit: 6
|
||||
RateLimit-Remaining: 4
|
||||
RateLimit-Reset: 47
|
||||
X-RateLimit-Limit-Minute: 10
|
||||
X-RateLimit-Remaining-Minute: 9
|
||||
```
|
||||
|
||||
## Throttle Limit
|
||||
## Rate Limit Exceeded
|
||||
|
||||
The limit on the number of calls per second. The default throttle limit is two calls per second.
|
||||
|
||||
### Example
|
||||
|
||||
Example response to a request over the throttle limit:
|
||||
If any of the limits configured in the API Gateway is being reached, it will return a HTTP/1.1 429 status code to the client:
|
||||
|
||||
```text
|
||||
HTTP/1.1 403 Forbidden
|
||||
Content-Type: application/problem+json
|
||||
HTTP/1.1 429 Too Many Requests
|
||||
Content-Type: application/json
|
||||
|
||||
Retry-After: 1
|
||||
X-Error-Detail-Header: Account Over Queries Per Second Limit
|
||||
X-Mashery-Error-Code: ERR_403_DEVELOPER_OVER_QPS
|
||||
|
||||
{
|
||||
"title": "Quota Limit Exceeded",
|
||||
"detail": "Account Over Queries Per Second Limit"
|
||||
}
|
||||
|
||||
{ "message": "API rate limit exceeded" }
|
||||
```
|
||||
|
||||
> NOTE: The `Retry-After` gives a hint how long before the same request should be repeated \(in seconds\).
|
||||
|
||||
## Detail Information
|
||||
|
||||
By default, the headers do not contain details about the current usage and quotas. The default can be changed in the API management.
|
||||
|
||||
### Example
|
||||
|
||||
A successful response with the details about throttle \(`X-Plan-QPS`\) and quota \(`X-Plan-Quota`\) rate limits:
|
||||
|
||||
```text
|
||||
HTTP/1.1 200 OK
|
||||
|
||||
X-Plan-QPS-Allotted: 10
|
||||
X-Plan-QPS-Current: 1
|
||||
X-Plan-Quota-Allotted: 1000
|
||||
X-Plan-Quota-Current: 2
|
||||
X-Plan-Quota-Reset: Tuesday, June 6, 2017 12:00:00 AM GMT
|
||||
```
|
||||
> NOTE: The response header `Retry-After` gives a hint how long before the same request should be repeated \(in seconds\).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user