Appearance
Cookbook
Short recipes for common steps. Each uses an input type Order and an output type OrderDto; replace them with your own flow types.
Map a record
csharp
public override void Execute(IMetaData metaDataInstance, IViaType inputDataInstance)
{
var metaData = (viauser.FlowMetaData)metaDataInstance;
var order = (Order)inputDataInstance;
var dto = new OrderDto { Id = order.Id, Total = order.Amount + order.Tax };
Next(metaData, dto);
}python
def execute(metaData: FlowMetaData, inputData: Order):
dto = OrderDto()
dto.Id = inputData.Id
dto.Total = inputData.Amount + inputData.Tax
services.Next(metaData, dto)javascript
async function execute(metaData, inputData) {
const dto = new OrderDto();
dto.Id = inputData.Id;
dto.Total = inputData.Amount + inputData.Tax;
services.Next(metaData, dto);
}Filter records
Return without calling Next to drop a record.
csharp
if (order.Status != "Paid") return;
Next(metaData, order);python
if inputData.Status != "Paid":
return
services.Next(metaData, inputData)javascript
if (inputData.Status !== "Paid") return;
services.Next(metaData, inputData);Split a list into records
csharp
foreach (var line in order.Lines)
Next(metaData, new OrderLineDto { OrderId = order.Id, Sku = line.Sku, Quantity = line.Quantity });python
for line in inputData.Lines:
dto = OrderLineDto()
dto.OrderId = inputData.Id
dto.Sku = line.Sku
dto.Quantity = line.Quantity
services.Next(metaData, dto)javascript
for (const line of inputData.Lines) {
const dto = new OrderLineDto();
dto.OrderId = inputData.Id;
dto.Sku = line.Sku;
dto.Quantity = line.Quantity;
services.Next(metaData, dto);
}When the consumer works in batches, send the lines in a batch.
Read a setting
csharp
var threshold = Services.Settings.Get<decimal>("Orders", "ReviewThreshold");
if (order.Amount > threshold) Services.Log.Warning($"Order {order.Id} needs review", metaData, order);python
threshold = float(services.Settings.Get("Orders", "ReviewThreshold"))
if inputData.Amount > threshold:
services.Log.Warning(f"Order {inputData.Id} needs review")javascript
const threshold = Number(services.Settings.Get("Orders", "ReviewThreshold"));
if (inputData.Amount > threshold) services.Log.Warning(`Order ${inputData.Id} needs review`);Number records
A sequence gives unique numbers that survive restarts.
csharp
Services.Storage.Sequences.NewIfMissing("invoice");
dto.InvoiceNumber = $"INV-{Services.Storage.Sequences.Next("invoice"):D6}";python
services.Storage.Sequences.NewIfMissing("invoice")
dto.InvoiceNumber = f"INV-{services.Storage.Sequences.Next('invoice'):06d}"javascript
services.Storage.Sequences.NewIfMissing("invoice");
dto.InvoiceNumber = "INV-" + String(services.Storage.Sequences.Next("invoice")).padStart(6, "0");Call an HTTP API
For most calls the Http client plugin is the better choice. Inside a script:
csharp
private static readonly HttpClient _http = new();
public override async Task ExecuteAsync(IMetaData metaDataInstance, IViaType inputDataInstance)
{
var metaData = (viauser.FlowMetaData)metaDataInstance;
var order = (Order)inputDataInstance;
var url = Services.Settings.Get("Crm", "ApiUrl");
var json = await _http.GetStringAsync($"{url}/customers/{order.CustomerId}", Services.CancellationToken);
order.CustomerJson = json;
await NextAsync(metaData, order);
}python
import urllib.request
def execute(metaData: FlowMetaData, inputData: Order):
url = services.Settings.Get("Crm", "ApiUrl")
with urllib.request.urlopen(f"{url}/customers/{inputData.CustomerId}") as response:
inputData.CustomerJson = response.read().decode("utf-8")
services.Next(metaData, inputData)javascript
async function execute(metaData, inputData) {
const url = services.Settings.Get("Crm", "ApiUrl");
const response = await fetch(`${url}/customers/${inputData.CustomerId}`);
if (!response.ok) throw new Error(`CRM answered ${response.status}`);
inputData.CustomerJson = await response.text();
services.Next(metaData, inputData);
}The C# version needs Async/Await switched on.