Skip to content

Commit 4960e00

Browse files
Handle throttled responses (#115)
Handle responses with the `429 Too Many Requests` status code. --------- Co-authored-by: fern-api <115122769+fern-api[bot]@users.noreply.github.com> Co-authored-by: Jay Vercellone <[email protected]>
1 parent e8d824a commit 4960e00

22 files changed

+717
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Add the dependency in your `pom.xml` file:
2525
<dependency>
2626
<groupId>com.pipedream</groupId>
2727
<artifactId>pipedream</artifactId>
28-
<version>1.0.3</version>
28+
<version>1.0.4</version>
2929
</dependency>
3030
```
3131

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ java {
4848

4949
group = 'com.pipedream'
5050

51-
version = '1.0.3'
51+
version = '1.0.4'
5252

5353
jar {
5454
dependsOn(":generatePomFileForMavenPublication")
@@ -79,7 +79,7 @@ publishing {
7979
maven(MavenPublication) {
8080
groupId = 'com.pipedream'
8181
artifactId = 'pipedream'
82-
version = '1.0.3'
82+
version = '1.0.4'
8383
from components.java
8484
pom {
8585
name = 'pipedream'

src/main/java/com/pipedream/api/core/ClientOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ private ClientOptions(
3535
this.headers.putAll(headers);
3636
this.headers.putAll(new HashMap<String, String>() {
3737
{
38-
put("User-Agent", "com.pipedream:pipedream/1.0.3");
38+
put("User-Agent", "com.pipedream:pipedream/1.0.4");
3939
put("X-Fern-Language", "JAVA");
4040
put("X-Fern-SDK-Name", "com.pipedream.fern:api-sdk");
41-
put("X-Fern-SDK-Version", "1.0.3");
41+
put("X-Fern-SDK-Version", "1.0.4");
4242
}
4343
});
4444
this.headerSuppliers = headerSuppliers;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* This file was auto-generated by Fern from our API Definition.
3+
*/
4+
package com.pipedream.api.errors;
5+
6+
import com.pipedream.api.core.BaseClientApiException;
7+
import okhttp3.Response;
8+
9+
public final class TooManyRequestsError extends BaseClientApiException {
10+
/**
11+
* The body of the response that triggered the exception.
12+
*/
13+
private final Object body;
14+
15+
public TooManyRequestsError(Object body) {
16+
super("TooManyRequestsError", 429, body);
17+
this.body = body;
18+
}
19+
20+
public TooManyRequestsError(Object body, Response rawResponse) {
21+
super("TooManyRequestsError", 429, body, rawResponse);
22+
this.body = body;
23+
}
24+
25+
/**
26+
* @return the body
27+
*/
28+
@java.lang.Override
29+
public Object body() {
30+
return this.body;
31+
}
32+
}

src/main/java/com/pipedream/api/resources/accounts/AsyncRawAccountsClient.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
package com.pipedream.api.resources.accounts;
55

6+
import com.fasterxml.jackson.core.JsonProcessingException;
67
import com.pipedream.api.core.BaseClientApiException;
78
import com.pipedream.api.core.BaseClientException;
89
import com.pipedream.api.core.BaseClientHttpResponse;
@@ -12,6 +13,7 @@
1213
import com.pipedream.api.core.QueryStringMapper;
1314
import com.pipedream.api.core.RequestOptions;
1415
import com.pipedream.api.core.pagination.SyncPagingIterable;
16+
import com.pipedream.api.errors.TooManyRequestsError;
1517
import com.pipedream.api.resources.accounts.requests.AccountsListRequest;
1618
import com.pipedream.api.resources.accounts.requests.AccountsRetrieveRequest;
1719
import com.pipedream.api.resources.accounts.requests.CreateAccountOpts;
@@ -136,6 +138,15 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
136138
return;
137139
}
138140
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
141+
try {
142+
if (response.code() == 429) {
143+
future.completeExceptionally(new TooManyRequestsError(
144+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response));
145+
return;
146+
}
147+
} catch (JsonProcessingException ignored) {
148+
// unable to map error response, throwing generic error
149+
}
139150
future.completeExceptionally(new BaseClientApiException(
140151
"Error with status code " + response.code(),
141152
response.code(),
@@ -220,6 +231,15 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
220231
return;
221232
}
222233
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
234+
try {
235+
if (response.code() == 429) {
236+
future.completeExceptionally(new TooManyRequestsError(
237+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response));
238+
return;
239+
}
240+
} catch (JsonProcessingException ignored) {
241+
// unable to map error response, throwing generic error
242+
}
223243
future.completeExceptionally(new BaseClientApiException(
224244
"Error with status code " + response.code(),
225245
response.code(),
@@ -293,6 +313,15 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
293313
return;
294314
}
295315
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
316+
try {
317+
if (response.code() == 429) {
318+
future.completeExceptionally(new TooManyRequestsError(
319+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response));
320+
return;
321+
}
322+
} catch (JsonProcessingException ignored) {
323+
// unable to map error response, throwing generic error
324+
}
296325
future.completeExceptionally(new BaseClientApiException(
297326
"Error with status code " + response.code(),
298327
response.code(),
@@ -334,6 +363,7 @@ public CompletableFuture<BaseClientHttpResponse<Void>> delete(String accountId,
334363
.url(httpUrl)
335364
.method("DELETE", null)
336365
.headers(Headers.of(clientOptions.headers(requestOptions)))
366+
.addHeader("Accept", "application/json")
337367
.build();
338368
OkHttpClient client = clientOptions.httpClient();
339369
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
@@ -349,6 +379,15 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
349379
return;
350380
}
351381
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
382+
try {
383+
if (response.code() == 429) {
384+
future.completeExceptionally(new TooManyRequestsError(
385+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response));
386+
return;
387+
}
388+
} catch (JsonProcessingException ignored) {
389+
// unable to map error response, throwing generic error
390+
}
352391
future.completeExceptionally(new BaseClientApiException(
353392
"Error with status code " + response.code(),
354393
response.code(),
@@ -391,6 +430,7 @@ public CompletableFuture<BaseClientHttpResponse<Void>> deleteByApp(String appId,
391430
.url(httpUrl)
392431
.method("DELETE", null)
393432
.headers(Headers.of(clientOptions.headers(requestOptions)))
433+
.addHeader("Accept", "application/json")
394434
.build();
395435
OkHttpClient client = clientOptions.httpClient();
396436
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
@@ -406,6 +446,15 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
406446
return;
407447
}
408448
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
449+
try {
450+
if (response.code() == 429) {
451+
future.completeExceptionally(new TooManyRequestsError(
452+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response));
453+
return;
454+
}
455+
} catch (JsonProcessingException ignored) {
456+
// unable to map error response, throwing generic error
457+
}
409458
future.completeExceptionally(new BaseClientApiException(
410459
"Error with status code " + response.code(),
411460
response.code(),

src/main/java/com/pipedream/api/resources/accounts/RawAccountsClient.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
package com.pipedream.api.resources.accounts;
55

6+
import com.fasterxml.jackson.core.JsonProcessingException;
67
import com.pipedream.api.core.BaseClientApiException;
78
import com.pipedream.api.core.BaseClientException;
89
import com.pipedream.api.core.BaseClientHttpResponse;
@@ -12,6 +13,7 @@
1213
import com.pipedream.api.core.QueryStringMapper;
1314
import com.pipedream.api.core.RequestOptions;
1415
import com.pipedream.api.core.pagination.SyncPagingIterable;
16+
import com.pipedream.api.errors.TooManyRequestsError;
1517
import com.pipedream.api.resources.accounts.requests.AccountsListRequest;
1618
import com.pipedream.api.resources.accounts.requests.AccountsRetrieveRequest;
1719
import com.pipedream.api.resources.accounts.requests.CreateAccountOpts;
@@ -120,6 +122,14 @@ public BaseClientHttpResponse<SyncPagingIterable<Account>> list(
120122
response);
121123
}
122124
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
125+
try {
126+
if (response.code() == 429) {
127+
throw new TooManyRequestsError(
128+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
129+
}
130+
} catch (JsonProcessingException ignored) {
131+
// unable to map error response, throwing generic error
132+
}
123133
throw new BaseClientApiException(
124134
"Error with status code " + response.code(),
125135
response.code(),
@@ -190,6 +200,14 @@ public BaseClientHttpResponse<Account> create(CreateAccountOpts request, Request
190200
ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Account.class), response);
191201
}
192202
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
203+
try {
204+
if (response.code() == 429) {
205+
throw new TooManyRequestsError(
206+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
207+
}
208+
} catch (JsonProcessingException ignored) {
209+
// unable to map error response, throwing generic error
210+
}
193211
throw new BaseClientApiException(
194212
"Error with status code " + response.code(),
195213
response.code(),
@@ -249,6 +267,14 @@ public BaseClientHttpResponse<Account> retrieve(
249267
ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Account.class), response);
250268
}
251269
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
270+
try {
271+
if (response.code() == 429) {
272+
throw new TooManyRequestsError(
273+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
274+
}
275+
} catch (JsonProcessingException ignored) {
276+
// unable to map error response, throwing generic error
277+
}
252278
throw new BaseClientApiException(
253279
"Error with status code " + response.code(),
254280
response.code(),
@@ -281,6 +307,7 @@ public BaseClientHttpResponse<Void> delete(String accountId, RequestOptions requ
281307
.url(httpUrl)
282308
.method("DELETE", null)
283309
.headers(Headers.of(clientOptions.headers(requestOptions)))
310+
.addHeader("Accept", "application/json")
284311
.build();
285312
OkHttpClient client = clientOptions.httpClient();
286313
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
@@ -292,6 +319,14 @@ public BaseClientHttpResponse<Void> delete(String accountId, RequestOptions requ
292319
return new BaseClientHttpResponse<>(null, response);
293320
}
294321
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
322+
try {
323+
if (response.code() == 429) {
324+
throw new TooManyRequestsError(
325+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
326+
}
327+
} catch (JsonProcessingException ignored) {
328+
// unable to map error response, throwing generic error
329+
}
295330
throw new BaseClientApiException(
296331
"Error with status code " + response.code(),
297332
response.code(),
@@ -325,6 +360,7 @@ public BaseClientHttpResponse<Void> deleteByApp(String appId, RequestOptions req
325360
.url(httpUrl)
326361
.method("DELETE", null)
327362
.headers(Headers.of(clientOptions.headers(requestOptions)))
363+
.addHeader("Accept", "application/json")
328364
.build();
329365
OkHttpClient client = clientOptions.httpClient();
330366
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
@@ -336,6 +372,14 @@ public BaseClientHttpResponse<Void> deleteByApp(String appId, RequestOptions req
336372
return new BaseClientHttpResponse<>(null, response);
337373
}
338374
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
375+
try {
376+
if (response.code() == 429) {
377+
throw new TooManyRequestsError(
378+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
379+
}
380+
} catch (JsonProcessingException ignored) {
381+
// unable to map error response, throwing generic error
382+
}
339383
throw new BaseClientApiException(
340384
"Error with status code " + response.code(),
341385
response.code(),

src/main/java/com/pipedream/api/resources/actions/AsyncRawActionsClient.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.pipedream.api.core.QueryStringMapper;
1414
import com.pipedream.api.core.RequestOptions;
1515
import com.pipedream.api.core.pagination.SyncPagingIterable;
16+
import com.pipedream.api.errors.TooManyRequestsError;
1617
import com.pipedream.api.resources.actions.requests.ActionsListRequest;
1718
import com.pipedream.api.resources.actions.requests.RunActionOpts;
1819
import com.pipedream.api.types.Component;
@@ -127,6 +128,15 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
127128
return;
128129
}
129130
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
131+
try {
132+
if (response.code() == 429) {
133+
future.completeExceptionally(new TooManyRequestsError(
134+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response));
135+
return;
136+
}
137+
} catch (JsonProcessingException ignored) {
138+
// unable to map error response, throwing generic error
139+
}
130140
future.completeExceptionally(new BaseClientApiException(
131141
"Error with status code " + response.code(),
132142
response.code(),
@@ -187,6 +197,15 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
187197
return;
188198
}
189199
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
200+
try {
201+
if (response.code() == 429) {
202+
future.completeExceptionally(new TooManyRequestsError(
203+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response));
204+
return;
205+
}
206+
} catch (JsonProcessingException ignored) {
207+
// unable to map error response, throwing generic error
208+
}
190209
future.completeExceptionally(new BaseClientApiException(
191210
"Error with status code " + response.code(),
192211
response.code(),
@@ -254,6 +273,15 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
254273
return;
255274
}
256275
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
276+
try {
277+
if (response.code() == 429) {
278+
future.completeExceptionally(new TooManyRequestsError(
279+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response));
280+
return;
281+
}
282+
} catch (JsonProcessingException ignored) {
283+
// unable to map error response, throwing generic error
284+
}
257285
future.completeExceptionally(new BaseClientApiException(
258286
"Error with status code " + response.code(),
259287
response.code(),
@@ -321,6 +349,15 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
321349
return;
322350
}
323351
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
352+
try {
353+
if (response.code() == 429) {
354+
future.completeExceptionally(new TooManyRequestsError(
355+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response));
356+
return;
357+
}
358+
} catch (JsonProcessingException ignored) {
359+
// unable to map error response, throwing generic error
360+
}
324361
future.completeExceptionally(new BaseClientApiException(
325362
"Error with status code " + response.code(),
326363
response.code(),
@@ -388,6 +425,15 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
388425
return;
389426
}
390427
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
428+
try {
429+
if (response.code() == 429) {
430+
future.completeExceptionally(new TooManyRequestsError(
431+
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response));
432+
return;
433+
}
434+
} catch (JsonProcessingException ignored) {
435+
// unable to map error response, throwing generic error
436+
}
391437
future.completeExceptionally(new BaseClientApiException(
392438
"Error with status code " + response.code(),
393439
response.code(),

0 commit comments

Comments
 (0)