A complete first Python algorithm
This scenario reads a small JSON catalog, checks product identifiers and prices, and emits stable records. Create a Python scenario and supply api_url for your permitted source.
def run(ctx):
url = ctx.param('api_url', '')
if not url:
ctx.abort('Enter api_url')
ctx.log('Loading catalog')
response = ctx.http().with_headers({'Accept': 'application/json'}).get(url)
if not response.ok():
ctx.fail('Catalog returned HTTP ' + str(response.status))
payload = response.json()
if not isinstance(payload, dict) or not isinstance(payload.get('items'), list):
ctx.fail('The response has no items list')
seen = set()
items = payload['items']
for index, item in enumerate(items):
if ctx.cancelled():
ctx.abort('Collection stopped')
if not isinstance(item, dict):
ctx.fail('Invalid product structure')
sku = str(item.get('sku') or '').strip()
if not sku or sku in seen:
ctx.metric('skipped', 1)
continue
price = ctx.text().money(item.get('price'))
if price is None or price < 0:
ctx.fail('Invalid price for SKU ' + sku)
seen.add(sku)
ctx.emit({'sku': sku, 'name': str(item.get('name') or ''), 'price': price}, sku)
ctx.progress(index + 1, len(items), 'Processing catalog')
ctx.log('Catalog processed', {'records': len(seen)})
Expected input: {"items":[{"sku":"A-1","name":"Cable","price":"1 250,50"}]}. The emitted price is the number 1250.5. The code checks duplicates within this run and explains failures. Decide explicitly whether an invalid price should fail the run or skip the record with a warning.
Check before scheduling
- Syntax validation succeeds without requesting the source.
- A one-record trial produces correct keys and types. An empty list produces no records, while invalid structure fails.
- A retry with the same identifier updates the intended record using the stable key.
- The published version succeeds manually before adding its schedule and time zone.
- CSV/JSON/XML exports contain the expected mapped fields. Verify the HTTP endpoint token and its scopes separately.
Add pagination for a large catalog. Avoid unbounded lists and endless loops.