Coverage for oc_ds_converter / oc_idmanager / support.py: 59%
32 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 18:06 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 18:06 +0000
1# SPDX-FileCopyrightText: 2023-2026 Arcangelo Massari <arcangelo.massari@unibo.it>
2#
3# SPDX-License-Identifier: ISC
6from __future__ import annotations
8from json import loads
9from time import sleep
11from bs4 import BeautifulSoup
12from requests import ReadTimeout, get
13from requests.exceptions import ConnectionError
16def call_api(
17 url: str, headers: dict[str, str], r_format: str = "json"
18) -> dict[str, object] | BeautifulSoup | None:
19 tentative = 3
20 while tentative:
21 tentative -= 1
22 try:
23 r = get(url, headers=headers, timeout=30)
24 if r.status_code == 200:
25 r.encoding = "utf-8"
26 if r_format == "json":
27 return loads(r.text) # type: ignore[no-any-return]
28 return BeautifulSoup(r.text, "xml")
29 if r.status_code == 404:
30 return None
31 except ReadTimeout:
32 pass
33 except ConnectionError:
34 sleep(5)
35 return None
38def extract_info(
39 api_response: dict[str, object],
40 choose_api: str | None = None,
41 orcid_doi_filepath: str = "",
42) -> dict[str, bool | object]:
43 from oc_ds_converter.oc_idmanager.metadata_manager import MetadataManager
45 info_dict: dict[str, bool | object] = {"valid": True}
46 metadata_manager = MetadataManager(
47 metadata_provider=choose_api, # type: ignore[arg-type]
48 api_response=api_response, # type: ignore[arg-type]
49 orcid_doi_filepath=orcid_doi_filepath,
50 )
51 metadata = metadata_manager.extract_metadata()
52 if metadata:
53 info_dict.update(metadata)
54 return info_dict