enpi_api.l2.client.enpi_api_client
1import os 2import sys 3from types import TracebackType 4from typing import Self, Type 5 6from loguru import logger 7 8from enpi_api.l1 import openapi_client 9from enpi_api.l2.client.api.basket_api import BasketApi 10from enpi_api.l2.client.api.cluster_api import ClusterApi 11from enpi_api.l2.client.api.collection_api import CollectionApi 12from enpi_api.l2.client.api.enrichment_api import EnrichmentApi 13from enpi_api.l2.client.api.file_api import FileApi 14from enpi_api.l2.client.api.filter_api import FilterApi 15from enpi_api.l2.client.api.liabilities_api import LiabilitiesApi 16from enpi_api.l2.client.api.ml_api import MlApi 17from enpi_api.l2.client.api.phylogeny_api import PhylogenyApi 18from enpi_api.l2.client.api.reference_database_api import ReferenceDatabaseApi 19from enpi_api.l2.client.api.sequence_annotation_api import SequenceAnnotationApi 20from enpi_api.l2.client.api.tag_api import TagApi 21from enpi_api.l2.client.api.whoami_api import WhoamiApi 22from enpi_api.l2.client.api.workflow_api import WorkflowApi 23from enpi_api.l2.types.log import LogLevel 24from enpi_api.l2.util.client import get_configuration 25from enpi_api.l2.util.env import env_or_raise, get_log_level 26 27 28def configure_logger(level: LogLevel) -> None: 29 """@private""" 30 logger.remove() 31 32 logger.add( 33 sys.stdout, 34 colorize=True, 35 level=level, 36 ) 37 38 39class EnpiApiClient: 40 """The higher level API client to comfortably interact with the ENPICOM API. 41 42 This class is a context manager, so it should be used with the `with` statement. 43 44 By default, arguments are taken from the environment variables. 45 46 Key is a required argument or the environment variable `ENPI_API_KEY` must be set. 47 48 """ 49 50 _configuration: openapi_client.Configuration 51 _log_level: LogLevel 52 _extra_headers: dict[str, str] | None = None 53 _inner_api_client: openapi_client.ApiClient 54 55 basket_api: BasketApi 56 cluster_api: ClusterApi 57 collection_api: CollectionApi 58 file_api: FileApi 59 filter_api: FilterApi 60 liabilities_api: LiabilitiesApi 61 ml_api: MlApi 62 sequence_annotation_api: SequenceAnnotationApi 63 reference_database_api: ReferenceDatabaseApi 64 tag_api: TagApi 65 enrichment_api: EnrichmentApi 66 phylogeny_api: PhylogenyApi 67 whoami_api: WhoamiApi 68 workflow_api: WorkflowApi 69 70 def __init__(self, api_key: str | None = None, log_level: LogLevel = get_log_level()): 71 """Initialize the API client. 72 73 Args: 74 api_key (str | None): The API key to use. If left blank the API key will be retrieved from the 75 `ENPI_API_KEY` environment variable. 76 log_level (LogLevel): The log level to use. Defaults to `DEBUG`. 77 78 Raises: 79 enpi_api.l2.client.enpi_api_client.ApiHostNotSet: When `ENPI_API_HOST` env variable is not set. 80 enpi_api.l2.client.enpi_api_client.ApiKeyNotSet: When `ENPI_API_KEY` env variable is not set or passed as an argument. 81 82 Example: 83 84 ```python 85 from enpi_api.l2.client.enpi_api_client import EnpiApiClient 86 87 with EnpiApiClient() as enpi_client: 88 # Use the client here, for example, access the collection api 89 collection_api = enpi_client.collection_api 90 ``` 91 """ 92 93 configure_logger(log_level) 94 self._log_level = log_level 95 self._configuration = get_configuration(api_key) 96 97 # If API_KEY was passed as a value, we write it to the environment variable so some of the internal functions 98 # (namely `get_client()`) could pick it up later. Also, this way the key passed overrides the key passed in an 99 # environment variable. Otherwise we assume the key is already present in that variable 100 if api_key is not None and api_key != "": 101 os.environ["ENPI_API_KEY"] = api_key 102 103 def _add_extra_headers(self, headers: dict[str, str]) -> Self: 104 self._extra_headers = headers if self._extra_headers is None else {**self._extra_headers, **headers} 105 return self 106 107 def __enter__(self) -> Self: 108 self._inner_api_client = openapi_client.ApiClient(self._configuration) 109 110 if os.environ.get("USER_ID") and os.environ.get("USER_ORG_ID") and os.environ.get("USER_DEFAULT_SPACE_ID"): 111 self._add_extra_headers( 112 { 113 env_or_raise("PUBLIC_API_USER_ID_HEADER"): str(os.environ.get("USER_ID")), 114 env_or_raise("PUBLIC_API_USER_ORG_HEADER"): str(os.environ.get("USER_ORG_ID")), 115 env_or_raise("PUBLIC_API_USER_DEFAULT_SPACE_HEADER"): str(os.environ.get("USER_DEFAULT_SPACE_ID")), 116 } 117 ) 118 119 if self._extra_headers: 120 for header, value in self._extra_headers.items(): 121 logger.trace(f"Setting header {header} to {value}") 122 self._inner_api_client.set_default_header(header, value) 123 124 self.basket_api = BasketApi(self._inner_api_client, self._log_level) 125 self.cluster_api = ClusterApi(self._inner_api_client, self._log_level) 126 self.collection_api = CollectionApi(self._inner_api_client, self._log_level) 127 self.file_api = FileApi(self._inner_api_client, self._log_level) 128 self.filter_api = FilterApi(self._inner_api_client, self._log_level) 129 self.liabilities_api = LiabilitiesApi(self._inner_api_client, self._log_level) 130 self.ml_api = MlApi(self._inner_api_client, self._log_level) 131 self.sequence_annotation_api = SequenceAnnotationApi(self._inner_api_client, self._log_level) 132 self.reference_database_api = ReferenceDatabaseApi(self._inner_api_client, self._log_level) 133 self.tag_api = TagApi(self._inner_api_client, self._log_level) 134 self.enrichment_api = EnrichmentApi(self._inner_api_client, self._log_level) 135 self.phylogeny_api = PhylogenyApi(self._inner_api_client, self._log_level) 136 self.whoami_api = WhoamiApi(self._inner_api_client, self._log_level) 137 self.workflow_api = WorkflowApi(self._inner_api_client, self._log_level) 138 139 return self 140 141 def __exit__(self, exc_type: Type[BaseException] | None, exc: BaseException | None, traceback: TracebackType | None) -> None: 142 self._inner_api_client.__exit__(exc_type, exc, traceback)
class
EnpiApiClient:
40class EnpiApiClient: 41 """The higher level API client to comfortably interact with the ENPICOM API. 42 43 This class is a context manager, so it should be used with the `with` statement. 44 45 By default, arguments are taken from the environment variables. 46 47 Key is a required argument or the environment variable `ENPI_API_KEY` must be set. 48 49 """ 50 51 _configuration: openapi_client.Configuration 52 _log_level: LogLevel 53 _extra_headers: dict[str, str] | None = None 54 _inner_api_client: openapi_client.ApiClient 55 56 basket_api: BasketApi 57 cluster_api: ClusterApi 58 collection_api: CollectionApi 59 file_api: FileApi 60 filter_api: FilterApi 61 liabilities_api: LiabilitiesApi 62 ml_api: MlApi 63 sequence_annotation_api: SequenceAnnotationApi 64 reference_database_api: ReferenceDatabaseApi 65 tag_api: TagApi 66 enrichment_api: EnrichmentApi 67 phylogeny_api: PhylogenyApi 68 whoami_api: WhoamiApi 69 workflow_api: WorkflowApi 70 71 def __init__(self, api_key: str | None = None, log_level: LogLevel = get_log_level()): 72 """Initialize the API client. 73 74 Args: 75 api_key (str | None): The API key to use. If left blank the API key will be retrieved from the 76 `ENPI_API_KEY` environment variable. 77 log_level (LogLevel): The log level to use. Defaults to `DEBUG`. 78 79 Raises: 80 enpi_api.l2.client.enpi_api_client.ApiHostNotSet: When `ENPI_API_HOST` env variable is not set. 81 enpi_api.l2.client.enpi_api_client.ApiKeyNotSet: When `ENPI_API_KEY` env variable is not set or passed as an argument. 82 83 Example: 84 85 ```python 86 from enpi_api.l2.client.enpi_api_client import EnpiApiClient 87 88 with EnpiApiClient() as enpi_client: 89 # Use the client here, for example, access the collection api 90 collection_api = enpi_client.collection_api 91 ``` 92 """ 93 94 configure_logger(log_level) 95 self._log_level = log_level 96 self._configuration = get_configuration(api_key) 97 98 # If API_KEY was passed as a value, we write it to the environment variable so some of the internal functions 99 # (namely `get_client()`) could pick it up later. Also, this way the key passed overrides the key passed in an 100 # environment variable. Otherwise we assume the key is already present in that variable 101 if api_key is not None and api_key != "": 102 os.environ["ENPI_API_KEY"] = api_key 103 104 def _add_extra_headers(self, headers: dict[str, str]) -> Self: 105 self._extra_headers = headers if self._extra_headers is None else {**self._extra_headers, **headers} 106 return self 107 108 def __enter__(self) -> Self: 109 self._inner_api_client = openapi_client.ApiClient(self._configuration) 110 111 if os.environ.get("USER_ID") and os.environ.get("USER_ORG_ID") and os.environ.get("USER_DEFAULT_SPACE_ID"): 112 self._add_extra_headers( 113 { 114 env_or_raise("PUBLIC_API_USER_ID_HEADER"): str(os.environ.get("USER_ID")), 115 env_or_raise("PUBLIC_API_USER_ORG_HEADER"): str(os.environ.get("USER_ORG_ID")), 116 env_or_raise("PUBLIC_API_USER_DEFAULT_SPACE_HEADER"): str(os.environ.get("USER_DEFAULT_SPACE_ID")), 117 } 118 ) 119 120 if self._extra_headers: 121 for header, value in self._extra_headers.items(): 122 logger.trace(f"Setting header {header} to {value}") 123 self._inner_api_client.set_default_header(header, value) 124 125 self.basket_api = BasketApi(self._inner_api_client, self._log_level) 126 self.cluster_api = ClusterApi(self._inner_api_client, self._log_level) 127 self.collection_api = CollectionApi(self._inner_api_client, self._log_level) 128 self.file_api = FileApi(self._inner_api_client, self._log_level) 129 self.filter_api = FilterApi(self._inner_api_client, self._log_level) 130 self.liabilities_api = LiabilitiesApi(self._inner_api_client, self._log_level) 131 self.ml_api = MlApi(self._inner_api_client, self._log_level) 132 self.sequence_annotation_api = SequenceAnnotationApi(self._inner_api_client, self._log_level) 133 self.reference_database_api = ReferenceDatabaseApi(self._inner_api_client, self._log_level) 134 self.tag_api = TagApi(self._inner_api_client, self._log_level) 135 self.enrichment_api = EnrichmentApi(self._inner_api_client, self._log_level) 136 self.phylogeny_api = PhylogenyApi(self._inner_api_client, self._log_level) 137 self.whoami_api = WhoamiApi(self._inner_api_client, self._log_level) 138 self.workflow_api = WorkflowApi(self._inner_api_client, self._log_level) 139 140 return self 141 142 def __exit__(self, exc_type: Type[BaseException] | None, exc: BaseException | None, traceback: TracebackType | None) -> None: 143 self._inner_api_client.__exit__(exc_type, exc, traceback)
The higher level API client to comfortably interact with the ENPICOM API.
This class is a context manager, so it should be used with the with statement.
By default, arguments are taken from the environment variables.
Key is a required argument or the environment variable ENPI_API_KEY must be set.
EnpiApiClient( api_key: str | None = None, log_level: enpi_api.l2.types.log.LogLevel = <LogLevel.Debug: 'DEBUG'>)
71 def __init__(self, api_key: str | None = None, log_level: LogLevel = get_log_level()): 72 """Initialize the API client. 73 74 Args: 75 api_key (str | None): The API key to use. If left blank the API key will be retrieved from the 76 `ENPI_API_KEY` environment variable. 77 log_level (LogLevel): The log level to use. Defaults to `DEBUG`. 78 79 Raises: 80 enpi_api.l2.client.enpi_api_client.ApiHostNotSet: When `ENPI_API_HOST` env variable is not set. 81 enpi_api.l2.client.enpi_api_client.ApiKeyNotSet: When `ENPI_API_KEY` env variable is not set or passed as an argument. 82 83 Example: 84 85 ```python 86 from enpi_api.l2.client.enpi_api_client import EnpiApiClient 87 88 with EnpiApiClient() as enpi_client: 89 # Use the client here, for example, access the collection api 90 collection_api = enpi_client.collection_api 91 ``` 92 """ 93 94 configure_logger(log_level) 95 self._log_level = log_level 96 self._configuration = get_configuration(api_key) 97 98 # If API_KEY was passed as a value, we write it to the environment variable so some of the internal functions 99 # (namely `get_client()`) could pick it up later. Also, this way the key passed overrides the key passed in an 100 # environment variable. Otherwise we assume the key is already present in that variable 101 if api_key is not None and api_key != "": 102 os.environ["ENPI_API_KEY"] = api_key
Initialize the API client.
Arguments:
- api_key (str | None): The API key to use. If left blank the API key will be retrieved from the
ENPI_API_KEYenvironment variable. - log_level (LogLevel): The log level to use. Defaults to
DEBUG.
Raises:
- enpi_api.l2.client.enpi_api_client.ApiHostNotSet: When
ENPI_API_HOSTenv variable is not set. - enpi_api.l2.client.enpi_api_client.ApiKeyNotSet: When
ENPI_API_KEYenv variable is not set or passed as an argument.
Example:
from enpi_api.l2.client.enpi_api_client import EnpiApiClient with EnpiApiClient() as enpi_client: # Use the client here, for example, access the collection api collection_api = enpi_client.collection_api
basket_api: enpi_api.l2.client.api.basket_api.BasketApi
cluster_api: enpi_api.l2.client.api.cluster_api.ClusterApi
collection_api: enpi_api.l2.client.api.collection_api.CollectionApi
file_api: enpi_api.l2.client.api.file_api.FileApi
filter_api: enpi_api.l2.client.api.filter_api.FilterApi
liabilities_api: enpi_api.l2.client.api.liabilities_api.LiabilitiesApi
sequence_annotation_api: enpi_api.l2.client.api.sequence_annotation_api.SequenceAnnotationApi
reference_database_api: enpi_api.l2.client.api.reference_database_api.ReferenceDatabaseApi
enrichment_api: enpi_api.l2.client.api.enrichment_api.EnrichmentApi
phylogeny_api: enpi_api.l2.client.api.phylogeny_api.PhylogenyApi
whoami_api: enpi_api.l2.client.api.whoami_api.WhoamiApi
workflow_api: enpi_api.l2.client.api.workflow_api.WorkflowApi