Base Request Handler

Specific abilities

  • self.rate_limiters

    • Array of q2_sdk.core.RateLimiter instances. Allows for limiting traffic to the extension either by ip (segment_by_ip=True) or globally (segment_by_ip=False)

    • Useful for mitigating Distributed Denial of Service (DDoS) attacks

class q2_sdk.core.http_handlers.base_handler.Q2BaseRequestHandler(application, request, **kwargs)[source]

Bases: RequestHandler

Inherits from Tornado’s RequestHandler, but adds a few Q2 specific niceties.

  • Handles both gets and posts

  • If REQUIRED_CONFIGURATIONS is set, ensures the entries are set in the extension’s settings file or the webserver will not start.

    REQUIRED_CONFIGURATIONS is a dictionary of key value pairs where the key is the required name and the value is the default value when q2 generate_config is called

  • If DYNAMIC_CORE_SELECTION is set to True, will prompt for a Core name on installation

  • OPTIONAL_CONFIGURATIONS work the same was as REQUIRED_CONFIGURATIONS, but will not stop the server from running if omitted

  • FRONTEND_OVERRIDE_EXTENSION allows using another extension as the frontend for this one

data_received(_)[source]

Implement this method to handle streamed request data.

Requires the stream_request_body decorator.

May be a coroutine for flow control.

async run_async(funcname, *args, **kwargs)[source]

Easy way to run a non_async function in a background thread

Parameters:
  • funcname (Callable) – Function reference (do not instantiate)

  • args – Positional parameters

  • kwargs – Keyword parameters

Usage:

def non_async_func(foo, spam="eggs"):
    time_consuming_thing()

await self.run_async(non_async_func, 1, spam="eggs")
property core: BaseCore

Instantiates a communication object between this extension and the core listed in configuration.settings.CORE Must pip install q2_cores or populate settings.CUSTOM_CORES for this to be accessible

Returns:

Subclass of q2_sdk.models.cores.BaseCore

property cache: Q2CacheClient[Q2CacheDefaultReturn]

Instantiates a communication object between this extension and the pymemcache library. Cache can be configured via configuration.settings.CACHE.

This is a factory that instantiates the cache in self.default_cache_level. There is also self.stack_cache, self.service_cache, or self.session_cache for direct access based on your context.

property service_cache: Q2CacheClient[Q2CacheDefaultReturn][source]

Q2CacheClient scoped to the loadbalanced running service.

property stack_cache: Q2CacheClient[Q2CacheDefaultReturn][source]

Q2CacheClient scoped to the current financial institution stack.

property session_cache: Q2CacheClient[Q2CacheDefaultReturn][source]

Same as self.cache but limited to online user session

get_cache(prefix=None, **kwargs)[source]
Parameters:

prefix – If defined will be prepended to all keys

Return type:

Q2CacheClient[TypeAliasType]

property sftp_dir: Path

Default location for SFTP mounts. Is a Path, which can be used as a cleaner way to access directories on the file system.

Usage:

from q2_sdk.core.q2_open import q2_open

async with q2_open(self.sftp_dir / "Inbound/MyReport.csv", "r") as handle:
    report_data = await handle.read()
return report_data
property base_assets_url

Url to use for creating a fully qualified URL to your extension assets

async get_ui_text(language=None, ui_selection=None, cache_time=60)[source]

Returns any UI Text elements from the database that were inserted as part of self.DB_PLAN

Return type:

dict

async prepare()[source]

Fires before any request handling code

By default, will check any Rate Limiter instances in self.rate_limiters and disallow further processing if any are over their specified limit

on_finish()[source]

Fires as the request is ending

log_exception(typ, value, tb)[source]

Add exception info to the open telemetry span. Automatically called by tornado in the event of an unhandled exception.

Return type:

None

log_exception_to_otel()[source]

Convenience method for logging the current handled exception to the open telemetry span.

Return type:

None

get_template(template_name, replacements_dict)[source]

Used for loading static blocks of text with substitutions. Great for storing large HTML blocks in separate files. Uses jinja behind the scenes (http://jinja.pocoo.org/)

Will search in all templates folders starting with the calling extension, then moving on to the parent class, and so on, preferring the lowest.

Parameters:
  • template_name (str) – file_name including suffix. ex. initial.html

  • replacements_dict (dict) – Key value pairs to replace within template_name

Return type:

str

Returns:

Body of template_name file replaced with values in replacements_dict