Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit f01c3ac

Browse files
authored
Merge pull request #109 from julienp/recreate-options-on-retry
Recreate function body on retry
2 parents e9a8e8b + 5f9fff9 commit f01c3ac

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/index.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,26 @@ test('Gives up retrying on API rate limiting after a timeout', async t => {
553553
t.false(scope.isDone())
554554
})
555555

556+
test('Recreates a function body when handling API rate limiting', async t => {
557+
const deploy_id = '3'
558+
const path = 'testPath'
559+
const body = 'test'
560+
const retryAtMs = Date.now() + TEST_RATE_LIMIT_DELAY
561+
const retryAt = Math.ceil(retryAtMs / SECS_TO_MSECS)
562+
const expectedResponse = { test: 'test' }
563+
const scope = nock(origin)
564+
.put(`${pathPrefix}/deploys/${deploy_id}/files/${path}`, body, { 'Content-Type': 'application/octet-stream' })
565+
.reply(429, { retryAt }, { 'X-RateLimit-Reset': retryAt })
566+
.put(`${pathPrefix}/deploys/${deploy_id}/files/${path}`, body, { 'Content-Type': 'application/octet-stream' })
567+
.reply(200, expectedResponse)
568+
const client = getClient()
569+
const response = await client.uploadDeployFile({ deploy_id, path, body: () => fromString(body) })
570+
571+
t.true(Date.now() >= retryAtMs)
572+
t.deepEqual(response, expectedResponse)
573+
t.true(scope.isDone())
574+
})
575+
556576
test('Can set (proxy) agent', async t => {
557577
const client = getClient({ accessToken, agent })
558578
t.is(client.agent, agent)

src/methods/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ const getMethod = function(method, NetlifyApi) {
3131
const callMethod = async function(method, NetlifyApi, params, opts) {
3232
const requestParams = Object.assign({}, NetlifyApi.globalParams, params)
3333
const url = getUrl(method, NetlifyApi, requestParams)
34-
const optsA = getOpts(method, NetlifyApi, requestParams, opts)
35-
36-
const response = await makeRequestOrRetry(url, optsA)
34+
const response = await makeRequestOrRetry({ url, method, NetlifyApi, requestParams, opts })
3735

3836
const parsedResponse = await parseResponse(response)
3937
return parsedResponse
@@ -68,9 +66,10 @@ const addAgent = function(NetlifyApi, opts) {
6866
}
6967
}
7068

71-
const makeRequestOrRetry = async function(url, opts) {
69+
const makeRequestOrRetry = async function({ url, method, NetlifyApi, requestParams, opts }) {
7270
for (let index = 0; index <= MAX_RETRY; index++) {
73-
const response = await makeRequest(url, opts)
71+
const optsA = getOpts(method, NetlifyApi, requestParams, opts)
72+
const response = await makeRequest(url, optsA)
7473

7574
if (shouldRetry(response, index)) {
7675
await waitForRetry(response)

0 commit comments

Comments
 (0)