Skip to content

Commit 3029252

Browse files
committed
add endpoint provider
1 parent 7381bb4 commit 3029252

File tree

5 files changed

+66
-32
lines changed

5 files changed

+66
-32
lines changed

alibabacloud_oss_v2/_client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
BodyType,
3030
OperationInput,
3131
OperationOutput,
32+
EndpointProvider,
3233
)
3334

3435

@@ -110,6 +111,7 @@ def __init__(
110111
feature_flags: Optional[int] = None,
111112
additional_headers: Optional[List[str]] = None,
112113
operation_timeout: Optional[Union[int, float]] = None,
114+
endpoint_provider: Optional[EndpointProvider] = None,
113115
) -> None:
114116
self.product = product
115117
self.region = region
@@ -127,7 +129,7 @@ def __init__(
127129
self.feature_flags = feature_flags or defaults.FF_DEFAULT
128130
self.additional_headers = additional_headers
129131
self.operation_timeout = operation_timeout
130-
132+
self.endpoint_provider = endpoint_provider
131133

132134
class _InnerOptions:
133135
"""client runtime's information."""
@@ -219,7 +221,10 @@ def build_request_context(self, op_input: OperationInput, options: _Options, inn
219221
"""build request context
220222
"""
221223
# host & path
222-
url = _build_url(op_input, options)
224+
if options.endpoint_provider is not None:
225+
url = options.endpoint_provider.build_url(op_input)
226+
else:
227+
url = _build_url(op_input, options)
223228

224229
# queries
225230
if op_input.parameters is not None:

alibabacloud_oss_v2/endpoints.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@ def from_region(region:str, disable_ssl:bool, etype:str) -> str:
3737

3838
return endpoint
3939

40-
41-
def vectors_from_region(region: str, disable_ssl: bool, etype: str) -> str:
42-
"""Generate vectors endpoint from region"""
43-
scheme = "https"
44-
if disable_ssl:
45-
scheme = "http"
46-
47-
if etype == "internal":
48-
endpoint = f"oss-{region}-internal.oss-vectors.aliyuncs.com"
49-
else:
50-
endpoint = f"oss-{region}.oss-vectors.aliyuncs.com"
51-
52-
return f"{scheme}://{endpoint}"
53-
5440
def is_ip(hostname:str):
5541
"""Check whether the host name is an IP address."""
5642
is_ipv6 = False

alibabacloud_oss_v2/types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,11 @@ def iter_bytes(self, **kwargs: Any) -> Iterator[bytes]:
450450
:return: An iterator of bytes from the stream
451451
:rtype: Iterator[str]
452452
"""
453+
454+
class EndpointProvider(abc.ABC):
455+
"""Abstract base class for a EndpointProvider."""
456+
457+
@abc.abstractmethod
458+
def build_url(self, op_input: OperationInput) -> None:
459+
"""build the request url"""
460+

alibabacloud_oss_v2/vectors/client.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from ..signer.vectors_v4 import VectorsSignerV4
88
from .. import utils
99
from .. import validation
10-
from .. import endpoints
1110

1211
from . import models
1312
from . import operations
13+
from . import endpoints
1414
from .paginator import (
1515
ListVectorBucketsPaginator,
1616
ListVectorIndexPaginator,
@@ -34,29 +34,29 @@ def __init__(self, config: Config, **kwargs) -> None:
3434
self._build_vectors_user_agent(_config)
3535
self._client = _SyncClientImpl(_config, **kwargs)
3636
self._client._options.signer = VectorsSignerV4(user_id=config.user_id)
37+
self._client._options.endpoint_provider = endpoints.VectorsEndpointProvider(
38+
account_id=config.user_id,
39+
endpoint=self._client._options.endpoint
40+
)
3741

3842
def __repr__(self) -> str:
3943
return "<OssVectorsClient>"
4044

4145
def _resolve_vectors_endpoint(self, config: Config) -> None:
4246
"""vectors endpoint"""
43-
disable_ssl = utils.safety_bool(config.disable_ssl)
44-
endpoint = utils.safety_str(config.endpoint)
45-
region = utils.safety_str(config.region)
46-
if len(endpoint) > 0:
47-
endpoint = endpoints.add_scheme(endpoint, disable_ssl)
48-
elif validation.is_valid_region(region):
49-
if bool(config.use_internal_endpoint):
50-
etype = "internal"
51-
else:
52-
etype = "default"
53-
54-
endpoint = endpoints.vectors_from_region(region, disable_ssl, etype)
55-
56-
if endpoint == "":
47+
if config.endpoint is not None:
5748
return
5849

59-
config.endpoint = endpoint
50+
if not validation.is_valid_region(config.region):
51+
return
52+
53+
if bool(config.use_internal_endpoint):
54+
etype = "internal"
55+
else:
56+
etype = "default"
57+
58+
config.endpoint = endpoints.from_region(config.region, etype)
59+
6060

6161
def _build_vectors_user_agent(self, config: Config) -> str:
6262
if config.user_agent:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
# -*- coding: utf-8 -*-
3+
from urllib.parse import ParseResult, quote
4+
from ..types import EndpointProvider, OperationInput
5+
6+
def from_region(region: str, etype: str) -> str:
7+
"""Generate vectors endpoint from region"""
8+
if etype == "internal":
9+
return f"oss-{region}-internal.oss-vectors.aliyuncs.com"
10+
else:
11+
return f"oss-{region}.oss-vectors.aliyuncs.com"
12+
13+
14+
class VectorsEndpointProvider(EndpointProvider):
15+
def __init__(
16+
self,
17+
endpoint: ParseResult,
18+
account_id: str,
19+
) -> None:
20+
self._endpoint = endpoint
21+
self._account_id = account_id or ""
22+
23+
def build_url(self, op_input: OperationInput) -> str:
24+
"""build the request url"""
25+
host = ""
26+
paths = []
27+
if op_input.bucket is None:
28+
host = self._endpoint.netloc
29+
else:
30+
host = f'{self._account_id}-{op_input.bucket}.{self._endpoint.netloc}'
31+
32+
if op_input.key is not None:
33+
paths.append(quote(op_input.key))
34+
35+
return f'{self._endpoint.scheme}://{host}/{"/".join(paths)}'

0 commit comments

Comments
 (0)