0
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 3.0 の使い方 (GET 引数付き)

Last updated at Posted at 2021-10-22

こちらのページと同じことを Ubuntu 21.10 で行いました。
【超初心者向け】5分で試せる!OpenAPI(Swagger3.0)ドキュメント作成〜API自動生成

##仕様を記述##

GET で http://localhost:8080/hello?userName=Scott にアクセスするとリスポンスがある。

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

https://editor.swagger.io/ でバリデート
hello_aa.png

##Flask のサーバーを作成##
Generate Server -> python-flask

python-flask-server-generated.zip が出来る

解凍

unzip python-flask-server-generated.zip

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

pip3 install -r requirements.txt

コードの修正

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

サーバーの実行

python3 -m swagger_server

クライアントでアクセス

$ http http://0.0.0.0:8080/hello?userName=Scott
HTTP/1.0 200 OK
Content-Length: 17
Content-Type: application/json
Date: Fri, 22 Oct 2021 02:47:57 GMT
Server: Werkzeug/1.0.1 Python/3.9.7

"Hello, Scott !"

0
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
0
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?