Skip to content

Commit

Permalink
Add support for body-data requests
Browse files Browse the repository at this point in the history
  • Loading branch information
wael34218 committed Nov 5, 2019
1 parent d7a35fe commit 202f4bb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
19 changes: 19 additions & 0 deletions openapi_generator/openapi_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,25 @@ def _get_request_body(response):
except ValueError:
print("invalid json passed")

else:
try:
request_body['content'] = {
response.headers['Content-Type'].split(",")[0]: {
'schema': {
'type': 'string',
'format': 'binary',
},
'examples': {
'sample_file': {
'summary': 'This is a sample binary file',
'value': str(response.request.body)
}
}
}
}
except ValueError:
print("Data body is invalid")

return request_body

@staticmethod
Expand Down
Binary file added openapi_generator/test.wav
Binary file not shown.
18 changes: 18 additions & 0 deletions openapi_generator/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,21 @@ def test_json_schema(self):
spec = json.load(f)
errors_iterator = openapi_v3_spec_validator.iter_errors(spec)
self.assertEqual(len(list(errors_iterator)), 0)

def test_audio_file(self):
gen = OpenapiGenerator("Title", "Testing description", "0.0.1",
"test.audio.upload")
lnk = "test.audio.upload"
with open('test.wav', 'rb') as f:
aud = f.read()

response = requests.post(lnk, data=aud, headers={
'Content-Type': 'application/body-data;rate={};channels={}'.format(16000, 1)})

gen.add_response(response)
gen.export("wav.json", extension="json")

with open("wav.json") as f:
spec = json.load(f)
errors_iterator = openapi_v3_spec_validator.iter_errors(spec)
self.assertEqual(len(list(errors_iterator)), 0)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='openapi_generator',
version='1.0.3',
version='1.0.4',
description="A library to generate OpenAPI documentation from requests",
author='Wael Farhan',
author_email='wael34218@gmail.com',
Expand All @@ -15,7 +15,7 @@
],
keywords='flask framework base class generic',
packages=['openapi_generator'],
download_url='https://github.com/wael34218/python_openapi_generator/archive/v1.0.3.tar.gz',
download_url='https://github.com/wael34218/python_openapi_generator/archive/v1.0.4.tar.gz',
url='https://github.com/wael34218/python_openapi_generator',
install_requires=[
'pyyaml',
Expand Down

0 comments on commit 202f4bb

Please sign in to comment.