Even though I'm not familiar with Web servers in my work We would like to introduce swagger-codegen to those who have been told to set up a web server for PoC (and in seconds). (It may not be possible to set up in seconds, but if you write the API specification in yaml, you can easily set up the server.)
When swagger-codegen defines API specifications in YAML (or JSON), Create a mock server according to the API definition, In addition, you can create a browser UI screen where you can hit the API as shown below.
If you do not have JRE, download and install it from the following.
Obtained from the maven repository.
This time, I used the following 2.4.15.
If you define the API specification, you can create a mock server according to the definition. Create a file like the one below.
swagger.yaml
swagger: "2.0"
info:
description: "This is an API for pet stores."
version: "1.0.0"
title: "Petstore API"
termsOfService: "http://swagger.io/terms/"
contact:
email: "[email protected]"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
paths:
/pet/{petId}:
get:
summary: "Pet information API"
description: "Returns information for the specified petId"
parameters:
- name: "petId"
in: "path"
description: "ID of the pet you want to get"
required: true
type: "integer"
format: "int64"
responses:
200:
description: "Response on success"
schema:
type: "object"
properties:
id:
type: "integer"
format: "int64"
name:
type: "string"
example: "doggie"
Please refer to the following for the meaning of the description in swagger.yml.
This time I want to use the familiar python, so I will create a mock server with pytoh-flask.
> java -jar {Downloaded jar file} generate -i swagger.yaml -l python-flask -o test-server
You can make Go, Node.js, Ruby on Rails, whatever by changing the -l
option.
See below for what else the -l
option has.
In the case of python-flask, you can create a mock server like this. Since a Dockerfile is also created, it is also possible to make it a Docker container.
> tree server
server
├── Dockerfile
├── README.md
├── git_push.sh
├── requirements.txt
├── setup.py
├── swagger_server
│ ├── __init__.py
│ ├── __main__.py
│ ├── controllers
│ │ ├── __init__.py
│ │ └── default_controller.py
│ ├── encoder.py
│ ├── models
│ │ ├── __init__.py
│ │ ├── base_model_.py
│ │ └── inline_response200.py
│ ├── swagger
│ │ └── swagger.yaml
│ ├── test
│ │ ├── __init__.py
│ │ └── test_default_controller.py
│ └── util.py
├── test-requirements.txt
└── tox.ini
In this, the actual processing is described in the pet_pet_id_get
part of default_controller.py
.
default_controller.py
import connexion
import six
from swagger_server.models.inline_response200 import InlineResponse200 # noqa: E501
from swagger_server import util
def pet_pet_id_get(petId): # noqa: E501
"""Pet information API
Returns information for the specified petId# noqa: E501
:param petId:ID of the pet you want to get
:type petId: int
:rtype: InlineResponse200
"""
return 'do some magic!'
Install the required packages. Only pip requirements.txt is written in the environment created by swagger-codegen, In the conda environment, you can run it by installing the following.
> conda install connexion
> conda install swagger-ui-bundle
start up.
> cd test-server
> python -m swagger_server
If you access the following from a browser and see the screen, you can start it.
Thank you for your hard work!
Recommended Posts