Skip to content

Commit 1816965

Browse files
authored
Fixed yet another api.semanticscholar.org:443 ssl:default error via retrying (#540)
1 parent d334afc commit 1816965

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

paperqa/clients/semantic_scholar.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,23 @@ def make_url_params( # noqa: PLR0911
110110
raise NotImplementedError
111111

112112

113+
@retry(
114+
retry=retry_if_exception(make_flaky_ssl_error_predicate(SEMANTIC_SCHOLAR_HOST)),
115+
before_sleep=before_sleep_log(logger, logging.WARNING),
116+
stop=stop_after_attempt(3),
117+
)
118+
async def _s2_get_with_retrying(url: str, **get_kwargs) -> dict[str, Any]:
119+
return await _get_with_retrying(
120+
url=url,
121+
headers=get_kwargs.get("headers") or semantic_scholar_headers(),
122+
timeout=(
123+
get_kwargs.get("timeout")
124+
or aiohttp.ClientTimeout(SEMANTIC_SCHOLAR_API_REQUEST_TIMEOUT)
125+
),
126+
**get_kwargs,
127+
)
128+
129+
113130
def s2_authors_match(authors: list[str], data: dict) -> bool:
114131
"""Check if the authors in the data match the authors in the paper."""
115132
AUTHOR_NAME_MIN_LENGTH = 2
@@ -131,7 +148,7 @@ def s2_authors_match(authors: list[str], data: dict) -> bool:
131148

132149

133150
async def parse_s2_to_doc_details(
134-
paper_data: dict, session: aiohttp.ClientSession
151+
paper_data: dict[str, Any], session: aiohttp.ClientSession
135152
) -> DocDetails:
136153

137154
bibtex_source = "self_generated"
@@ -217,12 +234,10 @@ async def s2_title_search(
217234
params={"query": title, "fields": fields}
218235
)
219236

220-
data = await _get_with_retrying(
237+
data = await _s2_get_with_retrying(
221238
url=endpoint,
222239
params=params,
223240
session=session,
224-
headers=semantic_scholar_headers(),
225-
timeout=SEMANTIC_SCHOLAR_API_REQUEST_TIMEOUT,
226241
http_exception_mappings={
227242
HTTPStatus.NOT_FOUND: DOINotFoundError(f"Could not find DOI for {title}.")
228243
},
@@ -277,19 +292,18 @@ async def get_s2_doc_details_from_doi(
277292
else:
278293
s2_fields = SEMANTIC_SCHOLAR_API_FIELDS
279294

280-
details = await _get_with_retrying(
281-
url=f"{SEMANTIC_SCHOLAR_BASE_URL}/graph/v1/paper/DOI:{doi}",
282-
params={"fields": s2_fields},
295+
return await parse_s2_to_doc_details(
296+
paper_data=await _s2_get_with_retrying(
297+
url=f"{SEMANTIC_SCHOLAR_BASE_URL}/graph/v1/paper/DOI:{doi}",
298+
params={"fields": s2_fields},
299+
session=session,
300+
http_exception_mappings={
301+
HTTPStatus.NOT_FOUND: DOINotFoundError(f"Could not find DOI for {doi}.")
302+
},
303+
),
283304
session=session,
284-
headers=semantic_scholar_headers(),
285-
timeout=SEMANTIC_SCHOLAR_API_REQUEST_TIMEOUT,
286-
http_exception_mappings={
287-
HTTPStatus.NOT_FOUND: DOINotFoundError(f"Could not find DOI for {doi}.")
288-
},
289305
)
290306

291-
return await parse_s2_to_doc_details(details, session)
292-
293307

294308
async def get_s2_doc_details_from_title(
295309
title: str | None,

paperqa/utils.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -409,20 +409,13 @@ def is_retryable(exc: BaseException) -> bool:
409409
)
410410
async def _get_with_retrying(
411411
url: str,
412-
params: dict[str, Any],
413412
session: aiohttp.ClientSession,
414-
headers: dict[str, str] | None = None,
415-
timeout: float = 10.0, # noqa: ASYNC109
416413
http_exception_mappings: dict[HTTPStatus | int, Exception] | None = None,
414+
**get_kwargs,
417415
) -> dict[str, Any]:
418416
"""Get from a URL with retrying protection."""
419417
try:
420-
async with session.get(
421-
url,
422-
params=params,
423-
headers=headers,
424-
timeout=aiohttp.ClientTimeout(timeout),
425-
) as response:
418+
async with session.get(url, **get_kwargs) as response:
426419
response.raise_for_status()
427420
return await response.json()
428421
except aiohttp.ClientResponseError as e:

0 commit comments

Comments
 (0)