1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

openapi-generator-cli (python-flask)

Posted at

こちらで行ったことを openapi-generator-cli で行いました。
OpenAPI 3.0 の使い方 (GET 引数付き)

バージョン

$ npx @openapitools/openapi-generator-cli version
5.3.0
hello.yaml
openapi: 3.0.2
info:
  description: ユーザ名を与えると挨拶を返してくれるAPI
  version: 1.0.0
  title: Hello
tags:
  - name: hello
    description: ユーザに挨拶を返すAPI
paths:
  /hello:
    get:
      tags:
        - hello
      description: ユーザに挨拶する。
      operationId: getHello
      parameters:
        - name: userName
          in: query
          description: ユーザ名
          required: true
          schema:
            type: string
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: object
                properties:
                  HelloUser:
                    type: string
                    example: Hello, userName
        "400":
          description: Bad Request
        "500":
          description: Internal Server Error

#Flask のサーバーを作成#

npx @openapitools/openapi-generator-cli generate \
	-g python-flask \
	-i ./hello.yaml \
	-o ./src \
	--api-package=api \
	--model-package=model \
	--additional-properties=withSeparateModelsAndApi=true

コードの修正

openapi_server/controllers/hello_controller.py
省略
#    return 'do some magic!'
    return 'Hello, ' + user_name + ' !'

ライブラリーのインストール

pip3 install -r requirements.txt

サーバーの実行

python3 -m openapi_server

クライアントでアクセス

$ http http://0.0.0.0:8080/hello?userName=Scott
HTTP/1.0 200 OK
Content-Length: 17
Content-Type: application/json
Date: Mon, 25 Oct 2021 09:51:48 GMT
Server: Werkzeug/1.0.1 Python/3.9.7

"Hello, Scott !"

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?