def __init__( self, api_key: str = None, project: str = None, client_name: str = None, base_url: str = None, max_workers: int = None, headers: Dict[str, str] = None, timeout: int = None, token: Union[str, Callable[[], str], None] = None, disable_pypi_version_check: Optional[bool] = None, debug: bool = False, ): self._config = ClientConfig( api_key=api_key, project=project, client_name=client_name, base_url=base_url, max_workers=max_workers, headers=headers, timeout=timeout, token=token, disable_pypi_version_check=disable_pypi_version_check, debug=debug, ) self.login = LoginAPI(self._config, cognite_client=self) if self._config.project is None: self._config.project = self._infer_project() self.assets = AssetsAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.datapoints = DatapointsAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.events = EventsAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.files = FilesAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.iam = IAMAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.data_sets = DataSetsAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.sequences = SequencesAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.time_series = TimeSeriesAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.raw = RawAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.three_d = ThreeDAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self._api_client = APIClient(self._config, cognite_client=self)
def api_client(): client = APIClient( project="test_proj", base_url="http://localtest.com/api/0.5/projects/test_proj", num_of_workers=1, cookies={"a-cookie": "a-cookie-val"}, headers={}, timeout=60, ) yield client
def api_client(): session = Session() retry = Retry(total=0, read=0, connect=0, backoff_factor=1, status_forcelist=STATUS_FORCELIST) adapter = HTTPAdapter(max_retries=retry) session.mount("http://", adapter) session.mount("https://", adapter) client = APIClient( request_session=session, project="test_proj", base_url="http://localtest.com/api/0.5/projects/test_proj", num_of_workers=1, cookies={"a-cookie": "a-cookie-val"}, headers={}, timeout=60, ) yield client
class CogniteClient: """Main entrypoint into Cognite Python SDK. All services are made available through this object. See examples below. Args: api_key (str): API key project (str): Project. Defaults to project of given API key. client_name (str): A user-defined name for the client. Used to identify number of unique applications/scripts running on top of CDF. base_url (str): Base url to send requests to. Defaults to "https://api.cognitedata.com" max_workers (int): Max number of workers to spawn when parallelizing data fetching. Defaults to 10. headers (Dict): Additional headers to add to all requests. timeout (int): Timeout on requests sent to the api. Defaults to 30 seconds. token (Union[str, Callable[[], str]]): A jwt or method which takes no arguments and returns a jwt to use for authentication. This will override any api-key set. disable_pypi_version_check (bool): Don't check for newer versions of the SDK on client creation debug (bool): Configures logger to log extra request details to stderr. """ _API_VERSION = "v1" def __init__( self, api_key: str = None, project: str = None, client_name: str = None, base_url: str = None, max_workers: int = None, headers: Dict[str, str] = None, timeout: int = None, token: Union[str, Callable[[], str], None] = None, disable_pypi_version_check: Optional[bool] = None, debug: bool = False, ): self._config = ClientConfig( api_key=api_key, project=project, client_name=client_name, base_url=base_url, max_workers=max_workers, headers=headers, timeout=timeout, token=token, disable_pypi_version_check=disable_pypi_version_check, debug=debug, ) self.login = LoginAPI(self._config, cognite_client=self) if self._config.project is None: self._config.project = self._infer_project() self.assets = AssetsAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.datapoints = DatapointsAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.events = EventsAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.files = FilesAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.iam = IAMAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.data_sets = DataSetsAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.sequences = SequencesAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.time_series = TimeSeriesAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.raw = RawAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self.three_d = ThreeDAPI(self._config, api_version=self._API_VERSION, cognite_client=self) self._api_client = APIClient(self._config, cognite_client=self) def get(self, url: str, params: Dict[str, Any] = None, headers: Dict[str, Any] = None): """Perform a GET request to an arbitrary path in the API.""" return self._api_client._get(url, params=params, headers=headers) def post(self, url: str, json: Dict[str, Any], params: Dict[str, Any] = None, headers: Dict[str, Any] = None): """Perform a POST request to an arbitrary path in the API.""" return self._api_client._post(url, json=json, params=params, headers=headers) def put(self, url: str, json: Dict[str, Any] = None, headers: Dict[str, Any] = None): """Perform a PUT request to an arbitrary path in the API.""" return self._api_client._put(url, json=json, headers=headers) def delete(self, url: str, params: Dict[str, Any] = None, headers: Dict[str, Any] = None): """Perform a DELETE request to an arbitrary path in the API.""" return self._api_client._delete(url, params=params, headers=headers) @property def version(self) -> str: """Returns the current SDK version. Returns: str: The current SDK version """ return utils._auxiliary.get_current_sdk_version() @property def config(self) -> ClientConfig: """Returns a config object containing the configuration for the current client. Returns: ClientConfig: The configuration object. """ return self._config def _infer_project(self): login_status = self.login.status() if login_status.logged_in: warnings.warn( "Authenticated towards inferred project '{}'. Pass project to the CogniteClient constructor or set" " the environment variable 'COGNITE_PROJECT' to suppress this warning." .format(login_status.project), stacklevel=3, ) return login_status.project else: raise CogniteAPIKeyError("Invalid API key")
def test_sanitize_headers(self, before, after): assert before != after APIClient._sanitize_headers(before) assert before == after
from cognite.client._api_client import APIClient from cognite.client.data_classes._base import * from cognite.client.exceptions import CogniteAPIError, CogniteNotFoundError from cognite.client.utils._client_config import ClientConfig from tests.utils import jsgz_load, set_request_limit BASE_URL = "http://localtest.com/api/1.0/projects/test-project" URL_PATH = "/someurl" RESPONSE = {"any": "ok"} COGNITE_CLIENT = CogniteClient() API_CLIENT_WITH_API_KEY = APIClient( ClientConfig( project="test-project", api_key="abc", base_url=BASE_URL, max_workers=1, headers={"x-cdp-app": "python-sdk-integration-tests"}, ), cognite_client=COGNITE_CLIENT, ) API_CLIENT_WITH_TOKEN_FACTORY = APIClient( ClientConfig( project="test-project", base_url=BASE_URL, max_workers=1, headers={"x-cdp-app": "python-sdk-integration-tests"}, token=lambda: "abc", ), cognite_client=COGNITE_CLIENT, )