Skip to content

Commit cf1b482

Browse files
committed
feat: support universal client
1 parent 49ad9ef commit cf1b482

File tree

4 files changed

+106
-9
lines changed

4 files changed

+106
-9
lines changed

volcenginesdkcore/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
from volcenginesdkcore.configuration import Configuration
44
from volcenginesdkcore.api_client import ApiClient
55
from volcenginesdkcore.universal import UniversalApi, UniversalInfo
6+
from volcenginesdkcore.flatten import Flatten

volcenginesdkcore/flatten.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# coding: utf-8
2+
# Borrowed from https://github.com/amirziai/flatten
3+
4+
# python 2 and python 3 compatibility library
5+
import six
6+
7+
8+
class Flatten(object):
9+
10+
def __init__(self, nested_dict, separator=".", start_index=1, root_keys_to_ignore=None, replace_separators=None):
11+
"""
12+
:param nested_dict: dictionary we want to flatten
13+
:param start_index: start index for list param
14+
:param separator: string to separate dictionary keys by
15+
:param root_keys_to_ignore: set of root keys to ignore from flattening
16+
:param str replace_separators: Replace separators within keys
17+
"""
18+
self.nested_dict = nested_dict
19+
self.separator = separator
20+
self.start_index = start_index
21+
self.root_keys_to_ignore = root_keys_to_ignore
22+
self.replace_separators = replace_separators
23+
24+
def flat(self):
25+
if not isinstance(self.nested_dict, dict):
26+
raise TypeError("flatten requires a dictionary input")
27+
28+
if not isinstance(self.separator, six.string_types):
29+
raise TypeError("separator must be string")
30+
31+
if self.root_keys_to_ignore is None:
32+
root_keys_to_ignore = set()
33+
34+
if len(self.nested_dict) == 0:
35+
return {}
36+
37+
# This global dictionary stores the flattened keys and values and is
38+
# ultimately returned
39+
flattened_dict = dict()
40+
41+
def _flatten(object_, key):
42+
"""
43+
For dict, list and set objects_ calls itself on the elements and for
44+
other types assigns the object_ to
45+
the corresponding key in the global flattened_dict
46+
:param object_: object to flatten
47+
:param key: carries the concatenated key for the object_
48+
:return: None
49+
"""
50+
# Empty object can't be iterated, take as is
51+
if not object_:
52+
flattened_dict[key] = object_
53+
# These object types support iteration
54+
elif isinstance(object_, dict):
55+
for object_key in object_:
56+
if not (not key and object_key in root_keys_to_ignore):
57+
_flatten(
58+
object_[object_key],
59+
self._construct_key(
60+
key,
61+
self.separator,
62+
object_key,
63+
replace_separators=self.replace_separators))
64+
elif isinstance(object_, (list, set, tuple)):
65+
for index, item in enumerate(object_):
66+
_flatten(
67+
item,
68+
self._construct_key(
69+
key,
70+
self.separator,
71+
index + self.start_index,
72+
replace_separators=self.replace_separators))
73+
# Anything left take as is
74+
else:
75+
flattened_dict[key] = object_
76+
77+
_flatten(self.nested_dict, None)
78+
return flattened_dict
79+
80+
def _construct_key(self, previous_key, separator, new_key, replace_separators=None):
81+
"""
82+
Returns the new_key if no previous key exists, otherwise concatenates
83+
previous key, separator, and new_key
84+
:param previous_key:
85+
:param separator:
86+
:param new_key:
87+
:param str replace_separators: Replace separators within keys
88+
:return: a string if previous_key exists and simply passes through the
89+
new_key otherwise
90+
"""
91+
if replace_separators is not None:
92+
new_key = str(new_key).replace(separator, replace_separators)
93+
if previous_key:
94+
return u"{}{}{}".format(previous_key, separator, new_key)
95+
else:
96+
return new_key

volcenginesdkcore/signv4.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# coding: utf-8
22

3-
# Copy from https://github.com/volcengine/volc-sdk-python
4-
53
import datetime
64
import hmac
75
import hashlib

volcenginesdkcore/universal.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66

77
class UniversalInfo(object):
8-
def __init__(self, method=None, service=None, version=None, action=None):
8+
def __init__(self, method=None, service=None, version=None, action=None, content_type=None):
99
self.method = method
1010
self.service = service
1111
self.version = version
1212
self.action = action
13+
self.content_type = content_type
1314

1415

1516
class UniversalApi(object):
@@ -48,7 +49,7 @@ def do_call_with_http_info(self, info, body, **kwargs): # noqa: E501
4849
"Missing the required parameter `body` when calling `do_call`") # noqa: E501
4950

5051
if type(params['body']) is not dict:
51-
raise ValueError(
52+
raise TypeError(
5253
"The required parameter `body` must be dict") # noqa: E501
5354

5455
collection_formats = {}
@@ -70,12 +71,13 @@ def do_call_with_http_info(self, info, body, **kwargs): # noqa: E501
7071
header_params['Accept'] = self.api_client.select_header_accept(
7172
['application/json']) # noqa: E501
7273

73-
# HTTP header `Content-Type`
74-
header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501
75-
['text/plain']) # noqa: E501
74+
if info.content_type is not None:
75+
# HTTP header `Content-Type`
76+
header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501
77+
[info.content_type]) # noqa: E501
7678

77-
if info.method.lower() != "get":
78-
header_params['Content-Type'] = 'application/json' # noqa: E501
79+
if info.method.lower() == "get":
80+
query_params = list(body.items())
7981

8082
# Authentication setting
8183
auth_settings = ['volcengineSign'] # noqa: E501

0 commit comments

Comments
 (0)