'How do I associate a filename with an uploaded file for an openapi autogenerated python call?
Current situation: My team is using openapi autogenerated code to provide the interface between angular an python clients on the one side, and a REST server based on Spring Boot on the other. I'm dealing with an endpoint that uploads files. Now, often when uploading files to Spring, the uploading app will include the name of the file in the headers, which the spring side will receive as Resource.filename. Ive been unable to figure out how to induce the autogenerated python code to do the same, if it is indeed possible at all.
For clarification - openapi autogen has seven different python generators. WE're using the one that's simply called "python"
The pertinent section of the openapi.yaml looks something like this:
/api/{item_id}/data:
post:
parameters:
- $ref: "#/components/parameters/item_id"
requestBody:
content:
"*/*":
schema:
type: string
format: binary
required: true
operationId: postItemData
tags: [ "Item Ops" ]
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/results"
...and the python that I'm trying to write looks something like this:
with open(os.path.join(scriptdir, "testData2.txt")) as newFile:
results = itemOpsAPI.post_item_data(itemID, newFile)
I tried looking at the autogenerated code, but it was not super-useful. the first layer is more or less as follows:
def post_item_data(
self,
item_id,
body,
**kwargs
):
"""
# noqa: E501
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.post_item_data(item_id, body, async_req=True)
>>> result = thread.get()
Args:
item_id (str):
body (file_type):
Keyword Args:
_return_http_data_only (bool): response data without head status
code and headers. Default is True.
_preload_content (bool): if False, the urllib3.HTTPResponse object
will be returned without reading/decoding response data.
Default is True.
_request_timeout (int/float/tuple): timeout setting for this request. If
one number provided, it will be total request timeout. It can also
be a pair (tuple) of (connection, read) timeouts.
Default is None.
_check_input_type (bool): specifies if type checking
should be done one the data sent to the server.
Default is True.
_check_return_type (bool): specifies if type checking
should be done one the data received from the server.
Default is True.
_host_index (int/None): specifies the index of the server
that we want to use.
Default is read from the configuration.
async_req (bool): execute request asynchronously
Returns:
Results
If the method is called asynchronously, returns the request
thread.
"""
kwargs['async_req'] = kwargs.get(
'async_req', False
)
kwargs['_return_http_data_only'] = kwargs.get(
'_return_http_data_only', True
)
kwargs['_preload_content'] = kwargs.get(
'_preload_content', True
)
kwargs['_request_timeout'] = kwargs.get(
'_request_timeout', None
)
kwargs['_check_input_type'] = kwargs.get(
'_check_input_type', True
)
kwargs['_check_return_type'] = kwargs.get(
'_check_return_type', True
)
kwargs['_host_index'] = kwargs.get('_host_index')
kwargs['workspace_hash'] = \
workspace_hash
kwargs['element_id'] = \
element_id
kwargs['version_id'] = \
version_id
kwargs['body'] = \
body
return self.post_item_data.call_with_http_info(**kwargs)
I recognize that it might be possible to add the filename to the "file_type" in a way that the generated code would recognize, but I have no real idea of how to do that and have it actually work. I've searched around online reasonably thoroughly already - the bits that I've found that decribe how to do things in python are asking me to make changes within the area that the autogen code has taken over. The bits that I've found on how to handle openapi autogenerated code don't seem to cover this particular issue on the python side.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
