Forum Discussion

rholloway's avatar
rholloway
Occasional Visitor
5 years ago

Python Client JSON Request Body

Does anyone know if there is a way to generate python client that allows you to create a JSON body in a POST request without users needing to know they must create and pass a model object to the API function call, but instead use parameter names to create JSON in same way POSTing form-data or query params works?

 

For example, I would like the following:

 

...
operationId: get_things
parameters:
- in: body name: request schema: type: object properties: things: type: array items: string
other:
type: string
#!/usr/bin/python

import api

things = ["thing1", "thing2"]
api.get_things(things=things, other="test")

to generate a request to server that looks like

 

{"things": ["thing1", "thing2"], "other": "test")

The problem is it failes because it only expects an argument called `request` on the function, not `things` and `other`

 

To get the correct request sent, we must do something like

 

definitions:
  ThingDef:
    type: object
    properties:
      things:
        type: array
        items: string
other:
type: string
#/usr/bin/python
import api

things = ["thing1", "thing2"]
thing_def = ThingDef(things=things, other="test")
api.get_things(request=thing_def)

Alternatively, it looks like you can manually create the dict and pass as request argument...

 

things = ["thing1", "thing2"]
api.get_things(request={"things": things, "other": "test"})

 

I can understand in complicated models being sent to the server, it might make sense to require a user to populate a model object and use that in the function call. However, if we want to send very basic JSON, it is much more intuitive and more in line with syntax for sending form-data, query params, etc. to just use the attribute names as function parameters. However, as is the generated client does not create the JSON request body in this way.

 

Is there a way to specify the yaml differently to obtain what I want, or do I need to create a custom codegen for python to generate a client as desired?

 

No RepliesBe the first to reply