API - Resource Manager
- flask_restaction.api
-
- abort(code, error=None, message=None)
-
Abort with suitable error response
- Args:
code (int): status code
error (str): error symbol or flask.Response
message (str): error message
- export(rv, code=None, headers=None)
-
Create a suitable response
- Args:
rv: return value of action
code: status code
headers: response headers
- Returns:
flask.Response
- get_request_data()
-
Get request data based on request.method
If method is GET or DELETE, get data from request.args
If method is POST, PATCH or PUT, get data from request.form or request.json
- get_title(desc, default=None)
-
Get title of desc
- parse_docs(docs, marks)
-
Parse YAML syntax content from docs
If docs is None, return {}
If docs has no YAML content, return {"$desc": docs}
Else, parse YAML content, return {"$desc": docs, YAML}- Args:
docs (str): docs to be parsed
marks (list): list of which indicate YAML content starts
- Returns:
A dict contains information of docs
- parse_request()
-
Parse request endpoint and return (resource, action)
- unpack(rv)
-
Convert rv to tuple(data, code, headers)
- Args:
rv: data or tuple that contain code and headers
- Returns:
tuple (rv, status, headers)
Manager of Resource
- class Api(self, app, validators=None, metafile=None, docs='')
-
- Args:
app: Flask or Blueprint
validators (dict): custom validators
metafile (str): path of metafile
docs (str): api docs
- Attributes:
validators (dict): custom validators
meta (dict): metadata of api
- add_resource(self, resource, *class_args, **class_kwargs)
-
Add resource
Parse resource and it's actions, route actions by naming rule.
- Args:
resource: resource class
class_args: class_args
class_kwargs: class_kwargs
- after_request(self, f)
-
Decorater
- authorize(self, role)
-
Check permission
- before_request(self, f)
-
Decorater
- error_handler(self, f)
-
Decorater
- make_action(self, fn, schema_parser, meta)
-
Make resource's method an action
Validate input, output by schema in meta.
If no input schema, call fn without params.
If no output schema, will not validate return value.- Args:
fn: resource's method
schema_parser: for parsing schema in meta
meta: meta data of the action
- make_view(self, action_group)
-
Create a view function
Check permission and Dispatch request to action by request.method
- meta_view(self)
-
Meta data / API document
By default, this view func will return API document(HTML),
you can set request headerAccept
toapplication/json
or set query stringjson
to get meta data(JSON).
Token based authorize and permission control
- class TokenAuth(self, api)
-
- after_request(self, rv, status, headers)
- before_request(self)
- calculate_expiration(self, token)
-
Calculate token expiration
return expiration if the token need to set expiration or refresh,
otherwise return None.- Args:
token (dict): a decoded token
- decode_token(self, token)
-
Decode Authorization token, return None if token invalid
- encode_token(self, token)
-
Encode Authorization token, return bytes token
- generate_headers(self, token)
-
Generate auth headers
- get_role(self, f)
-
Decorater for register get_role_func
A tool for calling API
- class Res(self, url_prefix='', test_client=None, auth_header='Authorization')
-
Will keep a session and handle auth token automatic
Usage:
>>> res = Res(test_client=app.test_client) # used in testing >>> res = Res("http://127.0.0.1:5000") # request remote api >>> res.ajax("/hello") {'message': 'Hello world, Welcome to flask-restaction!'} >>> res.hello.get() {'message': 'Hello world, Welcome to flask-restaction!'} >>> res.hello.get({"name":"kk"}) {'message': 'Hello kk, Welcome to flask-restaction!'} >>> res.xxx.get() ... requests.exceptions.HTTPError: 404 Client Error: NOT FOUND for url: http://127.0.0.1:5000/xxx
- Args:
url_prefix: url prefix of API
auth_header: auth header name of API
- Attributes:
url_prefix: url prefix
auth_header: auth header
session: requests.Session or TestClientSession
- ajax(self, url, method='GET', data=None, headers=None)
-
Send request
- flask_restaction.exporters
-
- export_json(data, status, headers)
-
Creates a JSON response
JSON content is encoded by utf-8, not unicode escape.
- Args:
data: any type object that can dump to json
status (int): http status code
headers (dict): http headers
- exporter(mediatype)
-
Decorater for register exporter
- Args:
mediatype: mediatype, eg:
application/json
- register_exporter(mediatype, fn)
-
Register exporter
- Args:
mediatype: mediatype, eg:
application/json
fn: exporter function