Files
connexion/examples/restyresolver/api/pets.py
Robbe Sneyders 073f0d446e Update examples for Connexion 3.0 (#1615)
This PR updates the examples for Connexion 3.0 and merges them for
OpenAPI and Swagger.

2 examples required some changes to make them work:
- The reverse proxy example required some fixes to the
SwaggerUIMiddleware to leverage the `root_path` correctly. This is
included in the PR.
- The enforced defaults example requires the json validator to adapt the
body and pass it on. We currently pass on the original body after
validation, and I'm not sure if we should change this. I'll submit a
separate PR to discuss this.
2022-12-30 20:34:19 +01:00

52 lines
1.0 KiB
Python

import datetime
from connexion import NoContent
pets = {}
def post(body):
name = body.get("name")
tag = body.get("tag")
count = len(pets)
pet = {}
pet["id"] = count + 1
pet["tag"] = tag
pet["name"] = name
pet["last_updated"] = datetime.datetime.now()
pets[pet["id"]] = pet
return pet, 201
def put(body):
id_ = body["id"]
name = body["name"]
tag = body.get("tag")
id_ = int(id_)
pet = pets.get(id_, {"id": id_})
pet["name"] = name
pet["tag"] = tag
pet["last_updated"] = datetime.datetime.now()
pets[id_] = pet
return pets[id_]
def delete(id_):
id_ = int(id_)
if pets.get(id_) is None:
return NoContent, 404
del pets[id_]
return NoContent, 204
def get(petId):
id_ = int(petId)
if pets.get(id_) is None:
return NoContent, 404
return pets[id_]
def search(limit=100):
# NOTE: we need to wrap it with list for Python 3 as dict_values is not JSON serializable
return list(pets.values())[0:limit]