Skip to content

Commit

Permalink
fix(python): pass format_options correctly (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
theSoenke authored Oct 18, 2023
1 parent 66ccd46 commit 3621400
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
3 changes: 3 additions & 0 deletions clients/python/test/fixtures/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key": "value"
}
23 changes: 21 additions & 2 deletions clients/python/test/test_locales_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,31 @@ def test_locale_delete(self):
"""
pass

def test_locale_download(self):
@patch('urllib3.PoolManager.urlopen')
def test_locale_download(self, mock_get):
"""Test case for locale_download
Download a locale # noqa: E501
"""
pass

body = bytes('{"key":"value"}', 'utf-8')
mock_get.return_value = Mock(ok=True)
mock_get.return_value.data = body
mock_get.return_value.status = 200
mock_get.return_value.getencoding.return_value = 'utf-8'
mock_get.return_value.getheader.side_effect = { 'Content-Disposition': None }.get

with phrase_api.ApiClient(self.configuration) as api_client:
api_instance = phrase_api.api.locales_api.LocalesApi(api_client)
api_response = api_instance.locale_download("project_id_example", "en", file_format="simple_json", format_options={"enable_pluralization": True, "custom_metadata_columns": { "E": "text" }})

self.assertEqual("https://api.phrase.com/v2/projects/project_id_example/locales/en/download?file_format=simple_json&format_options%5Benable_pluralization%5D=True&format_options%5Bcustom_metadata_columns%5D%5BE%5D=text", mock_get.call_args_list[0].args[1])

self.assertIsNotNone(api_response)
file = open(api_response, "r")
content = file.read()
file.close()
self.assertEqual(body.decode(), content)

def test_locale_show(self):
"""Test case for locale_show
Expand Down
12 changes: 8 additions & 4 deletions clients/python/test/test_uploads_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ def test_upload_create(self, mock_post):
project_id = "project_id_example"
with phrase_api.ApiClient(self.configuration) as api_client:
api_instance = phrase_api.UploadsApi(api_client)
api_response = api_instance.upload_create(project_id, file="./README.md", file_format="simple_json")

self.assertEqual("POST", mock_post.call_args_list[0].args[0])
self.assertEqual("https://api.phrase.com/v2/projects/project_id_example/uploads", mock_post.call_args_list[0].args[1])
api_response = api_instance.upload_create(
project_id,
file="./test/fixtures/en.json",
file_format="simple_json",
format_options={"enable_pluralization": True}
)

mock_post.assert_called_with("POST", "https://api.phrase.com/v2/projects/project_id_example/uploads", query_params=[], headers={'Accept': 'application/json', 'Content-Type': 'multipart/form-data', 'User-Agent': 'OpenAPI-Generator/1.14.0/python', 'Authorization': 'token YOUR_API_KEY'}, post_params=[('file_format', 'simple_json'), ('format_options', {'enable_pluralization': True}), ('file', ('en.json', b'{\n "key": "value"\n}\n', 'application/json'))], body=None, _preload_content=True, _request_timeout=None)

self.assertIsNotNone(api_response)
self.assertIsInstance(api_response, phrase_api.models.upload.Upload)
Expand Down
25 changes: 23 additions & 2 deletions openapi-generator/templates/python/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ class ApiClient(object):
# query parameters
if query_params:
query_params = self.sanitize_for_serialization(query_params)
query_params = self.parameters_to_tuples(query_params,
collection_formats)
query_params = self.parameters_to_tuples(query_params, collection_formats)
query_params = self.flatten_query_params(query_params)

# post parameters
if post_params or files:
Expand Down Expand Up @@ -473,6 +473,27 @@ class ApiClient(object):
new_params.append((k, v))
return new_params

def flatten_query_params(self, params):
result_params = {}
for key, value in params:
if isinstance(value, dict):
self.flatten_dict(result_params, value, key)
else:
result_params[key] = value

return [(key, value) for (key,value) in result_params.items()]

def flatten_dict(self, params, param, namespace):
for key, value in param.items():
new_key = "{0}[{1}]".format(namespace, key)
if isinstance(value, dict):
params = self.flatten_dict(params, value, new_key)
else:
params[new_key] = value

return params


def files_parameters(self, files=None):
"""Builds form parameters.

Expand Down

0 comments on commit 3621400

Please sign in to comment.