Using third-party Libraries

Our transaction history is taking shape, but those date strings don’t look very good. Python has full-featured datetime formatting built in, but it’s pretty complicated, so many developers choose more user-friendly, third-party libraries for formatting dates. Since the Caliper SDK can do anything Python can do, we can use them as well.

Note

When a new dependency is added on a remote server, the cache needs to be refreshed in PyCharm. File -> Invalidate Caches / Restart...

Installing a library locally is easy. Using the add_dependency entrypoint, you can download and install any of the thousands of libraries in the python registry in a single command.

In your terminal, run the following command inside your extension’s top-level directory:

$ q2 add_dependency arrow

This will install “arrow”, a popular third-party library for handling dates and times. This library will allow us to modify our dates into something more readable in just a few lines of code. Add this bit to your for loop responsible for converting the transactions into readable models:

import arrow
...
for transaction in hq_response.result_node.Data.AllHostTransactions.Transactions:
        date_string = str(transaction.PostDate)
        arrow_date_object = arrow.get(date_string)
        display_date = arrow_date_object.format('MMMM DD, YYYY')
...

Here, we tell arrow the relevant piece of the date string we want is in the ‘YYYY-MM-DD’ format, then ask it to reformat it in a human readable style. That’s all it takes. Here it is in context of the full method. Don’t forget to import arrow at the top of the file.:

async def submit(self):
    params_obj = GetAccountHistoryById.ParamsObj(
        self.logger,
        self.form_fields['account_id'],
        '',
        hq_credentials=self.hq_credentials
    )

    hq_response = await GetAccountHistoryById.execute(params_obj)

    transaction_models = []

    for transaction in hq_response.result_node.Data.AllHostTransactions.Transactions:
        date_string = str(transaction.PostDate)
        arrow_date_object = arrow.get(date_string)
        display_date = arrow_date_object.format('MMMM DD, YYYY')

        display_amount = str(transaction.TxnAmount)
        is_credit = (transaction.SignedTxnAmount > 0)
        display_description = str(transaction.Description)

        transaction_models.append({
            'display_date': display_date,
            'display_amount': display_amount,
            'is_credit': is_credit,
            'display_description': display_description
        })

    template = self.get_template(
        'transaction_history.html.jinja2',
        {
            'header': 'Transaction History',
            'transactions': transaction_models,
            'current_account_id': self.form_fields['account_id'],
        }
    )

    html = self.get_tecton_form(
        "Transaction History",
        custom_template=template,
        hide_submit_button=True
    )

    return html

Note

Full Arrow Documentation.

Looks much better now, doesn’t it?

There are thousands of useful, high-quality open source libraries available to the Python community. The ability to leverage this vast trove is a large part of what makes the Caliper SDK so powerful, so explore and see what’s out there.

Next, we will further explore the rich networking capabilities of the Caliper SDK by making some requests to a third-party service.