Skip to content

Commit

Permalink
Merge pull request #56 from thousandeyes/CP-2395
Browse files Browse the repository at this point in the history
CP-2395 Clean up configuration.py
  • Loading branch information
vascmart-te authored Sep 27, 2024
2 parents 3303af5 + a4c6de9 commit ffa07ce
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 267 deletions.
47 changes: 2 additions & 45 deletions thousandeyes-sdk-core/src/thousandeyes_sdk/core/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ class ApiClient:
:param header_name: a header to pass when making calls to the API.
:param header_value: a header value to pass when making calls to
the API.
:param cookie: a cookie to include in the header when making calls
to the API
"""

PRIMITIVE_TYPES = (float, bool, bytes, str, int)
Expand All @@ -71,19 +69,15 @@ def __init__(
configuration=None,
header_name=None,
header_value=None,
cookie=None
) -> None:
# use default configuration if none is provided
if configuration is None:
configuration = Configuration.get_default()
self.configuration = configuration

self.rest_client = rest.RESTClientObject(configuration)
self.default_headers = {}
if header_name is not None:
self.default_headers[header_name] = header_value
self.cookie = cookie
self.client_side_validation = configuration.client_side_validation

def __enter__(self):
return self
Expand Down Expand Up @@ -171,8 +165,6 @@ def param_serialize(
# header parameters
header_params = header_params or {}
header_params.update(self.default_headers)
if self.cookie:
header_params['Cookie'] = self.cookie
if header_params:
header_params = self.sanitize_for_serialization(header_params)
header_params = dict(
Expand All @@ -190,7 +182,7 @@ def param_serialize(
# specified safe chars, encode everything
resource_path = resource_path.replace(
'{%s}' % k,
quote(str(v), safe=config.safe_chars_for_path_param)
quote(str(v), safe='')
)

# post parameters
Expand Down Expand Up @@ -299,8 +291,6 @@ def response_deserialize(
try:
if response_type == "bytearray":
return_data = response_data.data
elif response_type == "file":
return_data = self.__deserialize_file(response_data)
elif response_type is not None:
match = None
content_type = response_data.getheader('content-type')
Expand Down Expand Up @@ -635,9 +625,7 @@ def _apply_auth_params(
The object type is the return value of sanitize_for_serialization().
:param auth_setting: auth settings for the endpoint
"""
if auth_setting['in'] == 'cookie':
headers['Cookie'] = auth_setting['value']
elif auth_setting['in'] == 'header':
if auth_setting['in'] == 'header':
if auth_setting['type'] != 'http-signature':
headers[auth_setting['key']] = auth_setting['value']
elif auth_setting['in'] == 'query':
Expand All @@ -647,37 +635,6 @@ def _apply_auth_params(
'Authentication token must be in `query` or `header`'
)

def __deserialize_file(self, response):
"""Deserializes body to file
Saves response body into a file in a temporary folder,
using the filename from the `Content-Disposition` header if provided.
handle file downloading
save response body into a tmp file and return the instance
:param response: RESTResponse.
:return: file path.
"""
fd, path = tempfile.mkstemp(dir=self.configuration.temp_folder_path)
os.close(fd)
os.remove(path)

content_disposition = response.getheader("Content-Disposition")
if content_disposition:
m = re.search(
r'filename=[\'"]?([^\'"\s]+)[\'"]?',
content_disposition
)
assert m is not None, "Unexpected 'content-disposition' header value"
filename = m.group(1)
path = os.path.join(os.path.dirname(path), filename)

with open(path, "wb") as f:
f.write(response.data)

return path

def __deserialize_primitive(self, data, klass):
"""Deserializes string to primitive type.
Expand Down
Loading

0 comments on commit ffa07ce

Please sign in to comment.