Skip to content

Commit f5ca3d0

Browse files
Update ted_api.py
1 parent 800f480 commit f5ca3d0

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

ted_sws/notice_fetcher/adapters/ted_api.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
2-
import pathlib
2+
import time
33
from datetime import date
4+
from http import HTTPStatus
45
from typing import List, Generator
56

67
import requests
@@ -9,7 +10,6 @@
910
from ted_sws.event_manager.services.log import log_warning
1011
from ted_sws.notice_fetcher.adapters.ted_api_abc import TedAPIAdapterABC, RequestAPI
1112

12-
1313
DOCUMENTS_PER_PAGE = 100
1414

1515
DEFAULT_TED_API_QUERY_RESULT_SIZE = {"limit": DOCUMENTS_PER_PAGE,
@@ -42,6 +42,13 @@ def __call__(self, api_url: str, api_query: dict) -> dict:
4242
"""
4343

4444
response = requests.post(api_url, json=api_query)
45+
try_again_request_count = 0
46+
while response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
47+
try_again_request_count += 1
48+
time.sleep(try_again_request_count * 0.1)
49+
response = requests.post(api_url, json=api_query)
50+
if try_again_request_count > 5:
51+
break
4552
if response.ok:
4653
response_content = json.loads(response.text)
4754
return response_content
@@ -109,7 +116,13 @@ def _retrieve_document_content(self, document_content: dict) -> str:
109116

110117
xml_document_content_link = xml_links[language_key]
111118
response = requests.get(xml_document_content_link)
112-
119+
try_again_request_count = 0
120+
while response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
121+
try_again_request_count += 1
122+
time.sleep(try_again_request_count * 0.1)
123+
response = requests.get(xml_document_content_link)
124+
if try_again_request_count > 5:
125+
break
113126
if response.ok:
114127
return response.text
115128
else:

0 commit comments

Comments
 (0)