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 1 year has passed since last update.

OpenAPI 3.0 の使い方

Last updated at Posted at 2021-10-22

こちらのページを参考にしました。
作って理解する OpenAPI 3.0 / connexion

仕様を記述

GET で http://localhost:8080/api/v0/health にアクセスするとリスポンスがある。

ex02.yaml
openapi: 3.0.3
info:
  title: OpenAPI Tutorial
  description: OpenAPI Tutorial by halhorn
  version: 0.0.0
servers:
  - url: https://example.com/api/v0
    description: プロダクション API
  - url: http://{host}:{port}/api/v0
    description: 開発用
    variables:
      host:
        default: localhost
      port:
        default: '10080'
paths:
  /health:
    get:
      operationId: openapitutorial.controller.health.call
      summary: サーバーの状態を返します
      description: サーバーの状態を返します。
      responses:
        '200':
          description: サーバーは正常に動作しています
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/get_health_response'
components:
  schemas:
    get_health_response:
      description: サーバーの状態のレスポンス
      type: object
      properties:
        status:
          type: string
          enum:
            - ok
      required:
        - status

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

openapi_aa.png

JSON に変換

この工程は必要というわけではありません。

File -> convert and save as JSON

openapi.json
{
  "openapi": "3.0.3",
  "info": {
    "title": "OpenAPI Tutorial",
    "description": "OpenAPI Tutorial by halhorn",
    "version": "0.0.0"
  },
  "servers": [
    {
      "url": "https://example.com/api/v0",
      "description": "プロダクション API"
    },
    {
      "url": "http://{host}:{port}/api/v0",
      "description": "開発用",
      "variables": {
        "host": {
          "default": "localhost"
        },
        "port": {
          "default": "10080"
        }
      }
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "openapitutorial.controller.health.call",
        "summary": "サーバーの状態を返します",
        "description": "サーバーの状態を返します。",
        "responses": {
          "200": {
            "description": "サーバーは正常に動作しています",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/get_health_response"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "get_health_response": {
        "description": "サーバーの状態のレスポンス",
        "type": "object",
        "properties": {
          "status": {
            "type": "string",
            "enum": [
              "ok"
            ]
          }
        },
        "required": [
          "status"
        ]
      }
    }
  }
}

Flask のサーバーを作成

Generate Server -> python-flask

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

解凍

unzip python-flask-server-generated.zip

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

pip3 install -r requirements.txt

サーバーの実行

python3 -m swagger_server

クライアントでアクセス

$ http http://0.0.0.0:8080/api/v0/health
HTTP/1.1 200 OK
Connection: close
Content-Length: 17
Content-Type: application/json
Date: Sun, 09 Apr 2023 08:24:17 GMT
Server: Werkzeug/2.2.3 Python/3.10.10

"do some magic!"

関連ページ

OpenAPI 3.0 の使い方 (GET 引数付き)
OpenAPI 3.0 の使い方 (POST 引数付き)

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?