Skip to content

Commit 1bf60d0

Browse files
authored
optimized code (#3)
* optimized code
1 parent a73245d commit 1bf60d0

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
os: [ "ubuntu-latest" ]
1919
python-version: [ "3.8" ]
2020
runs-on: ${{ matrix.os }}
21+
environment: test
2122
steps:
2223
- uses: actions/checkout@v2
2324
- name: Set up Python ${{ matrix.python-version }}
@@ -46,4 +47,6 @@ jobs:
4647
source .venv/bin/activate
4748
pytest tests/
4849
env:
49-
SERVER_ADDR: ${{ secrets.SERVER_ADDR }}
50+
NACOS_SERVER_ADDR: ${{ secrets.NACOS_SERVER_ADDR }}
51+
NACOS_USERNAME: ${{ secrets.NACOS_USERNAME }}
52+
NACOS_PASSWORD: ${{ secrets.NACOS_PASSWORD }}

src/use_nacos/client.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import logging
2+
import os
23
from abc import abstractmethod
34
from json import JSONDecodeError
4-
from typing import Union, Any, Optional, Dict, List, Generator
5+
from typing import Any, Optional, Dict, List, Generator
56

67
import httpx
78
from httpx import Request, Response, Auth, HTTPTransport, AsyncHTTPTransport
@@ -11,7 +12,7 @@
1112
ConfigAsyncEndpoint, InstanceAsyncEndpoint
1213
)
1314
from .exception import HTTPResponseError
14-
from .typings import SyncAsync
15+
from .typings import SyncAsync, HttpxClient
1516

1617
logger = logging.getLogger(__name__)
1718

@@ -23,18 +24,18 @@ class BaseClient:
2324

2425
def __init__(
2526
self,
26-
client: Union[httpx.Client, httpx.AsyncClient],
27+
client: HttpxClient,
2728
server_addr: Optional[str] = None,
2829
username: Optional[str] = None,
2930
password: Optional[str] = None,
3031
namespace_id: Optional[str] = None,
3132
):
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
3637

37-
self._clients: List[Union[httpx.Client, httpx.AsyncClient]] = []
38+
self._clients: List[HttpxClient] = []
3839
self.client = client
3940
# endpoints
4041
self.config = ConfigEndpoint(self)
@@ -43,11 +44,11 @@ def __init__(
4344
self.namespace = NamespaceEndpoint(self)
4445

4546
@property
46-
def client(self) -> Union[httpx.Client, httpx.AsyncClient]:
47+
def client(self) -> HttpxClient:
4748
return self._clients[-1]
4849

4950
@client.setter
50-
def client(self, client: Union[httpx.Client, httpx.AsyncClient]) -> None:
51+
def client(self, client: HttpxClient) -> None:
5152
client.base_url = httpx.URL(self.server_addr)
5253
client.timeout = httpx.Timeout(timeout=60_000 / 1_000)
5354
client.headers = httpx.Headers(
@@ -112,7 +113,11 @@ def __init__(
112113
""" Nacos Sync Client """
113114
client = client or httpx.Client(transport=HTTPTransport(retries=http_retries))
114115
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
116121
)
117122

118123
def request(
@@ -152,7 +157,13 @@ def __init__(
152157
):
153158
""" Nacos Async Client """
154159
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+
)
156167
self.config = ConfigAsyncEndpoint(self)
157168
self.instance = InstanceAsyncEndpoint(self)
158169

src/use_nacos/typings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from typing import Awaitable, Union, TypeVar, TypedDict, Optional
22

3+
import httpx
4+
35
T = TypeVar("T")
46
SyncAsync = Union[T, Awaitable[T]]
7+
HttpxClient = Union[httpx.Client, httpx.AsyncClient]
58

69

710
class BeatType(TypedDict):

tests/test_config.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import os
32
import random
43
import time
54

@@ -12,17 +11,15 @@
1211
from use_nacos.exception import HTTPResponseError
1312
from use_nacos.serializer import JsonSerializer, AutoSerializer, YamlSerializer, TomlSerializer
1413

15-
server_addr = os.environ.get('SERVER_ADDR')
16-
1714

1815
@pytest.fixture
1916
def client():
20-
return NacosClient(server_addr=server_addr, username="nacos", password="nacos")
17+
return NacosClient()
2118

2219

2320
@pytest.fixture
2421
def async_client():
25-
return NacosAsyncClient(server_addr=server_addr, username="nacos", password="nacos")
22+
return NacosAsyncClient()
2623

2724

2825
@pytest.fixture

0 commit comments

Comments
 (0)