-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Davies
committed
Dec 18, 2018
1 parent
3b478ea
commit 2b2c180
Showing
10 changed files
with
244 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
__version__ = '0.1.0a7' | ||
__version__ = '0.1.0a8' | ||
|
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,67 @@ | ||
import importlib.util | ||
|
||
|
||
class Method(object): | ||
def __init__(self, file, path, resource, **options): | ||
self.file = file | ||
self.path = path | ||
self.code = resource | ||
self.options = options | ||
|
||
def get_resource(self): | ||
_cls = self.code.split('.')[-1] | ||
spec = importlib.util.spec_from_file_location(self.code, self.file) | ||
module = importlib.util.module_from_spec(spec) | ||
spec.loader.exec_module(module) | ||
return getattr(module, _cls) | ||
|
||
@property | ||
def swagger_definition(self): | ||
extra_swagger = self.options.get('swagger', {}) | ||
|
||
res = self.get_resource() | ||
auth_type = self.options.get('auth_type') | ||
swagger = {} | ||
|
||
get = getattr(res, 'get', None) | ||
post = getattr(res, 'post', None) | ||
put = getattr(res, 'put', None) | ||
delete = getattr(res, 'delete', None) | ||
|
||
obj = { | ||
'tags': [self.code], | ||
'summary': 'Custom function', | ||
'consumes': 'application/json', | ||
'produces': 'application/json', | ||
'responses': { | ||
'200': { | ||
'description': 'successful operation', | ||
} | ||
}, | ||
'security': [ | ||
{ | ||
'basicAuth': [] | ||
} | ||
] if auth_type else [] | ||
} | ||
|
||
if get: | ||
swagger['get'] = obj | ||
for key, val in extra_swagger.get('get', {}).items(): | ||
obj[key] = val | ||
if post: | ||
swagger['post'] = obj | ||
for key, val in extra_swagger.get('post', {}).items(): | ||
obj[key] = val | ||
if put: | ||
swagger['put'] = obj | ||
for key, val in extra_swagger.get('put', {}).items(): | ||
obj[key] = val | ||
if delete: | ||
swagger['delete'] = obj | ||
for key, val in extra_swagger.get('delete', {}).items(): | ||
obj[key] = val | ||
|
||
return swagger | ||
|
||
|
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
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,96 @@ | ||
Adding additional methods to your API | ||
------------------------------------- | ||
|
||
You can very easily extend your Crudcast project by creating additional API methods from your own code. This is done | ||
by adding a `methods:` section to your config file | ||
|
||
.. code-block:: yaml | ||
methods: | ||
- path: say-hello/<string:arg1> | ||
resource: HelloResource | ||
file: hello.py | ||
The above will look for a class called `HelloResource` in the file `hello.py`, and insert this method into your API | ||
at the provided path | ||
|
||
.. note:: | ||
The `file` attribute can be relative, as shown above, but its generally best to provide the absolute path to this | ||
file, especially if you're using a custom config file location | ||
|
||
Writing methods | ||
--------------- | ||
|
||
Your custom resources must subclass the `crudcast.resources.Resource` class, which itself extends the | ||
`flask_restplus.Resource` object. | ||
|
||
.. code-block:: python | ||
from crudcast.resources import Resource | ||
class HelloResource(Resource): | ||
def get(self, arg1): | ||
return {'hello': arg1} | ||
Request types | ||
------------- | ||
|
||
You can also specify multiple method types in the same resource, as follows: | ||
|
||
.. code-block:: python | ||
from crudcast.resources import Resource | ||
from flask import request | ||
class HelloResource(Resource): | ||
def get(self, arg1): | ||
return {'hello': arg1} | ||
def post(self, arg1): | ||
# do something | ||
return request.json | ||
Additionally, it is also possible to modify data in your MongoDB collections in your own methods, by using the | ||
`Resource` object's `app` property. For example: | ||
|
||
.. code-block:: yaml | ||
book: | ||
fields: | ||
name: | ||
published: | ||
type: boolean | ||
methods: | ||
- path: books/<string:_id>/publish | ||
resource: MyBooks | ||
file: books.py | ||
.. code-block:: python | ||
from crudcast.resources import Resource | ||
from pymongo.collection import ObjectId | ||
class MyBooks(Resource): | ||
def get(self, _id): | ||
collection = self.app.db['books'] | ||
collection.find_one_and_update({'_id': ObjectId(_id)}, {'$set': {'published': True}}) | ||
return {'updated': True} | ||
Swagger | ||
------- | ||
|
||
As with other methods, your own custom methods will be added to the swagger view. You can modify the information | ||
displayed there by adding a `swagger:` property to the config for your method: | ||
|
||
.. code-block:: yaml | ||
methods: | ||
- path: books/<string:_id>/publish | ||
resource: MyBooks | ||
file: books.py | ||
swagger: | ||
summary: Mark a book as published | ||
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