Skip to content

Commit f380919

Browse files
committed
refine code
1 parent d74d65e commit f380919

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+723
-1006
lines changed

alibabacloud_oss_v2/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444

4545
from .config import Config
4646
from .client import Client
47-
from .vector_client import VectorClient
4847

4948
# If the Crypto(pycryptodome) module was not imported, the encryption feature is not supported.
5049
try:

alibabacloud_oss_v2/_client.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any, Optional, Dict, Iterable, List, Union, cast, Tuple, Iterator
66
from urllib.parse import urlparse, ParseResult, urlencode, quote
77
from xml.etree import ElementTree as ET
8+
import json
89
from . import retry
910
from . import transport
1011
from . import exceptions
@@ -367,7 +368,10 @@ def service_error_response_handler(response: HttpResponse) -> None:
367368
if not response.is_stream_consumed:
368369
_ = response.read()
369370

370-
raise _to_service_error(response)
371+
if response.headers.get('Content-Type', '') == 'application/json':
372+
raise _to_service_error_json(response)
373+
else:
374+
raise _to_service_error(response)
371375

372376
# insert service error responsed handler first
373377
handlers.append(service_error_response_handler)
@@ -677,6 +681,61 @@ def _to_service_error(response: HttpResponse) -> exceptions.ServiceError:
677681
error_fileds=error_fileds
678682
)
679683

684+
def _to_service_error_json(response: HttpResponse) -> exceptions.ServiceError:
685+
timestamp = serde.deserialize_httptime(response.headers.get('Date'))
686+
content = response.content or b''
687+
response.close()
688+
689+
error_fileds = {}
690+
code = 'BadErrorResponse'
691+
message = ''
692+
ec = ''
693+
request_id = ''
694+
err_body = b''
695+
try:
696+
err_body = content
697+
if len(err_body) == 0:
698+
err_body = base64.b64decode(
699+
response.headers.get('x-oss-err', ''))
700+
root = json.loads(err_body)
701+
errElem = root.get('Error', None)
702+
if errElem is not None:
703+
for k, v in errElem.items():
704+
if isinstance(v, str):
705+
error_fileds[k] = v
706+
message = error_fileds.get('Message', '')
707+
code = error_fileds.get('Code', '')
708+
ec = error_fileds.get('EC', '')
709+
request_id = error_fileds.get('RequestId', '')
710+
else:
711+
message = f'Expect root node Error, but get {root.keys()}.'
712+
except json.JSONDecodeError as e:
713+
errstr = err_body.decode()
714+
if '"Error":' in errstr:
715+
m = re.search('"Code":(.*),', errstr)
716+
if m:
717+
code = m.group(1).strip(' "')
718+
m = re.search('<Message>(.*)</Message>', errstr)
719+
if m:
720+
message = m.group(1).strip(' "')
721+
if len(message) == 0:
722+
message = f'Failed to parse json from response body due to: {str(e)}. With part response body {err_body[:256]}.'
723+
except Exception as e:
724+
message = f'The body of the response was not readable, due to : {str(e)}.'
725+
726+
return exceptions.ServiceError(
727+
status_code=response.status_code,
728+
code=code,
729+
message=message,
730+
request_id=request_id or response.headers.get('x-oss-request-id', ''),
731+
ec=ec or response.headers.get('x-oss-ec', ''),
732+
timestamp=timestamp,
733+
request_target=f'{response.request.method} {response.request.url}',
734+
snapshot=content,
735+
headers=response.headers,
736+
error_fileds=error_fileds
737+
)
738+
680739
def _build_user_agent(config: Config) -> str:
681740
if config.user_agent:
682741
return f'{utils.get_default_user_agent()}/{config.user_agent}'

alibabacloud_oss_v2/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def __init__(
2929
additional_headers: Optional[List[str]] = None,
3030
user_agent: Optional[str] = None,
3131
cloud_box_id: Optional[str] = None,
32-
enable_auto_detect_cloud_box_id: Optional[bool] = None
32+
enable_auto_detect_cloud_box_id: Optional[bool] = None,
33+
user_id: Optional[str] = None
3334
) -> None:
3435
"""
3536
Args:
@@ -73,6 +74,7 @@ def __init__(
7374
user_agent: (str, optional): The optional user specific identifier appended to the User-Agent header.
7475
cloud_box_id: (str, optional): The cloud box id.
7576
enable_auto_detect_cloud_box_id: (bool, optional): The cloud box id is automatically extracted from endpoint.
77+
user_id: (str, optional): The user id, must be required in vectors options.
7678
"""
7779
self.region = region
7880
self.endpoint = endpoint
@@ -98,6 +100,7 @@ def __init__(
98100
self.user_agent = user_agent
99101
self.cloud_box_id = cloud_box_id
100102
self.enable_auto_detect_cloud_box_id = enable_auto_detect_cloud_box_id
103+
self.user_id = user_id
101104

102105
def load_default() -> Config:
103106
"""Using the SDK's default configuration"""

alibabacloud_oss_v2/signer/signer_vector_v4.py renamed to alibabacloud_oss_v2/signer/vectors_v4.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
from ..types import HttpRequest, SigningContext, Signer
1212

1313

14-
class SignerVectorsV4(Signer):
14+
class VectorsSignerV4(Signer):
1515
"""Signer Vectors V4
1616
"""
1717

18+
def __init__(self, user_id: str) -> None:
19+
self._user_id = user_id
20+
1821
def sign(self, signing_ctx: SigningContext) -> None:
1922
if signing_ctx is None:
2023
raise exceptions.ParamNullError(field="SigningContext")
@@ -26,6 +29,10 @@ def sign(self, signing_ctx: SigningContext) -> None:
2629
if signing_ctx.request is None:
2730
raise exceptions.ParamNullOrEmptyError(
2831
field="SigningContext.request")
32+
33+
if self._user_id is None or self._user_id == '':
34+
raise exceptions.ParamNullOrEmptyError(
35+
field="SignerVectorsV4.user_id")
2936

3037
if signing_ctx.auth_method_query:
3138
return self._auth_query(signing_ctx)
@@ -231,7 +238,7 @@ def _calc_canonical_request(self,
231238
request = signing_ctx.request
232239

233240
# canonical uri
234-
uri = '/'
241+
uri = f'/acs:ossvector:{signing_ctx.region}:{self._uid}:'
235242
if signing_ctx.bucket is not None:
236243
uri = uri + signing_ctx.bucket + '/'
237244
if signing_ctx.key is not None:

alibabacloud_oss_v2/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,5 +269,4 @@ def get_default_user_agent() -> str:
269269
def get_vector_user_agent() -> str:
270270
"""Returns the default user agent string
271271
"""
272-
sysinfo = f'{platform.system()}/{platform.release()}/{platform.machine()};{platform.python_version()}'
273-
return f'vector/alibabacloud-python-sdk-v2/{VERSION} ({sysinfo})'
272+
return 'vector-client'

alibabacloud_oss_v2/vector_models/__init__.py

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# sub mod
4+
from . import models
5+
6+
7+
from .client import Client

0 commit comments

Comments
 (0)