Coverage for datacite/fdsn.py: 97%

29 statements  

« prev     ^ index     » next       coverage.py v7.10.1, created at 2025-07-29 15:38 +0000

1import logging 

2from typing import Any 

3 

4import requests 

5from django.conf import settings 

6 

7FDSN_BASE_URL = settings.FDSN_BASE_URL 

8 

9 

10logger = logging.getLogger(__name__) 

11 

12 

13class FdsnRESTClient: 

14 """ 

15 REST client for the FDSN API. 

16 """ 

17 

18 @staticmethod 

19 def get_all_networks(params: dict | None = None) -> Any | None: 

20 """ 

21 Get the list of network metadata from FDSN repository 

22 :param params: parameters to the query 

23 :return: list of metadata of all the networks presents in the FDSN database 

24 """ 

25 resp: requests.Response = requests.get( 

26 url=FDSN_BASE_URL + "networks/1/query", 

27 params=params, 

28 timeout=1, 

29 ) 

30 if resp.status_code == requests.codes.ok: 

31 logger.info("Fetched the list of networks from FDSN.") 

32 return resp.json()["networks"] 

33 logger.warning("Failed to fetched the list of networks from FDSN.") 

34 return None 

35 

36 @staticmethod 

37 def get_doi_list(params: dict | None = None) -> list: 

38 """ 

39 Get the list of network DOIs from FDSN repository 

40 :param params: parameters to the query 

41 :return: list of DOIs of all the networks presents in the FDSN database 

42 """ 

43 resp = FdsnRESTClient.get_all_networks(params=params) or [] 

44 doi_list = [] 

45 for network in resp: 

46 network_doi = network["doi"].strip() 

47 if network_doi: 47 ↛ 45line 47 didn't jump to line 45 because the condition on line 47 was always true

48 doi_list.append(network_doi) 

49 return doi_list 

50 

51 @staticmethod 

52 def exists_network_doi(doi: str, doi_list: list[str] | None = None) -> bool: 

53 """ 

54 Checks if the doi exists in the network FDSN repository 

55 :return: whether the DOI is in the FDSN database 

56 """ 

57 if doi_list is None: 

58 doi_list = FdsnRESTClient.get_doi_list() 

59 return doi in doi_list