Writing a connector type¶
A connector type is a kind of connection: FlowAssembly’s own are rest (web
APIs), soap (SOAP web services), sql (databases), object_storage
(files in S3 or compatible stores), sftp (files on SFTP servers), rabbitmq
(messages to a queue), webhook (signed events), smtp (email) and
sequence (numbers it issues). When a project needs a
system none of them reaches, such as a core banking ledger with its own
protocol, your developers can write a type of your own. It runs in FlowAssembly’s
connector worker, alongside the built-in ones, and projects use it like any
other connection.
This guide is for the developers who write the type, and for the people who review and install it.
What you write¶
A Python package with one class per type. The class says:
its name (what a project writes as
type);which of an operation’s settings name case fields, so FlowAssembly can check and rename them. Each such setting may hold a field, a list of fields, or a mapping of the other system’s names to fields, such as
{CustomerName: customer_name};what else it checks in a connection;
how to make a call.
from flowassembly.connectors.sdk import Call, ConnectorFailure, ConnectorType, Context, Result
class Ledger(ConnectorType):
name = "ledger" # lower-case letters, digits and _
title = "Core banking ledger" # shown in the builder
reads = ("account", "amount") # settings naming case fields it sends
returns = ("returns",) # the setting naming fields it fills in
def check(self, definition): # the connection as a whole
return [] if definition.get("branch") else ["branch must name the posting branch"]
def check_operation(self, name, operation):
if operation.get("posting") not in ("debit", "credit"):
return [f"Operation '{name}': posting must be debit or credit"]
return []
def execute(self, call: Call, context: Context) -> Result:
host = call.binding.get("host") # where it goes, from the instance
...
return Result({"posting_reference": reference}, {"status": 200})
A project then has connectors/core_ledger.yaml:
type: ledger
branch: "042"
secretRef: ledger-api # the secret's value arrives as call.secret
operations:
pay_commission:
posting: credit
account: agent_account # a case field
amount: commission # a case field
returns: [posting_reference]
What a call gives you¶
call (a flowassembly.connectors.sdk.Call) holds:
|
the operation’s settings, from the project |
|
the whole connection, from the project |
|
this instance’s settings for the connection (connector bindings), such as a host; |
|
the case’s data |
|
the value of the connection’s |
|
the same on every try of one step: use it to make a repeat harmless |
|
which call this is |
context.files(field) gives the files in a case field, as (name, media type,
content). context.keep(field, name, content) keeps a file a call brought back,
checked as a file a person adds (type and size against the field, and the virus
scanner) and encrypted; put the reference it returns in the output, as
{field: [reference]}.
Return the fields you fill in, as a dict or a Result. A Result can also say
what to record about the call, such as a status; it goes in the step’s audit
record and logs, so never put a secret or case data in it. You may fill in only
the fields the operation names; any other field is refused.
If the call sends something to another system, keep evidence of it: put
{"to": where it went, "content": the bytes sent} under "sent" in the
Result’s detail, and in a ConnectorFailure’s detail when a try fails.
FlowAssembly takes it out before anything is recorded, and keeps it on the
case as it does for its own types: where it went, whether it was accepted, a
fingerprint of the bytes and, where the instance keeps files, the bytes
themselves, encrypted.
When a call fails¶
Raise ConnectorFailure(message, retryable=...):
retryable=Truewhen trying again may work: a timeout, a connection refused, a server busy or briefly down. FlowAssembly tries again with growing waits (four more times unless the operation setsretries).retryable=Falsewhen it will not: refused credentials, a rejected request, missing settings. The step stops at once and a supervisor sees it under Supervision, with your message.
Messages are shown to supervisors and kept in the audit trail: say what happened, and never include a secret.
A step may run more than once: a call that succeeded can be repeated if the
worker stops before recording it. Use call.key to make the second call do
nothing new, as the example below does.
What FlowAssembly does for every type¶
fa validateand the builder check that the fields yourreadsandreturnsname are in the case data, and run yourcheckandcheck_operation.Renaming a case field in the builder renames it in your settings too.
Each call is reported: the
connector.calledevent and thefa_connector_calls_totalandfa_connector_call_secondsmetrics, with its outcome and how long it took.Retries, dead-lettering, the audit record and tracing are the same as for the built-in types.
Testing it¶
flowassembly.connectors.testing runs your type as FlowAssembly does, without
a running FlowAssembly:
from flowassembly.connectors import testing
def test_mistakes_are_said():
assert testing.check(Ledger(), DEFINITION, case_fields={...}) == []
def test_a_posting_returns_its_reference():
result = testing.call(Ledger(), DEFINITION, "pay_commission", {"agent_account": "0123"},
binding={"host": "ledger.test"}, secret="token")
assert result.output == {"posting_reference": "..."}
check returns what fa validate would say. call runs one operation as the
connector worker would, including refusing fields the operation does not name.
Packaging¶
Declare the class as an entry point in the package’s pyproject.toml, so
FlowAssembly finds it once installed:
[project.entry-points."flowassembly.connector_types"]
ledger = "bank_ledger:Ledger"
examples/connector-type-file-drop is a complete package to start from. Its
type adds one CSV row per call to a folder another system collects from, and
it has its own tests.
Reviewing and switching it on¶
A connector type is code that runs with case data and secrets, so it is reviewed like any other code the organisation runs. FlowAssembly then needs two things before it runs one:
The package is installed with FlowAssembly, in the image or environment the connector worker runs from. The validator and the builder then know the type, so projects using it can be checked and built.
The instance switches it on by name, in its settings:
connectorTypes: [ledger]
A worker runs only FlowAssembly’s own types and those named here. A project using a type that is installed but not switched on fails at that step, with a message saying so.
When the connector worker starts, it logs which types it runs
(connector.types). It also logs any installed package it could not use
(connector.type_refused): one that would not load, is not a ConnectorType,
or takes a name already used. A package can never replace one of
FlowAssembly’s own types.