NovaAPI Core¶
A package to accelerate REST API development
-
nova_api.close_if_still_open(entity_dao: nova_api.dao.GenericDAO) → None[source]¶ Closes a DAO connection if it’s still open.
Parameters: entity_dao – DAO instance to be closed Returns:
-
nova_api.create_api_files(entity_class: Type[nova_api.entity.Entity], dao_class: Type[nova_api.dao.GenericDAO], version: str, *, overwrite: bool = False, auth_schema: int = None) → None[source]¶ Write api files for the entity informed with the dao_class informed.
Generated the api.py and api.yml with the informed entity, dao and version. If overwrite is false and files exist, no file will be generated. If overwrite is True and the files already exist, they’ll be replaced. Adds the Authorization schema informed.
Parameters: - entity_class – The entity to generate api files for.
- dao_class – The dao class for the entity
- version – The version of the api
- overwrite – Whether to overwrite existing files or not
- auth_schema – The authorization schema to apply to api methods.
Returns: None
-
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.format_parameter(field: dataclasses.Field) → str[source]¶ Formats the field for the query parameters in get_all function.
Parameters: field – The field of the dataclass Returns: The formatted param to the YAML Swagger file
-
nova_api.format_property(field: dataclasses.Field) → str[source]¶ Formats the field for the properties of Entity definition used in swagger.
Parameters: field – The field of the Entity Returns: The formatted property to the YAML Swagger file
-
nova_api.formatted_properties(entity_class: Type[nova_api.entity.Entity]) → str[source]¶ Returns the formatted properties of the entity for the #/definitions/Entity used in swagger.
Parameters: entity_class – The entity class Returns: Formatted properties of the entity
-
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.generate_base_api_for_entity(dao_class: Type[nova_api.dao.GenericDAO], entity_class: Type[nova_api.entity.Entity]) → str[source]¶ Generates the base API implementation for the dao_class and the entity.
Parameters: - dao_class – The dao class for the entity
- entity_class – The entity
Returns: The API implementation
-
nova_api.get_auth_schema_yml(schema: int = None) → Optional[str][source]¶ Returns the yml definition for the selected schema.
Parameters: schema – The identifier of the authorization schema. Returns: The yml definition for the schema
-
nova_api.get_parameter_format() → str[source]¶ Returns the base query parameter format for the YAML Swagger file.
Returns: The base format with the fields for the format function.
-
nova_api.get_python_api_filename(entity_lower: str) → str[source]¶ Returns the default API filename.
Parameters: entity_lower – The entity name in lower case. Returns:
-
nova_api.is_valid_auth_schema(auth: str) → bool[source]¶ Checks if the informed auth schema is valid.
Parameters: auth – The informed auth schema Returns: True if the schema is in the valid schemas and False otherwise
-
nova_api.python_api_exists(entity_lower: str) → bool[source]¶ Checks if the api implementation file exists with the default name.
Parameters: entity_lower – The entity name in lower case. Returns: True if the file exists, false otherwise
-
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: Type[nova_api.dao.GenericDAO], error_message: str = 'Error', 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
-
nova_api.write_api_implementation(api_implementation_file: str, dao_class: Type[nova_api.dao.GenericDAO], entity_class: Type[nova_api.entity.Entity]) → None[source]¶ Writes the api implementation for the entity and dao_class in the api_implementation_file
Parameters: - api_implementation_file – File name to write api implementation
- dao_class – The dao class for the entity
- entity_class – The entity class
Returns: None