Skip to content

Commit

Permalink
fix(python): pass format_options correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
theSoenke committed Oct 13, 2023
1 parent 7442f08 commit 557c108
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 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"
}
26 changes: 24 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,34 @@ def test_locale_delete(self):
"""
pass

def test_locale_download(self):
@patch('urllib3.PoolManager.request')
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", mock_get.call_args_list[0].args[1])

self.assertEqual([('file_format', 'simple_json'), ('format_options[enable_pluralization]', True), ('format_options[custom_metadata_columns][E]', 'text')], mock_get.call_args_list[0][1]['fields'])

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
23 changes: 23 additions & 0 deletions openapi-generator/templates/python/rest.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class RESTClientObject(object):
if 'Content-Type' not in headers:
headers['Content-Type'] = 'application/json'

if query_params:
query_params = self.flatten_params(query_params)

try:
# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
Expand Down Expand Up @@ -295,3 +298,23 @@ class RESTClientObject(object):
_preload_content=_preload_content,
_request_timeout=_request_timeout,
body=body)

def flatten_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

0 comments on commit 557c108

Please sign in to comment.