-
-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added MethodViewResolver By subclassing RestyResolver and modifying its `resolve_function_from_operation_id` method, it is now possible to use automatic routing functionality with Flask's MethodView together with MethodViewResolver. * Add MethodView example * Add tests for methodview * add documentation on how to use MethodViewResolver
- Loading branch information
Showing
10 changed files
with
624 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
===================== | ||
MethodViewResolver Example | ||
===================== | ||
|
||
Running: | ||
|
||
.. code-block:: bash | ||
$ ./app.py | ||
Now open your browser and go to http://localhost:9090/v1.0/ui/ to see the Swagger UI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .petsview import PetsView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import datetime | ||
|
||
from connexion import NoContent | ||
from flask import request | ||
from flask.views import MethodView | ||
|
||
|
||
class PetsView(MethodView): | ||
""" Create Pets service | ||
""" | ||
method_decorators = [] | ||
pets = {} | ||
|
||
def post(self): | ||
body= request.json | ||
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 = request.json | ||
name = body["name"] | ||
tag = body.get("tag") | ||
id_ = int(petId) | ||
pet = self.pets.get(petId, {"id": id_}) | ||
pet["name"] = name | ||
pet["tag"] = tag | ||
pet['last_updated'] = datetime.datetime.now() | ||
self.pets[id_] = pet | ||
return self.pets[id_], 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): | ||
id_ = int(petId) | ||
if self.pets.get(id_) is None: | ||
return NoContent, 404 | ||
return self.pets[id_] | ||
|
||
def search(self, limit=100): | ||
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python | ||
import logging | ||
|
||
import connexion | ||
from connexion.resolver import MethodViewResolver | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
if __name__ == '__main__': | ||
app = connexion.FlaskApp(__name__, specification_dir='openapi/', debug=True) | ||
|
||
options = {"swagger_ui": True} | ||
app.add_api('pets-api.yaml', | ||
options=options, | ||
arguments={'title': 'MethodViewResolver Example'}, | ||
resolver=MethodViewResolver('api'), strict_validation=True, validate_responses=True ) | ||
app.run(port=9090) |
Oops, something went wrong.