Working with OpenAPI, Swagger Tools & Python

API Solution Requirements and Assumptions

Solution Architecture

Overview of Activities Covered

Install Swagger Editor

docker pull swaggerapi/swagger-editor
docker run -d -p 8085:8080 swaggerapi/swagger-editor

Using Swagger Editor to Create OpenAPI Spec

swagger: "2.0"
info:
description: API for Deactivating Customer Accounts
version: "0.1"
title: Deactivate Customer Account
host: localhost:8080
basePath: "/"
schemes:
- http
consumes:
- application/json
produces:
- application/json
security:
- Authorization-Header: []
paths:
/customer/deactivate/{cust_id}:
put:
tags:
- "Deactivate Customer"
summary: "Deactivate Customer Account"
operationId: deactivate
parameters:
- name: cust_id
in: path
required: true
type: string
responses:
200:
description: "Customer Deactivated"
400:
description: Request Failed.
securityDefinitions:
Authorization-Header:
type: apiKey
name: Authorization
in: header

Generate python-flask-server Module

C:\Users>cd c:\Apps\deact_api\python-flask-serverc:\Apps\deact_api\python-flask-server> pip3 install -r requirements.txtc:\Apps\deact_api\python-flask-server> python3 -m swagger_server
* Serving Flask app “__main__” (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

Test the python-flask-server Using Web UI

C:\Apps\deact_api\python-flask-server\swagger_server\__main__.py
----------------------------------------------------------------
#!/usr/bin/env python3import connexionfrom swagger_server import encoderdef main():
app = connexion.App(__name__, specification_dir='./swagger/')
app.app.json_encoder = encoder.JSONEncoder
app.add_api('swagger.yaml', arguments={'title': 'Deactivate Customer Account'})
app.run(port=8080)
if __name__ == '__main__':
main()
swagger.yaml
--------------------------------------------------------------
paths:
/customer/deactivate/{cust_id}:
put:
tags:
- "Deactivate Customer"
summary: "Deactivate Customer Account"
operationId: "deactivate"
parameters:
- name: "cust_id"
in: "path"
required: true
type: "string"
responses:
"200":
description: "Customer Deactivated"
"400":
description: "Request Failed"
x-swagger-router-controller: "swagger_server.controllers.deactivate_customer_controller"
deactivate_customer_controller.py
-----------------------------------------------------------------
import connexion
import six
from swagger_server import utildef deactivate(cust_id): # noqa: E501
"""Deactivate Customer Account
# noqa: E501:param cust_id:
:type cust_id: str
:rtype: None
"""
return 'do some magic!'

Generate our Python Client

Test Python Client

from __future__ import print_function
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint

configuration = swagger_client.Configuration()
configuration.api_key['Authorization'] = '11111222222'
configuration.host = "http://localhost:8080"

api_instance = swagger_client.DeactivateCustomerApi(swagger_client.ApiClient(configuration))
cust_id = '12345'

try:
# Deactivate Customer Account
req = api_instance.deactivate(cust_id)

status_code = api_instance.api_client.last_response.urllib3_response.status
status_msg = api_instance.api_client.last_response.data
print(status_code, status_msg)

except ApiException as e:
print("Exception when calling DeactivateCustomerApi->deactivate: %s\n" % e)

Where to from Here?

--

--

Learner. Interests include Cloud and Devops technologies.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store