11from __future__ import annotations
22
33import warnings
4- from abc import ABC
54from collections .abc import Sequence
6- from functools import cached_property
75from typing import overload
86from urllib .parse import urljoin
97
10- from cognite .client ._api_client import APIClient
118from cognite .client ._constants import DEFAULT_LIMIT_READ
9+ from cognite .client ._org_client import OrgAPIClient
1210from cognite .client .data_classes .principals import Principal , PrincipalList
13- from cognite .client .exceptions import CogniteAPIError
1411from cognite .client .utils ._identifier import PrincipalIdentifierSequence
1512from cognite .client .utils .useful_types import SequenceNotStr
1613
1714
18- class OrgAPI (APIClient , ABC ):
19- _auth_url = "https://auth.cognite.com"
20-
21- def _get_base_url_with_base_path (self ) -> str :
22- """Get base URL with base path including organization and api version if applicable"""
23- base_path = ""
24- if self ._api_version :
25- base_path = f"/api/{ self ._api_version } /orgs/{ self ._organization } "
26- # The OrganizationAPi uses the auth_url as the base for these endpoints instead of the
27- # base_url like the rest of the SDK.
28- return urljoin (self ._auth_url , base_path )
29-
30- @cached_property
31- def _organization (self ) -> str :
32- headers = self ._configure_headers (
33- "application/json" ,
34- additional_headers = self ._config .headers .copy (),
35- api_subversion = self ._api_subversion ,
36- )
37- # This is an internal endpoint, not part of the public API
38- full_url = urljoin (self ._config .base_url , f"/api/v1/projects/{ self ._config .project } " )
39- response = self ._http_client_with_retry .request (method = "GET" , url = full_url , headers = headers )
40- if response .status_code != 200 :
41- raise CogniteAPIError (
42- "Could not look-up organization" , response .status_code , response .headers .get ("x-request-id" )
43- )
44- return response .json ()["organization" ]
45-
46-
47- class PrincipalsAPI (OrgAPI ):
15+ class PrincipalsAPI (OrgAPIClient ):
4816 _RESOURCE_PATH = "/principals"
4917
5018 def me (self ) -> Principal :
@@ -61,9 +29,10 @@ def me(self) -> Principal:
6129 >>> res = client.iam.principals.me()
6230 """
6331 # the /me endpoint is not using the /orgs/{org} base path, so we have to construct the URL manually
64- path = "/principals /me"
32+ path = f" { self . _RESOURCE_PATH } /me"
6533 if self ._api_version :
6634 path = f"/api/{ self ._api_version } { path } "
35+
6736 full_url = urljoin (self ._auth_url , path )
6837 headers = self ._configure_headers (
6938 "application/json" ,
0 commit comments