NLDS API-server
The core content of the NLDS API-server run using FastAPI.
The Publisher class
- class nlds.rabbit.publisher.RabbitMQPublisher(name='publisher', setup_logging_fl=False)
Bases:
object
- classmethod create_log_message(message: str, target: str, route: Optional[str] = None) Dict[str, Any]
Create logging message to send to rabbit exchange. Message is, as with the standard message, in json format with metadata described in DETAILS and data, i.e. the log message, under DATA.
- Parameters
message (str) – ID of transaction as provided by fast-api
target (str) – The target log file to be written to, must be set to the name of a particular microservice, usually the one which instigated the log event
route (str) – Route that the message has taken. Optional, will be set to target if not specifed.
- Returns
JSON encoded string in the proper format for message passing
- declare_bindings() None
Go through list of exchanges from config file and declare each. Will also declare delayed exchanges for use in scheduled messaging if the delayed flag is present and activated for a given exchange.
- get_retry_delay(retries: int)
Simple convenience function for getting the delay (in seconds) for an indexlist with a given number of retries. Works off of the member variable self.retry_delays, maxing out at its final value i.e. if there are 5 elements in self.retry_delays, and 7 retries requested, then the 5th element is returned.
- publish_message(routing_key: str, msg_dict: Dict, exchange: Dict = None, delay: int = 0, properties: pika.spec.BasicProperties = None, mandatory_fl: bool = True, correlation_id: str = None) None
Sends a message with the specified routing key to an exchange for routing. If no exchange is provided it will default to the first exchange declared in the server_config.
An optional delay can be added which will force the message to sit for the specified number of seconds at the exchange before being routed. Note that this only happens if the given (or default if not specified) exchange is declared as a x-delayed-message exchange at start up with the ‘delayed’ flag.
This is in essence a light wrapper around the basic_publish method in pika.
- setup_logging(enable: bool = True, log_level: Optional[str] = None, log_format: Optional[str] = None, add_stdout_fl: bool = False, stdout_log_level: Optional[str] = None, log_files: Optional[List[str]] = None, log_max_bytes: Optional[int] = None, log_backup_count: Optional[int] = None) None
Sets up logging for a publisher (i.e. the nlds-api server) using a set number of configuration options from the logging interface. Each of the configuration options are able to be overridden by kwargs, allowing for child classes (i.e. consumers) to implement their own settings.
Allows the creation of stderr and stdout handlers under the generic global logger, and file specific handlers/loggers under the name of each output file - intended to be used for tracking each consumer’s output on the logging consumer.
- Parameters
enable (bool) – Whether to activate the logging functionality.
log_format (str) – The format string of the logging output, as per instructions in logging docs. Controls all logging outputs (stderr, stdout, files).
log_level (str) – The logging level of the global logger (applies to only the stderr stream)
add_stdout_fl (bool) – Boolean flag for controlling whether the global logger also prints to stdout.
stdout_log_level (str) – Logging level of the stdout logging stream
log_files (list[str]) – List of files to write logging output to, with each made in its own logger object referencable by the file name.
- static verify_exchange(exchange)
Verify that an exchange dict defined in the config file is valid. Throws a ValueError if not.
The routers
The routers control the endpoints of the API, and are the backbone of the nlds-server API. They are documented automatically (thanks to Swagger UI) and instructions on accessing these can be found in ../home.
The authenticators
- class nlds.authenticators.base_authenticator.BaseAuthenticator
- authenticate_group(oauth_token: str, group: str)
Validate whether the Bearer of the token belongs to the group.
- authenticate_token(oauth_token: str)
Validate an oauth token.
- authenticate_user(oauth_token: str, user: str)
Validate whether the Bearer of the token is a valid user.
- class nlds.authenticators.jasmin_authenticator.JasminAuthenticator
- authenticate_group(oauth_token: str, group: str)
Make a call to the JASMIN services for a user to determine whether the user with the token is part of the requested group.
- authenticate_token(oauth_token: str)
Make a call to the JASMIN token introspection to determine whether the token is a true token and whether it is valid.
- authenticate_user(oauth_token: str, user: str)
Make a call to the JASMIN services for a user to determine whether the user with the token is a valid user.
Authenticate methods also contains 3 general methods, used by the above 2 modules, to validate the given user, group and token.
The server config module
- nlds.server_config.load_config(config_file_path: str = '/etc/nlds/server_config') dict
Config file for the server contains authentication and rabbitMQ sections, the required contents of which are set by the schema in utils.constants. This function opens the config file (at a preset, configurable location) then verifies it.
- Parameters
config_file_path –
- nlds.server_config.validate_config_file(json_config: dict) None
Validate the JSON config file matches the schema defined in nlds_setup. Currently only checks that required headings and subheadings exist, i.e. only scans one layer deep and does no value checking.
- Parameters
json_config – Config file loaded using json.load()