Creating a new endpoint in a Swagger OpenAPI application


We are using the code from this link: Demo of Swagger OpenAPI Specs using Python 

We will change the following files:

1. ...\swagger_hello_world\api\api_endpoints.py

import connexion
import logging
from flask import Response
import json

class MyEndpoints(object):

    def __init__(self):
        self.logger=logging.getLogger("MyEndpoints")
        self.logger.debug('From __init__()')
        
    def get_hello_world(self):
        req_data = connexion.request.get_json()
        data = req_data.get('msg')
        return Response(json.dumps("msg: " + str(data)), status=201, mimetype='application/json')

    def post_hello_world(self):
        req_data = connexion.request.get_json()
        data = req_data.get('msg')
        return Response(json.dumps("msg: " + str(data)), status=201, mimetype='application/json')

    def post_new_endpoint(self):
        req_data = connexion.request.get_json()
        data = req_data.get('new_msg')
        return Response(json.dumps("new_msg: " + str(data)), status=201, mimetype='application/json')


class_instance = MyEndpoints()

2. ...\swagger_hello_world\swagger\sw_conf.yaml

swagger: '2.0'

info:
  title: {{ title }}
  description: Hello world microservice.
  version: "0.1"

consumes:
  - application/json
produces:
  - application/json

basePath: /v1.0
tags:
  - name: "na"
    description: "NA"
schemes:
  - "http"
paths:
  /get_test:
    get:
      operationId: api.api_endpoints.class_instance.get_hello_world
      summary: Get Hello World
      parameters:
        - in: body
          name: my_data
          description: NA.
          schema:
            type: object
            items:
              $ref: '#/definitions/my_data'
      responses:
        200:
          description: successful return
          schema:
            type: string
  /post_test:
    post:
      operationId: api.api_endpoints.class_instance.post_hello_world
      summary: Post Hello World
      parameters:
        - in: body
          name: my_data
          description: NA.
          schema:
            type: object
            items:
              $ref: '#/definitions/my_data'
      responses:
        200:
          description: successful return
          schema:
            type: string

  /new_endpoint:
    post:
      operationId: api.api_endpoints.class_instance.post_new_endpoint
      summary: New Endpoint
      parameters:
        - in: body
          name: my_new_data
          description: NA.
          schema:
            type: object
            items:
              $ref: '#/definitions/my_new_data'
      responses:
        200:
          description: successful return
          schema:
            type: string 


definitions:
  my_data:
    type: object
    properties:
      msg:
        type: string

  my_new_data:
    type: object
    properties:
      new_msg:
        type: string 


3. ...\swagger_hello_world\my_client.py
import requests
headers = {'content-type': 'application/json'}

URL = "http://127.0.0.1:8083/v1.0/post_test"
r = requests.post(url = URL, data = {}, json = { "msg": "Post Holla" }, headers = headers)
print("Response text: " + r.text)

URL = "http://127.0.0.1:8083/v1.0/get_test"
r = requests.get(url = URL, data = {}, json = { "msg": "Get Holla" }, headers = headers)
print("Response text: " + r.text)

URL = "http://127.0.0.1:8083/v1.0/new_endpoint"
r = requests.post(url = URL, data = {}, json = { "new_msg": "Get New Holla" }, headers = headers)
print("Response text: " + r.text)


4.A. Server Logs:
(swagger) C:\Users\ashish\Desktop\swagger_hello_world>python hw_app.py
init.py
ENV:  PRODUCTION
'formatters'
 * Serving Flask app "hw_app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:8083/ (Press CTRL+C to quit)
127.0.0.1 - - [04/Feb/2019 16:38:37] "POST /v1.0/post_test HTTP/1.1" 201 -
127.0.0.1 - - [04/Feb/2019 16:38:37] "GET /v1.0/get_test HTTP/1.1" 201 -
127.0.0.1 - - [04/Feb/2019 16:38:37] "POST /v1.0/new_endpoint HTTP/1.1" 201 -

4.B. Client Logs:
(base) C:\Users\ashish\Desktop\swagger_hello_world>python my_client.py
Response text: "msg: Post Holla"
Response text: "msg: Get Holla"
Response text: "new_msg: Get New Holla"

Link to code on Google Drive

No comments:

Post a Comment