1
1
import logging
2
+ import os
2
3
from abc import abstractmethod
3
4
from json import JSONDecodeError
4
- from typing import Union , Any , Optional , Dict , List , Generator
5
+ from typing import Any , Optional , Dict , List , Generator
5
6
6
7
import httpx
7
8
from httpx import Request , Response , Auth , HTTPTransport , AsyncHTTPTransport
11
12
ConfigAsyncEndpoint , InstanceAsyncEndpoint
12
13
)
13
14
from .exception import HTTPResponseError
14
- from .typings import SyncAsync
15
+ from .typings import SyncAsync , HttpxClient
15
16
16
17
logger = logging .getLogger (__name__ )
17
18
@@ -23,18 +24,18 @@ class BaseClient:
23
24
24
25
def __init__ (
25
26
self ,
26
- client : Union [ httpx . Client , httpx . AsyncClient ] ,
27
+ client : HttpxClient ,
27
28
server_addr : Optional [str ] = None ,
28
29
username : Optional [str ] = None ,
29
30
password : Optional [str ] = None ,
30
31
namespace_id : Optional [str ] = None ,
31
32
):
32
- self .server_addr = server_addr or DEFAULT_SERVER_ADDR
33
- self .username = username
34
- self .password = password
35
- self .namespace_id = namespace_id or DEFAULT_NAMESPACE
33
+ self .server_addr = server_addr or os . environ . get ( "NACOS_SERVER_ADDR" ) or DEFAULT_SERVER_ADDR
34
+ self .username = username or os . environ . get ( "NACOS_USERNAME" )
35
+ self .password = password or os . environ . get ( "NACOS_PASSWORD" )
36
+ self .namespace_id = namespace_id or os . environ . get ( "NACOS_NAMESPACE" ) or DEFAULT_NAMESPACE
36
37
37
- self ._clients : List [Union [ httpx . Client , httpx . AsyncClient ] ] = []
38
+ self ._clients : List [HttpxClient ] = []
38
39
self .client = client
39
40
# endpoints
40
41
self .config = ConfigEndpoint (self )
@@ -43,11 +44,11 @@ def __init__(
43
44
self .namespace = NamespaceEndpoint (self )
44
45
45
46
@property
46
- def client (self ) -> Union [ httpx . Client , httpx . AsyncClient ] :
47
+ def client (self ) -> HttpxClient :
47
48
return self ._clients [- 1 ]
48
49
49
50
@client .setter
50
- def client (self , client : Union [ httpx . Client , httpx . AsyncClient ] ) -> None :
51
+ def client (self , client : HttpxClient ) -> None :
51
52
client .base_url = httpx .URL (self .server_addr )
52
53
client .timeout = httpx .Timeout (timeout = 60_000 / 1_000 )
53
54
client .headers = httpx .Headers (
@@ -112,7 +113,11 @@ def __init__(
112
113
""" Nacos Sync Client """
113
114
client = client or httpx .Client (transport = HTTPTransport (retries = http_retries ))
114
115
super ().__init__ (
115
- client , server_addr , username , password , namespace_id
116
+ client = client ,
117
+ server_addr = server_addr ,
118
+ username = username ,
119
+ password = password ,
120
+ namespace_id = namespace_id
116
121
)
117
122
118
123
def request (
@@ -152,7 +157,13 @@ def __init__(
152
157
):
153
158
""" Nacos Async Client """
154
159
client = client or httpx .AsyncClient (transport = AsyncHTTPTransport (retries = http_retries ))
155
- super ().__init__ (client , server_addr , username , password , namespace_id )
160
+ super ().__init__ (
161
+ client = client ,
162
+ server_addr = server_addr ,
163
+ username = username ,
164
+ password = password ,
165
+ namespace_id = namespace_id
166
+ )
156
167
self .config = ConfigAsyncEndpoint (self )
157
168
self .instance = InstanceAsyncEndpoint (self )
158
169
0 commit comments