我尝试过使用Swagger api使其成功运行以实现Crud Operation。但是我尝试在Swagger api中实现Rediscache,但是yml对其路由进行路由以获取方法的抛出错误(ValueError:没有足够的值要解压(预期2,得到1))。如何修复此错误,我还添加了Swagger yml文件和demo .py文件,还可以检查我的代码是否正确。或者可以使用Redis和auth令牌将Swagger实现的任何文档提供给python。
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