|
1 | 1 | import json |
2 | | -import pathlib |
| 2 | +import time |
3 | 3 | from datetime import date |
| 4 | +from http import HTTPStatus |
4 | 5 | from typing import List, Generator |
5 | 6 |
|
6 | 7 | import requests |
|
9 | 10 | from ted_sws.event_manager.services.log import log_warning |
10 | 11 | from ted_sws.notice_fetcher.adapters.ted_api_abc import TedAPIAdapterABC, RequestAPI |
11 | 12 |
|
12 | | - |
13 | 13 | DOCUMENTS_PER_PAGE = 100 |
14 | 14 |
|
15 | 15 | DEFAULT_TED_API_QUERY_RESULT_SIZE = {"limit": DOCUMENTS_PER_PAGE, |
@@ -42,6 +42,13 @@ def __call__(self, api_url: str, api_query: dict) -> dict: |
42 | 42 | """ |
43 | 43 |
|
44 | 44 | 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 |
45 | 52 | if response.ok: |
46 | 53 | response_content = json.loads(response.text) |
47 | 54 | return response_content |
@@ -109,7 +116,13 @@ def _retrieve_document_content(self, document_content: dict) -> str: |
109 | 116 |
|
110 | 117 | xml_document_content_link = xml_links[language_key] |
111 | 118 | 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 |
113 | 126 | if response.ok: |
114 | 127 | return response.text |
115 | 128 | else: |
|
0 commit comments