Ivozprovider API swagger codegen with python

86 views
Skip to first unread message

Gil Obradors

unread,
Feb 22, 2020, 4:55:25 AM2/22/20
to ivozprovider-users
I would like to share the first steeps for getting ready the API to play with Ivoz with Python
First of all, thanks to Irontec team, Swagger and Python commuity. Thanks to him for thinking openly and betting on inclusion

I spent 1-2h to understand and play with the code generator of Swagger, I would like that the next, give it with 10 minutes. Although sooner or later you will need to understand what are you doing, and may be you will need more time.
I wish to drain your initial time

Swagger-Codegen allow us to create the interface automatically for a lot of langauges, here Python. More info.


Requirements:
Java jdk 8+
Know swagger version of Ivoz Avpi ( Ivoz now is 2.0)

# Download current stable 2.x.x branch (Swagger and OpenAPI version 2)
wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.12/swagger-codegen-cli-2.4.12.jar -O swagger-codegen-cli.jar

# Create Python package for Ivozprovider API :
java -jar swagger-codegen-cli-2.4.12.jar generate -i https://xxx.xxxx.xxt/api/brand/swagger.json -l python

or if you have problems with ssl, can be a file downlaoded

java -jar swagger-codegen-cli-2.4.12.jar generate -i swagger.json -l python

The package can be customizable(  name...) but I write the essentials.


Example code:



from __future__ import print_function
import time
import swagger_client
from swagger_client.api.provider_api import ProviderApi
from swagger_client.rest import ApiException
import json
from pprint import pprint

# Configure API key authorization: bearer
configuration = swagger_client.Configuration()
configuration.api_key['Authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
configuration.api_key_prefix['Authorization'] = 'Bearer'
configuration.host = 'https://xxxxxxxxxxxxx/api/brand'
configuration.verify_ssl = False

# # create an instance of the API class
api_instance = swagger_client.AuthApi(swagger_client.ApiClient(configuration))
username = 'aaaaaaaaaaaaa' # str |
password = 'bbbbbbbbbbbb' # str |

try:
# Retrieve JWT token
api_instance.post_admin_authenticate(username, password)
configuration.api_key['Authorization'] = json.loads(api_instance.api_client.last_response.data)['token']
except ApiException as e:
print("Exception when calling AuthApi->post_admin_authenticate: %s\n" % e)

provider_api_client = swagger_client.ProviderApi(swagger_client.ApiClient(configuration))
provider_api_client.get_company_collection()



And magic appears, all methods into provider_api_client, and first invocation to get all companies.


We write :)






Reply all
Reply to author
Forward
0 new messages