Introduction
Some operations do not finish inside a single request. This extension defines how an endpoint signals that the client should wait, then send the same request again. The signal is a defer key on the returned object. When defer is true, wait is how long to wait.
Activation
An endpoint activates this extension by including the deferred flag in its flags array. When the flag is present, a response MAY be a deferral as defined below. Clients that implement this extension MUST check for the flag and, when it is present, follow the client behavior in this document.
returns describes the response body. defer and wait are keys of that body. This flag MAY appear alongside other endpoint flags.
A package types those keys by declaring them on the object. Add them to a named object's attributes, or to the endpoint's attributes when returns is the bare object type. A package that does not declare them leaves the keys untyped. Clients still follow this extension from the flag.
Deferral response
A response is a deferral when all of the following are true:
- The status is
200. - The body is a JSON object.
- The object contains the key
defer, and its value istrue. - The object contains the key
wait, and its value is a positive number.
A positive number is a JSON number greater than 0. The value of wait is how long the client MUST wait, in milliseconds, before sending the same request again.
{
"defer": true,
"wait": 1000
}
Other keys MAY be present. They are part of the returned object. The client reads defer and wait to decide whether to send the request again.
A response is not a deferral when defer is false, when defer is absent, when the status is not 200, or when the body is not a JSON object. The client MUST NOT send the request again. It surfaces that body. A value of defer other than true or false is not a deferral.
If defer is true and wait is missing or is not a positive number, the client MUST NOT send the request again and SHOULD surface an error.
Client behavior
After a response from an endpoint whose flags include deferred, a client MUST:
If
deferis nottrue, the client MUST stop and surface the response. It MUST NOT send the request again. This includesdeferset tofalse, a missingdeferkey, a status other than200, and a body that is not a JSON object.If
deferistrueandwaitis missing or is not a positive number, the client MUST NOT send the request again and SHOULD surface an error.Wait the number of milliseconds given by
wait, measured from when the response was received.Send the same request again.
A client MUST apply this behavior only when the endpoint's flags include deferred. A defer key on any other response is part of that response. The client MUST NOT treat it as a deferral.
Same request
The request the client sends again MUST be the request that produced the deferral: the same URL, the same headers, and the same body. The client MUST NOT change the path, the query string, the Authorization header, the Api-Version header, or the JSON body. If the original request had no Authorization header, the new request MUST NOT add one. If the original request had no Api-Version header, the new request MUST NOT add one.
Repeated deferral
If that request is also a deferral, the client MUST apply this behavior again, using the new wait value. The client continues until a response is not a deferral, and surfaces that response to the caller.
Example
This example requests an export that is not ready yet. export has the deferred flag and returns object.export. The first response is a deferral, so the client waits 1000 milliseconds and sends the same request again. The second response is the object the client surfaces. The package is versioned and the endpoint requires a bearer token. Both requests carry the same URL, the same Authorization value, the same Api-Version, and the same body.
Initial request
POST /export
Content-Type: application/json
Accept: application/json
Authorization: Bearer tok_abc123
Api-Version: 2
{
"report_id": "report_9f3"
}
Deferred response
HTTP/1.1 200 OK
Content-Type: application/json
{
"defer": true,
"wait": 1000
}
The client waits 1000 milliseconds. url is absent. Any other key on this object is part of object.export.
Resent request
POST /export
Content-Type: application/json
Accept: application/json
Authorization: Bearer tok_abc123
Api-Version: 2
{
"report_id": "report_9f3"
}
The URL, the bearer token, the version, and the body are unchanged.
Result
HTTP/1.1 200 OK
Content-Type: application/json
{
"url": "https://cdn.example.com/report_9f3.csv"
}
This response does not set defer to true, so the client surfaces the body. An object with defer set to false is also surfaced.
Example package definition
url, defer, and wait are attributes of object.export. Each is nullable, because a response may omit it. The deferred response omits url. The surfaced response omits defer and wait.
{
"base_url": "https://api.example.com",
"name": "ExamplePackage",
"flags": ["versioned"],
"version": "2",
"versions": ["1", "2"],
"docs": "Example API. export may respond with defer set to true until the report is ready.",
"objects": [
{
"name": "export",
"attributes": [
{
"name": "url",
"type": "string",
"flags": ["nullable"],
"docs": "Location of the finished report."
},
{
"name": "defer",
"type": "boolean",
"flags": ["nullable"],
"docs": "True while the report is not ready."
},
{
"name": "wait",
"type": "number",
"flags": ["nullable"],
"docs": "Milliseconds to wait before sending the same request again."
}
]
}
],
"endpoints": [
{
"name": "export",
"returns": "object.export",
"flags": ["deferred", "bearer_auth"],
"docs": "Exports a report. May set defer to true until the export is ready.",
"arguments": [
{
"name": "report_id",
"type": "string",
"flags": ["required"],
"docs": "Identifier of the report to export."
}
]
}
]
}