Skip to content

Commit

Permalink
add 1s timeout to retry throttle (#1422)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio authored Mar 25, 2024
1 parent 8750020 commit 411fade
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout } from "timers/promises";
import { ethers } from "ethers";
import {
getCollectionPath,
Expand Down Expand Up @@ -606,20 +607,36 @@ export class OpenSeaAPI {
* @param body Optional body to send. If set, will POST, otherwise GET
*/
private async _fetch(url: string, headers?: object, body?: object) {
// Create the fetch request
const req = new ethers.FetchRequest(url);

// Set the headers
headers = {
"x-app-id": "opensea-js",
...(this.apiKey ? { "X-API-KEY": this.apiKey } : {}),
...headers,
};

const req = new ethers.FetchRequest(url);
for (const [key, value] of Object.entries(headers)) {
req.setHeader(key, value);
}

// Set the body if provided
if (body) {
req.body = body;
}

// Set the throttle params
// - Should be able to replace this retryFunc with `setThrottleParams({ slotInterval: 1000 })`
// when this bug is fixed in ethers: https://github.com/ethers-io/ethers.js/issues/4663
req.retryFunc = async (_req, resp, attempt) => {
this.logger(
`Fetch attempt ${attempt} failed with status ${resp.statusCode}`,
);
// Wait 1s between tries
await setTimeout(1000);
return true;
};

this.logger(
`Sending request: ${url} ${JSON.stringify({
request: req,
Expand Down

0 comments on commit 411fade

Please sign in to comment.