Configuration
The Q2RequestHandler features several methods of configuration that change the way it behaves and allows extension customization both in the code and in the database.
What if we wanted the name of our imaginary FI displayed on our AccountDashboard form, but the bosses are considering a name change? Text that could change is one simple use case for Required Configurations. Let’s add a Required Configurations object to our extension:
class AccountDashboardHandler(Q2TectonServerRequestHandler):
REQUIRED_CONFIGURATIONS = {
'fi_name': 'Imaginary FI of Texas'
}
...
To activate these configurations, use the CLI:
$ q2 generate_config AccountDashboard
These values will be added to a new settings file at configuration/AccountDashboard.py. The values here are defaults: if you change them in configuration/AccountDashboard.py, those values will be used instead.
The values are dynamically set as attributes in a Configuration object named config, and can be referenced using the key set in the REQUIRED_CONFIGURATIONS dictionary.
We can now display our FI name on our AccountDashboard form:
async def default(self):
...
html = self.get_tecton_form(
self.config.fi_name,
custom_template=template,
routing_key="submit"
)
return html
Our get_currency_rates
method requires an api key to talk to the local-dev-api curencyRates endpoint. We
might not want to hard code this value in the repository the way REQUIRED_CONFIGURATION is. If that key changes, we should
be able to update it without making a code change. We should update it via a database value instead.
We can store this value in the Q2 database using Wedge address configs.
Wedge address configs are named after the database table in which they are stored, and are a simple dictionary of key/value pairs. This feature is activated on our RequestHandler and takes effect after form installation.
Let’s add the api key for our third-party service in the previous step to our extension:
class AccountDashboardHandler(Q2TectonServerRequestHandler):
...
WEDGE_ADDRESS_CONFIGS = DbConfigList(
[
DbConfig('apikey', 'S6TczFkiWjDZy7FElvqKYMogzdcEcoQy')
]
)
...
Note
The DbConfig object has an optional parameter for required
. This defaults to True, but
if False will not raise an error if not present. It is recommended to set subsequent
DbConfigs to required=False after the initial install to allow a clean upgrade path for
deployments.
Remember to uncomment the DbConfig import line at the top of the file! This key/value pair will be added to the database when the form is installed with q2 install
.
Again, this value is only a default for use on installation: if it is changed later in the database, the database value will be provided to the RequestHandler instead. This way, the value can be updated without updating our extension code.
We’ll update the reference inside our get_currency_rates
method:
@cache(timeout=3600, key="quotes_dictionary")
async def get_currency_rates(self):
...
response = await q2_requests.get(
self.logger,
'https://local-dev-api.q2developer.com/projects/currencyRates',
headers={
'apikey': self.wedge_address_configs['apikey']
}
)
...
Before this takes effect, we need to update the form. That takes a single command:
$ q2 update_installed -e AccountDashboard
Don’t forget: the values we add to the RequestHandler are just defaults for the Caliper SDK to create when the relevant commands are run. At runtime, the values will be pulled from the settings.py file for Required Configurations, or the database for wedge address configs.
One more challenge before we take a step back and admire our work. Let’s improve the user experience a bit by remembering our user’s preferences.