Files
connexion/examples/methodresolver/api/petsview.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

68 lines
1.8 KiB
Python

import datetime
from connexion import NoContent
from flask.views import MethodView
def example_decorator(f):
"""
the returned view from <class>.as_view can be decorated
the decorator is initialized exactly once per class
"""
def decorator(*args, **kwargs):
return f(*args, **kwargs)
return decorator
class PetsView(MethodView):
"""Create Pets service"""
decorators = [example_decorator]
pets = {}
def __init__(self, pets=None):
# the args and kwargs can be provided
# via the MethodViewResolver's class_params dict
if pets is not None:
self.pets = pets
def post(self, body: dict):
name = body.get("name")
tag = body.get("tag")
count = len(self.pets)
pet = {}
pet["id"] = count + 1
pet["tag"] = tag
pet["name"] = name
pet["last_updated"] = datetime.datetime.now()
self.pets[pet["id"]] = pet
return pet, 201
def put(self, petId, body: dict):
name = body["name"]
tag = body.get("tag")
pet = self.pets.get(petId, {"id": petId})
pet["name"] = name
pet["tag"] = tag
pet["last_updated"] = datetime.datetime.now()
self.pets[petId] = pet
return self.pets[petId], 201
def delete(self, petId):
id_ = int(petId)
if self.pets.get(id_) is None:
return NoContent, 404
del self.pets[id_]
return NoContent, 204
def get(self, petId=None, limit=100):
if petId is None:
# NOTE: we need to wrap it with list for Python 3 as
# dict_values is not JSON serializable
return list(self.pets.values())[0:limit]
if self.pets.get(petId) is None:
return NoContent, 404
return self.pets[petId]