44
55import os
66import re
7+ from collections .abc import Iterator
78from datetime import timedelta
89from enum import Enum
910from pathlib import Path
@@ -53,23 +54,44 @@ class ConfigModel(BaseModel):
5354 )
5455
5556
56- class _ClientCredentialsConfig (ConfigModel ):
57- type : Literal ["client-credentials" ]
57+ class Scopes (str ):
58+ def __init__ (self , scopes : str ) -> None :
59+ self ._scopes = list (scopes .split (" " ))
60+
61+ @classmethod
62+ def __get_pydantic_core_schema__ (cls , source_type : Any , handler : GetCoreSchemaHandler ) -> CoreSchema :
63+ return core_schema .no_info_after_validator_function (cls , handler (str ))
64+
65+ def __eq__ (self , other : object ) -> bool :
66+ if not isinstance (other , Scopes ):
67+ return NotImplemented
68+ return self ._scopes == other ._scopes
69+
70+ def __hash__ (self ) -> int :
71+ return hash (self ._scopes )
72+
73+ def __iter__ (self ) -> Iterator [str ]:
74+ return iter (self ._scopes )
75+
76+
77+ class BaseCredentialsConfig (ConfigModel ):
5878 client_id : str
79+ scopes : Scopes
80+
81+
82+ class _ClientCredentialsConfig (BaseCredentialsConfig ):
83+ type : Literal ["client-credentials" ]
5984 client_secret : str
6085 token_url : str
61- scopes : list [str ]
6286 resource : str | None = None
6387 audience : str | None = None
6488
6589
66- class _ClientCertificateConfig (ConfigModel ):
90+ class _ClientCertificateConfig (BaseCredentialsConfig ):
6791 type : Literal ["client-certificate" ]
68- client_id : str
6992 path : Path
7093 password : str | None = None
7194 authority_url : str
72- scopes : list [str ]
7395
7496
7597AuthenticationConfig = Annotated [_ClientCredentialsConfig | _ClientCertificateConfig , Field (discriminator = "type" )]
@@ -191,18 +213,26 @@ def __repr__(self) -> str:
191213 return self ._expression
192214
193215
194- class _ConnectionParameters (ConfigModel ):
195- gzip_compression : bool = False
196- status_forcelist : list [int ] = Field (default_factory = lambda : [429 , 502 , 503 , 504 ])
197- max_retries : int = 10
198- max_retries_connect : int = 3
199- max_retry_backoff : TimeIntervalConfig = Field (default_factory = lambda : TimeIntervalConfig ("30s" ))
200- max_connection_pool_size : int = 50
201- ssl_verify : bool = True
202- proxies : dict [str , str ] = Field (default_factory = dict )
216+ class RetriesConfig (ConfigModel ):
217+ max_retries : int = Field (default = 10 , ge = - 1 )
218+ max_backoff : TimeIntervalConfig = Field (default_factory = lambda : TimeIntervalConfig ("30s" ))
203219 timeout : TimeIntervalConfig = Field (default_factory = lambda : TimeIntervalConfig ("30s" ))
204220
205221
222+ class SslCertificatesConfig (ConfigModel ):
223+ verify : bool = True
224+ allow_list : list [str ] | None = None
225+
226+
227+ class ConnectionParameters (ConfigModel ):
228+ retries : RetriesConfig = Field (default_factory = RetriesConfig )
229+ ssl_certificates : SslCertificatesConfig = Field (default_factory = SslCertificatesConfig )
230+
231+
232+ class IntegrationConfig (ConfigModel ):
233+ external_id : str
234+
235+
206236class ConnectionConfig (ConfigModel ):
207237 """
208238 Configuration for connecting to a Cognite Data Fusion project.
@@ -216,11 +246,11 @@ class ConnectionConfig(ConfigModel):
216246 project : str
217247 base_url : str
218248
219- integration : str
249+ integration : IntegrationConfig
220250
221251 authentication : AuthenticationConfig
222252
223- connection : _ConnectionParameters = Field (default_factory = _ConnectionParameters )
253+ connection : ConnectionParameters = Field (default_factory = ConnectionParameters )
224254
225255 def get_cognite_client (self , client_name : str ) -> CogniteClient :
226256 """
@@ -235,14 +265,9 @@ def get_cognite_client(self, client_name: str) -> CogniteClient:
235265 from cognite .client .config import global_config
236266
237267 global_config .disable_pypi_version_check = True
238- global_config .disable_gzip = not self .connection .gzip_compression
239- global_config .status_forcelist = set (self .connection .status_forcelist )
240- global_config .max_retries = self .connection .max_retries
241- global_config .max_retries_connect = self .connection .max_retries_connect
242- global_config .max_retry_backoff = self .connection .max_retry_backoff .seconds
243- global_config .max_connection_pool_size = self .connection .max_connection_pool_size
244- global_config .disable_ssl = not self .connection .ssl_verify
245- global_config .proxies = self .connection .proxies
268+ global_config .max_retries = self .connection .retries .max_retries
269+ global_config .max_retry_backoff = self .connection .retries .max_backoff .seconds
270+ global_config .disable_ssl = not self .connection .ssl_certificates .verify
246271
247272 credential_provider : CredentialProvider
248273 match self .authentication :
@@ -270,7 +295,7 @@ def get_cognite_client(self, client_name: str) -> CogniteClient:
270295 client_id = client_certificate .client_id ,
271296 cert_thumbprint = str (thumbprint ),
272297 certificate = str (key ),
273- scopes = client_certificate .scopes ,
298+ scopes = list ( client_certificate .scopes ) ,
274299 )
275300
276301 case _:
@@ -280,7 +305,7 @@ def get_cognite_client(self, client_name: str) -> CogniteClient:
280305 project = self .project ,
281306 base_url = self .base_url ,
282307 client_name = client_name ,
283- timeout = self .connection .timeout .seconds ,
308+ timeout = self .connection .retries . timeout .seconds ,
284309 credentials = credential_provider ,
285310 )
286311
@@ -315,7 +340,9 @@ def from_environment(cls) -> "ConnectionConfig":
315340 client_id = os .environ ["COGNITE_CLIENT_ID" ],
316341 client_secret = os .environ ["COGNITE_CLIENT_SECRET" ],
317342 token_url = os .environ ["COGNITE_TOKEN_URL" ],
318- scopes = os .environ ["COGNITE_TOKEN_SCOPES" ].split ("," ),
343+ scopes = Scopes (
344+ os .environ ["COGNITE_TOKEN_SCOPES" ],
345+ ),
319346 )
320347 elif "COGNITE_CLIENT_CERTIFICATE_PATH" in os .environ :
321348 auth = _ClientCertificateConfig (
@@ -324,15 +351,17 @@ def from_environment(cls) -> "ConnectionConfig":
324351 path = Path (os .environ ["COGNITE_CLIENT_CERTIFICATE_PATH" ]),
325352 password = os .environ .get ("COGNITE_CLIENT_CERTIFICATE_PATH" ),
326353 authority_url = os .environ ["COGNITE_AUTHORITY_URL" ],
327- scopes = os .environ ["COGNITE_TOKEN_SCOPES" ].split ("," ),
354+ scopes = Scopes (
355+ os .environ ["COGNITE_TOKEN_SCOPES" ],
356+ ),
328357 )
329358 else :
330359 raise KeyError ("Missing auth, either COGNITE_CLIENT_SECRET or COGNITE_CLIENT_CERTIFICATE_PATH must be set" )
331360
332361 return ConnectionConfig (
333362 project = os .environ ["COGNITE_PROJECT" ],
334363 base_url = os .environ ["COGNITE_BASE_URL" ],
335- integration = os .environ ["COGNITE_INTEGRATION" ],
364+ integration = IntegrationConfig ( external_id = os .environ ["COGNITE_INTEGRATION" ]) ,
336365 authentication = auth ,
337366 )
338367
0 commit comments