Skip to main content

Relay Python SDK

This is the Python SDK for use with the internal Relay service APIs. The SDK requires Python 3.8.

It is intended for use by integration authors who are building containers to run inside the service. For running workflows and interacting with the user-facing service APIs, use the Relay CLI.

The API documentation is auto-generated from the source code. Here are some higher-level examples that show the main SDK classes that integration authors will interact with.

Installation

The SDK is available to install via pip:

pip install relay-sdk

If you use the relaysh/core:latest-python container image as your base image, it’ll be pre-installed.

Usage

The main purpose of the SDK is to provide helpers for interacting with Relay’s metadata service. Each container that runs in Relay has access to this service, which allows the container to read and write key-value data, emit events, and generate logs.

Accessing Data from the step spec

The Interface class is the primary way to interact with the service. Import it and instantiate an object, then call methods on that object to access metadata, which comes from the spec section of the step and global Connection information. The Dynamic class provides syntactic sugar for getting data like connection credentials, workflow-specific parameters, and secrets. It represents nested data structures as dot-separated method accessors.

from relay_sdk import Interface, Dynamic as D

relay = Interface()
azuresecret = relay.get(D.azure.connection.secret) # using Dynamic
azureclient = relay.get('azure["connection"]["clientID"]') # same as above
secret = relay.get(D.mysecret)
relay.outputs.set("outputkey","This will be the value of outputkey")

Running Workflows

The Workflow class offers the method run to run a workflow.

Use the following to run the workflow my_workflow with parameter param_name and value param_value:

from relay_sdk import Interface

relay = Interface()
relay.workflows.run('my_workflow', parameters={'param_name':'param_value'})

Webhook Triggers

The WebhookServer class provides a helper that sets up a webserver to handle incoming requests for Trigger actions.

This example, from the Docker Hub integration, makes use of the Interface class to access the events.emit method, which will cause the workflow associated with this trigger to be run with the inline mapping of workflow parameters to values extracted from the webhook payload.

The WebhookServer class can run any WSGI or ASGI application passed to it. The integrations the Relay team develops internally use the Quart web app framework.

from relay_sdk import Interface, WebhookServer
from quart import Quart, request, jsonify, make_response

relay = Interface()
app = Quart('image-pushed')

@app.route('/', methods=['POST'])
async def handler():
    event_payload = await request.get_json()

    if event_payload is None:
        return await make_response(jsonify(message='not a valid Docker Hub event'), 400)

    pd = event_payload['push_data']
    rd = event_payload['repository']

    relay.events.emit({
        'pushedAt': pd['pushed_at'],
        'pusher': pd['pusher'],
        'tag': pd['tag'],
        'name': rd['repo_name']
    })

    return await make_response(jsonify(message='success'), 200)


if __name__ == '__main__':
    WebhookServer(app).serve_forever()

Metadata

Release files for relay-sdk 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for relay-sdk 0.3.1
File Size Uploaded
relay-sdk-0.3.1.tar.gz 17.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for relay-sdk 0.3.1
File Interpreter ABI Platform
relay_sdk-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 29.6 kB

Release files / relay-sdk-0.3.1.tar.gz

Download URL relay-sdk-0.3.1.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0cfd32ecd0904520fe58930098ffc4a256f9b268e22580a768d25f85e3218659
BLAKE2b-256 checksum
How to use checksums
afdbb2344245b10d8b02dfd38fa391658fe744bd1edd45fead85fc6d28a11727
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.13

Release files / relay_sdk-0.3.1-py3-none-any.whl

Download URL relay_sdk-0.3.1-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
229d2459d7fca3b9a5a4ac1a445e27db7876431d31251547b7d98948024db021
BLAKE2b-256 checksum
How to use checksums
a996bcbe968fa45367b36436a257b808a8dbbfaffb7d0bbd6aece8c29b2f83cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page