NovaAPI Core¶
A package to accelerate REST API development
-
nova_api.default_response(success: bool, status_code: int, message: str, data: dict) → flask.wrappers.Response[source]¶ Send a flask response with json payload in a default format
Example
JSON response
{ "sucess": True, "message": "API call OK", "data": { "num": "123" } }
Parameters: - success – bool that represents if the request was successfully processed.
- status_code – integer that represents the http status code of the response.
- message – summary string for the response.
- data – dictionary (json valid) with data to be sent in the response
Returns: a flask response with headers and status codes set
-
nova_api.error_response(status_code: int = 500, message: str = 'Error', data: dict = None) → flask.wrappers.Response[source]¶ Wrapper of default_response for error responses.
Calls default_response with status_code=500, message=Error and success=false passing data. Other status code and messages may be passed.
Parameters: - status_code – Integer that represents the http status code of the response.
- message – Summary string for the response.
- data – Dictionary (json valid) with data to be sent in the response
Returns: Default response with success=false
-
nova_api.generate_api()[source]¶ CLI interface for generate_nova_api. Generates API files.
- Must be called at least with -e <Entity>. Generates the api files with the arguments. Accepts the following arguments:
- -e: Name of the Entity, which must be the same of the file that contains it
- -d: Name of the Entity DAO class, which must be the same of the file that contains it. Only needs to be specified if it’s not EntityDAO. (Where Entity is the name of the entity passed to -e)
- -v: API version string. Will be used in the base path before the entity name
- -a: Authentication Schema, type of authentication which will be applied to endpoints in the generated API.
Returns: None.
-
nova_api.success_response(status_code: int = 200, message: str = 'OK', data: dict = None) → flask.wrappers.Response[source]¶ Wrapper of default_response for success responses.
Calls default_response with status_code=200, message=OK and success=true passing data. Other status code and messages may be passed.
Parameters: - status_code – Integer that represents the http status code of the response.
- message – Summary string for the response.
- data – Dictionary (json valid) with data to be sent in the response
Returns: Default response with success=true
-
nova_api.use_dao(dao_class: nova_api.dao.GenericDAO, error_message: str = 'Erro', dao_parameters: dict = None, retry_delay: float = 1.0, retries: int = 3)[source]¶ Decorator to handle database access in an API call
This decorator instantiates the DAO specified in dao_class within a try except block. If a exception is raised during the API call or database access, it generates an error_response with message=error_message and with the exception description in data. The DAO instance is passed to the decorated function as a keyword argument dao.
Parameters: - dao_class – DAO to instantiate and pass to the decorated function
- error_message – Default error message to send in the error_response if an exception is thrown
- dao_parameters – Parameters to add to the call to the DAO constructor. They’ll be added to the call with the expansion **dao_parameters.
- retries – Number of times to retry connection with database. Defaults to 3. May be set through the env variable NOVAAPI_RETRIES
- retry_delay – Seconds to wait before retrying to connect to database. Defaults to 1.0. May be set through the env variable NOVAAPI_RETRY_DELAY.
Returns: The decorated function