Skip to content

Commit 3c3a956

Browse files
committed
feat: retry request if api returns a 429 when precaching nodes
1 parent 557e708 commit 3c3a956

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

scripts/precache_nodes.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import json
99
import os
10+
import time
1011
import typing
1112
from itertools import batched
1213

@@ -16,7 +17,7 @@
1617
from user_agent import user_agent
1718

1819

19-
def fetch_drupal_nodes(nids: list[str]) -> list[drupal.Node]:
20+
def fetch_drupal_nodes(nids: list[str], retry=True) -> list[drupal.Node]:
2021
"""
2122
Fetches a node from drupal.org by its id
2223
"""
@@ -27,6 +28,13 @@ def fetch_drupal_nodes(nids: list[str]) -> list[drupal.Node]:
2728

2829
resp = requests.get(url, headers={'user-agent': user_agent})
2930

31+
if retry and resp.status_code == 429:
32+
seconds = int(resp.headers.get('Retry-After', 0))
33+
print(f' |* (waiting {seconds} seconds before retrying)')
34+
time.sleep(seconds)
35+
36+
return fetch_drupal_nodes(nids, retry=False)
37+
3038
if resp.status_code == 200:
3139
items = typing.cast(drupal.ApiResponse[drupal.Node], resp.json())['list']
3240

0 commit comments

Comments
 (0)