Connections
Connections allow you to take data from different data sources (databases, REST APIs, Excel files etc.) and read that data in real time during contract execution. Building connections typically involves:
- Defining
Connection
s in a template which describe some data you want to be injected into the contract at runtime. - Defining clauses in a template which read data from a
Connection
and do something useful with it. - Building connections in the UI which can read and validate data from various
sources & make it available to a contract. Connections can have placeholders
to enable them to be customised when using them (for example, a stock price
connection may have a placeholder for
ticker
).
When you create a contract in the UI, if the chosen template has Connection
s
defined, then you will need to map connections set up in the UI to supply data
for each one. Alternatively, instead of using a live connection the user can
enter a value to return whenever the Connection
is read.
Example template
Imagine we are creating a template for contracts which make needs two things: a live stock price, and the credit score of a client. We could write a template like this:
from decimal import Decimal
from luna import Connection, Parameter, Template, clause
class MyTemplate(Template):
stock_price = Connection(type_=Decimal)
credit_score = Connection(type_=int)
stock_price_threshold = Parameter(type_=Decimal)
@clause
def do_something(self):
stock_price = self.stock_price.read()
if stock_price > self.stock_price_threshold:
if self.credit_score.read() > 700:
# Do something useful
The template does not contain any knowledge of where the data is actually sourced from; it simply declares its requirements, and the data sources are then mapped when creating the contract - which could be something like
- We map
stock_price
to a connection to an HTTP API - and this connection has a placeholder calledticker
which we set toMSFT
. The underlying connection works by calling a REST API, injecting the ticker into the URL to get the stock price we want, locating the price in the response data, turning it into aDecimal
which gets returned when callingself.stock_price.read()
. - We map
credit_score
to a connection to an Excel file of the customers data, which can read the latest credit score from within the file.