You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "Learn how to simplify polling behaviors in Speakeasy-managed SDKs by creating SDKs with built-in polling methods."
3
+
sidebar_label: "Add Polling"
4
+
---
5
+
6
+
import { Callout } from"@/mdx/components";
7
+
8
+
# Adding polling to SDKs
9
+
10
+
Customize polling methods for each API operation using the `x-speakeasy-polling` extension.
11
+
12
+
Adding polling to an SDK enhances the developer experience by providing a consistent way to handle consuming code that waits for a particular API response change, which is common in API backends that perform asynchronous operations.
The polling methods, such as `PollingEndpointWaitForCompleted()` above, are generated from configuration within the API operation that declares one or more success criteria.
22
+
23
+
## Configuring polling
24
+
25
+
To configure polling, add the `x-speakeasy-polling` extension to the OpenAPI Specification document.
The `x-speakeasy-polling` configuration supports multiple entries with unique names, which will generate unique polling methods with their required success criteria.
67
+
68
+
## Configuration options
69
+
70
+
### delaySeconds
71
+
72
+
Customize the delay before the API operation requests begin. By default, this is set to 1 second.
The `WaitForCompleted` polling method makes the first API operation request after 5 seconds. Clients can override this value when using the SDK.
89
+
90
+
### failureCriteria
91
+
92
+
Customize the polling method to immediately return an error when defined conditions are met. This is useful when the API response contains any sort of "errored" or "failed" status value.
93
+
94
+
<Callout title="Note" type="info">
95
+
It is not necessary to define failure criteria for API responses that are
96
+
already considered errors, such as a 4XX or 5XX HTTP status code.
The `WaitForCompleted` polling method will immediately return an error when the response body `status` property value is `errored`. Clients cannot override this behavior.
113
+
114
+
Refer to `successCriteria` for additional information about supported criteria as they share implementation details.
115
+
116
+
### intervalSeconds
117
+
118
+
Customize the interval between the API operation requests. By default, this is set to 1 second.
The `WaitForCompleted` polling method makes the next API operation request 5 seconds after the previous response. Clients can override this value when using the SDK.
135
+
136
+
### limitCount
137
+
138
+
Customize the total number of API operation requests before raising an error. By default, this is set to 60.
The `WaitForCompleted` polling method makes at most 120 API operation requests. Clients can override this value when using the SDK.
155
+
156
+
### successCriteria
157
+
158
+
Configure the polling method success criteria. This configuration is required for all polling methods. The criteria themselves are a limited subset of [OpenAPI Arazzo criterion](https://spec.openapis.org/arazzo/latest.html#criterion-object). Each condition is a logical AND operation, evaluated in configuration order.
159
+
160
+
Supported conditions include:
161
+
162
+
- `$statusCode`: HTTP status code. Must be defined before response body conditions.
163
+
- `$response.body`: HTTP response body data. A value within the data is defined with a JSONPath expression.
164
+
165
+
Supported operators include:
166
+
167
+
- `==`: Equals
168
+
- `!=`: Not Equals
169
+
170
+
Certain language implementations, such as Go, also support using error status codes as success criteria. In these cases any SDK error that would have been returned to the SDK client are swallowed instead.
171
+
172
+
In this example:
173
+
174
+
```yaml
175
+
x-speakeasy-polling:
176
+
- name: WaitForNotFound
177
+
successCriteria:
178
+
- condition: $statusCode == 404
179
+
```
180
+
181
+
The `WaitForNotFound` polling method immediately returns without error when the API operation returns a 404 Not Found HTTP status code.
Properties above the `x-speakeasy-entity` annotation are flattened, which could cause conflicts. Apply the annotation carefully to align the structure of the Terraform provider with the API's intended interaction.
131
+
Properties above the `x-speakeasy-entity` annotation are flattened, which
132
+
could cause conflicts. Apply the annotation carefully to align the structure
133
+
of the Terraform provider with the API's intended interaction.
One API operation for multiple resources can be combined with the entity operation ordering of [multiple API operations for one resource](#multiple-api-operations-for-one-resource) as necessary.
239
242
243
+
### API Operation Polling
244
+
245
+
Define automatic API operation polling logic for APIs with asynchronous behaviors via the `x-speakeasy-polling` and `x-speakeasy-entity-operation` extensions in the OpenAPI Specification document. Refer to the [SDK Polling documentation](/docs/customize/runtime/polling) for additional information about configuring `x-speakeasy-polling`.
The API operation is used for both the read operation and the second create operation, where the second create operation will use the `WaitForCompleted` polling method to ensure the success criteria is met before the resource logic (and therefore Terraform) continues.
292
+
293
+
There are `delaySeconds`, `intervalSeconds`, and `limitCount` configurations to override the `x-speakeasy-polling` configuration values for a specific entity operation.
The `WaitForCompleted` polling method for the API operation defaults to a 5 second interval, however the create entity operation overrides to a 10 second interval.
319
+
240
320
### Manual association between Operations and Resource / Data Sources
241
321
242
322
The default behavior within Speakeasy is to automatically infer a data source from all operations that have an `x-speakeasy-entity-operation: Entity#read` association defined.
@@ -335,7 +415,7 @@ Since the response is `type: array`, Speakeasy wraps it in a `data` attribute fo
335
415
Access in Terraform:
336
416
337
417
```hcl
338
-
data.my_things_list.data[0].id
418
+
data.example_things.data[0].id
339
419
```
340
420
341
421
**Customize the wrapper name:**
@@ -346,7 +426,7 @@ paths:
346
426
get:
347
427
x-speakeasy-entity-operation: Things#read
348
428
responses:
349
-
'200':
429
+
"200":
350
430
description: OK
351
431
content:
352
432
application/json:
@@ -360,11 +440,13 @@ paths:
360
440
Now the wrapping attribute is named `items` instead:
361
441
362
442
```hcl
363
-
data.my_things_list.items[0].id
443
+
data.example_things.items[0].id
364
444
```
365
445
366
446
<Callout title="Note" type="info">
367
-
The wrapping attribute name is a Terraform construct created by Speakeasy, completely separate from your API's response structure. Use `x-speakeasy-wrapped-attribute` to customize it.
447
+
The wrapping attribute name is a Terraform construct created by Speakeasy,
448
+
completely separate from your API's response structure. Use
0 commit comments