How to Implement GET API in swagger using python?

Viewed 19

Hi all I am trying to make one API using swagger i.e. yaml file which give me output

{
calls: 1000,
msgs: 200,
successCalls: 800,
failedCalls: 200
}

I have one main.py which have dictionary as {calls: 1000, message: 200, successCalls: 800, failedCalls: 200} this main.py will invoke API.

This API must return these dictionary values after hitting GET, How to create YAML(api in swagger) and python script for it

What I have done -->

python code

from flask import Flask, request
from flasgger import Swagger, LazyString, LazyJSONEncoder
from flasgger import swag_from

app = Flask(__name__)
app.json_provider_class = LazyJSONEncoder
swagger_template = dict(
    info={
        'title': LazyString(lambda: 'My first Swagger UI document'),
        'version': LazyString(lambda: '0.1'),
        'description': LazyString(
            lambda: 'This document depicts a      sample Swagger UI document and implements Hello World functionality '
                    'after executing GET.'),
    },
    host=LazyString(lambda: request.host)
)
swagger_config = {
    "headers": [],
    "specs": [
        {
            "endpoint": 'hello_world',
            "route": '/hello_world.json',
            "rule_filter": lambda rule: True,
            "model_filter": lambda tag: True,
        }
    ],
    "static_url_path": "/flasgger_static",
    "swagger_ui": True,
    "specs_route": "/apidocs/"
}
swagger = Swagger(app, template=swagger_template,
                  config=swagger_config)


@swag_from("hello_world.yml", methods=['GET'])
@app.route("/")
def hello_world():
    return "Hello World!!!"


if __name__ == '__main__':
    app.run()

yaml code -->

openapi: 3.0.0
tags:
  - name: Hello World
get:
  
  description: None
responses:
    '200':
      description: Successful response
    '400':
      description: Bad Request
    '500':
      description: Internal Server Error

Can someone pls help me to fullfill the above requirement --> to fetch dictionary values from swagger GET api using python.

0 Answers
Related