1
1
from importlib .metadata import version
2
+ from typing import Optional
2
3
4
+ import httpx
3
5
from kiota_abstractions .authentication import AuthenticationProvider
4
6
from kiota_abstractions .authentication .access_token_provider import AccessTokenProvider
5
7
from kiota_abstractions .authentication .anonymous_authentication_provider import AnonymousAuthenticationProvider
6
8
from kiota_abstractions .authentication .base_bearer_token_authentication_provider import (
7
9
BaseBearerTokenAuthenticationProvider )
8
10
from kiota_http .httpx_request_adapter import HttpxRequestAdapter
11
+ from kiota_http .kiota_client_factory import KiotaClientFactory
9
12
13
+ from .configs import HorreumCredentials , ClientConfiguration
10
14
from .keycloak_access_provider import KeycloakAccessProvider
11
15
from .raw_client .horreum_raw_client import HorreumRawClient
12
16
17
+ DEFAULT_CONNECTION_TIMEOUT : int = 30
18
+ DEFAULT_REQUEST_TIMEOUT : int = 100
19
+
13
20
14
21
async def setup_auth_provider (base_url : str , username : str , password : str ) -> AccessTokenProvider :
15
22
# Use not authenticated client to fetch the auth mechanism
@@ -25,34 +32,48 @@ async def setup_auth_provider(base_url: str, username: str, password: str) -> Ac
25
32
26
33
class HorreumClient :
27
34
__base_url : str
28
- __username : str
29
- __password : str
35
+ __credentials : Optional [HorreumCredentials ]
36
+ __client_config : Optional [ClientConfiguration ]
37
+ __http_client : httpx .AsyncClient
30
38
31
39
# Raw client, this could be used to interact with the low-level api
32
40
raw_client : HorreumRawClient
33
- auth_provider : AuthenticationProvider
41
+ # By default, set as anonymous authentication
42
+ auth_provider : AuthenticationProvider = AnonymousAuthenticationProvider ()
34
43
35
- def __init__ (self , base_url : str , username : str = None , password : str = None ):
44
+ def __init__ (self , base_url : str , credentials : Optional [HorreumCredentials ],
45
+ client_config : Optional [ClientConfiguration ]):
36
46
self .__base_url = base_url
37
- self .__username = username
38
- self .__password = password
47
+ self .__credentials = credentials
48
+ self .__client_config = client_config
49
+
50
+ if client_config and client_config .http_client and client_config .use_default_middlewares :
51
+ self .__http_client = KiotaClientFactory .create_with_default_middleware (client = client_config .http_client ,
52
+ options = client_config .options )
53
+ else :
54
+ self .__http_client = client_config .http_client if client_config else None
39
55
40
56
async def setup (self ):
41
57
"""
42
58
Set up the authentication provider, based on the Horreum configuration, and the low-level horreum api client
43
59
"""
44
60
45
- if self .__username is not None :
46
- # Bearer token authentication
47
- access_provider = await setup_auth_provider (self .__base_url , self .__username , self .__password )
48
- self .auth_provider = BaseBearerTokenAuthenticationProvider (access_provider )
49
- elif self .__password is not None :
50
- raise RuntimeError ("providing password without username, have you missed something?" )
61
+ if self .__credentials :
62
+ if self .__credentials .username is not None :
63
+ # Bearer token authentication
64
+ access_provider = await setup_auth_provider (self .__base_url , self .__credentials .username ,
65
+ self .__credentials .password )
66
+ self .auth_provider = BaseBearerTokenAuthenticationProvider (access_provider )
67
+ elif self .__credentials .password is not None :
68
+ raise RuntimeError ("providing password without username, have you missed something?" )
69
+
70
+ if self .__http_client :
71
+ req_adapter = HttpxRequestAdapter (authentication_provider = self .auth_provider ,
72
+ http_client = self .__http_client )
51
73
else :
52
- # Anonymous authentication
53
- self . auth_provider = AnonymousAuthenticationProvider ( )
74
+ # rely on the Kiota default is not provided by user
75
+ req_adapter = HttpxRequestAdapter ( authentication_provider = self . auth_provider )
54
76
55
- req_adapter = HttpxRequestAdapter (self .auth_provider )
56
77
req_adapter .base_url = self .__base_url
57
78
58
79
self .raw_client = HorreumRawClient (req_adapter )
@@ -66,15 +87,16 @@ def version() -> str:
66
87
return version ("horreum" )
67
88
68
89
69
- async def new_horreum_client (base_url : str , username : str = None , password : str = None ) -> HorreumClient :
90
+ async def new_horreum_client (base_url : str , credentials : Optional [HorreumCredentials ] = None ,
91
+ client_config : Optional [ClientConfiguration ] = None ) -> HorreumClient :
70
92
"""
71
93
Initialize the horreum client
72
94
:param base_url: horreum api base url
73
- :param username: auth username
74
- :param password: auth password
95
+ :param credentials: horreum credentials in the form of username and pwd
96
+ :param client_config: inner http client configuration
75
97
:return: HorreumClient instance
76
98
"""
77
- client = HorreumClient (base_url , username , password )
99
+ client = HorreumClient (base_url , credentials = credentials , client_config = client_config )
78
100
await client .setup ()
79
101
80
102
return client
0 commit comments