Yelp / bravado

Bravado is a python client library for Swagger 2.0 services
Other
602 stars 117 forks source link

Can not pass a parameter whose name is python keyword #438

Closed simonzgx closed 4 years ago

simonzgx commented 4 years ago

When I try to use bravado and Swagger to build REST APIs client, there is a problem that I can't pass a parameter whose name is python keyword.

For instance, I need a variable named "from" that in python is a key word. My code is:

from bravado.client import SwaggerClient

host = 'https://api-testnet.bybit.com'
spec_uri = host + "/doc/swagger.txt"
...
...
client = SwaggerClient.from_url(spec_uri, config=config, http_client=request_client)

client.Kline.Kline_get(symbol='BTCUSD', interval='1m', from='1572231328')

Because from is a keyword in python, so there is a syntax error.

So I have to add this code in the call function to change the variable name

if "from_time" in op_kwargs: op_kwargs["from"]=op_kwargs.pop("from_time")

Can you provide me a batter way to solve the problem? Or fix it?

sjaensch commented 4 years ago

Unfortunately, there is currently no code in bravado that deals with Python reserved keywords. The solution is to use a dict to pass the arguments:

kline_get_kwargs = {
    'symbol': 'BTCUSD',
    'interval': '1m',
    'from': '1572231328',
}
client.Kline.Kline_get(**kline_get_kwargs)
simonzgx commented 4 years ago

got it, thanks.