Как Swagger API реализовать сокол? - PullRequest
0 голосов
/ 21 марта 2019

Я попытался Swagger API, чтобы успешно завершить его. Работа для Crud Operation.but я попытался реализовать Rediscache в Swagger API, но yml файл его маршрутизации, чтобы получить метод его ошибка для (ValueError: недостаточно значений для распаковки (ожидается 2, получил1)). Как исправить эту ошибку Я добавил Swagger yml-файл и демонстрационный .py-файл. Также можно проверить мой код. Правильно или нет. Или Может передать любую документацию для Swagger для python, используя redis и токен авторизации.

Swagger.yml

swagger: '2.0'
info:
title: FALSY SIMPLE DEMO API
version: "0.1"
consumes:
- application/json
produces:
- application/json
basePath: "/v1"

schemes:
  # tip: remove http to make production-grade
  - http
  - https
paths:
  '/hello':
    #x-swagger-router-controller: demo
    get:
      tags: [Method]
      operationId: get_it
      summary: testing
      parameters:
        - name: name
          in: query
          type: string
          required: false
      response:
        "200":
          description: Success
          schema:
            $ref: "#/definitions/MovieListBody"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
    #x-swagger-router-controller: demo
    post:
      tags: [Method]
      operationId: demo.create
      summary: testing
      parameters:
        - name: name
          required: true
          in: body
          description: a new movie details
          schema:
            $ref: "#/definitions/MovieBody"
      response:
        "200":
          description: Success
          schema:
            $ref: "#/definitions/MovieListBody"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"        
  /hello/{id}:
    get:
      tags: [Method]
      operationId: demo.show
      summary: testing
      parameters:
        - name: id
          required: true
          in: path
          description: get particular movie details
          type: string
      responses:
        "200":
          description: Sucess
          schema:
            $ref: "#/definitions/MovieBody"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
    put:
      tags: [Method]
      operationId: demo.update
      summary: testing
      parameters:
        - name: id
          required: true
          in: path
          type: string
        - name: movie
          required: true
          in: body
          description: an updated movie details
          schema:
            $ref: "#/definitions/MovieBody"
      responses:
        "200":
          description: Sucess
          schema:
            type: string
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
    delete:
      tags: [Method]
      operationId: demo.delete
      summary: testing
      parameters:
        - name: id
          required: true
          in: path
          description: remove single record in db
          type: string
      responses:
        "200":
          description: Sucess
          schema:
            type: string
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"

definitions:
  MovieListBody:
    required:
      - id
      - movies
    properties:
      id:
        type: integer
      movies:
        type: array
        items:
          $ref: "#/definitions/Movie"
  Movie:
    required:
      - title
      - gener
      - year
    properties:
      title:
        type: string
      gener:
        type: string
      year:
        type: integer

  MovieBody:
    required:
      - movies
    properties:
      movies:
          $ref: "#/definitions/Movie"

  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

Demo.py

from pymongo import MongoClient
import falcon
import json
from bson import json_util
from bson.json_util import dumps
from helper_modules import cache
from settings import CACHES

class Swagger_Falcon(object):
    def __init__(self,database,cache):
        self.db = database
        self.cc = cache


    def get_it(self,name):
        print("hello")
        # try:
        #     if 'products' in self.cc:
        #         data = self.cc.get('products')
        #     else:
        #         query = self.db.all_data()
        #         data = dumps(query)
        #         self.cc.set('products', data, ex=CACHES['default']['TIMEOUT'])
        # except Exception as ex:
        #     print(ex)
        return "hai"


    def create(self,name):
        print("Hello")
        client = MongoClient('localhost', 27017)
        db = client.olc_prod_db
        collection = db.Ecommerce
        data = json.loads(name)
        query = collection.insert_one(data)
        print(query)
        return "Sucessfully Inserted data"

    def show(self,id):
        print("Geting")
        client = MongoClient('localhost', 27017)
        db = client.olc_prod_db
        collection = db.Ecommerce
        query = collection.find({"id":int(id)})
        data = dumps(query)
        return data

    def update(self,movie,id):
        print("Updated")
        data = json.loads(movie)
        client = MongoClient('localhost', 27017)
        db = client.olc_prod_db
        collection = db.Ecommerce
        query = collection.update_one({'id':int(id)},{ "$set":{"movies":{"title":data["movies"]["title"],"gener":data["movies"]["gener"],"year":data["movies"]["year"]}}})
        return "Updated Sucessfully"


    def delete(self,id):
        print("Deleted")
        client = MongoClient('localhost', 27017)
        db = client.olc_prod_db
        collection = db.Ecommerce
        query = collection.remove({"id":int(id)})
        return "Deleted Sucessfully"








    # print(query)
    # return posted_data
...