Skip to main content

MicroFunctions Client SDK

Project description

KNIX MicroFunctions SDK

Easy-to use package to access the management interface of KNIX MicroFunctions as well as deployed workflows.

The latest version of the SDK can be obtained from KNIX releases. It is also hosted at PyPI and can be installed with the PyPA recommended tool for installing Python packages:

pip3 install mfn_sdk

Setup the client

The MFN client can be configured by various means, using precedence of the following order:

  • configuration file ../settings.json, ~/settings.json and/or ./settings.json)
  • environment variables (MFN_URL, MFN_USER, MFN_PASSWORD, MFN_NAME)
  • using the constructor MfnClient(mfn_url,mfn_user,mfn_password,mfn_name,proxies)

The configuration file should contain a JSON dictionary with the parameter names as keys:

{
    "mfn_url": "<http://host:port>",
    "mfn_user": "<email>",
    "mfn_password": "<password>",
    "mfn_name": "<full name>",
    "proxies":
    {
        "http": "http://<proxyhost>:<port>",
        "https": "http://<proxyhost>:<port>"
    }
}

An equivalent of environment variables can also be used, with the exception of the proxy configuration, which uses the http_proxy and https_proxy variables:

export MFN_URL="<http://host:port>"
export MFN_USER="<email>"
export MFN_PASSWORD="<password>"
export MFN_NAME="<full name>"
export HTTP_PROXY="http://<proxyhost>:<port>" # or http_proxy
export HTTPS_PROXY="http://<proxyhost>:<port>" # or https_proxy

To overwrite parameters at runtime, they can be passed in the constructor.

from mfn_sdk import MfnClient

# MfnClient(mfn_url,mfn_user,mfn_password,mfn_name,proxies)
mfn = MfnClient(
    mfn_url="<http://host:port>",
    mfn_user="<email>",
    mfn_password="<password>",
    mfn_name="<full name>",
    proxies={
        "http": "http://<proxyhost>:<port>",
        "https": "http://<proxyhost>:<port>"
    })

NOTE: The mfn_name parameter is only used if the user does not exist (as it is required for every new user). If the parameter is missing, the client SDK will only try to login but won't create a user.

List functions and workflows

from mfn_sdk import MfnClient
mfn = MfnClient(
    mfn_url="https://knix.io",
    mfn_user="test@example.com",
    mfn_password="test123",
    mfn_name="Mr. Test")

for function in mfn.functions:
    print(function)

for workflow in mfn.workflows:
    print(workflow)

Create a new workflow

To create a simple workflow with just a single function, the function source and the workflow description is required. A function can have plaintext code or a ZIP file or both attached to it (more below).

function = mfn.add_function("echo")
function.code = """
def handle(event, context):
    context.log("Echoing"+event)
    return event
"""

The associated workflow description is provided as a string containing the JSON.

wf = mfn.add_workflow('wf_echo')
wf.json = """{
  "Comment": "Echo workflow",
  "StartAt": "entry",
  "States": {
    "entry": {
      "Type": "Task",
      "Resource": "echo",
      "End": true
    }
  }
}"""

Read and write objects (Note: only strings are allowed)

The key-value storage shared by workflows of a tenant can be modified or accessed using put(), get() and delete() on the client object:

mfn.put("my_key","some-value")
print("We have stored: " + mfn.get("my_key"))
mfn.delete("my_key")
mfn.keys() # should be empty list

Function ZIPs

A function can be a source code string and/or a ZIP file. If the ZIP file contains a source file with the name of the resource (i.e., function name), then its handle function would be used as a starting point. However, if source code is also attached as a string, it would overwrite said file and be used instead.

In the following example, the current directory is zipped and uploaded as a function ZIP.

import os
from zipfile import ZipFile
# Create a new function
g = mfn.add_function('myfunction')

# Create a zip file from the directory contents
zip_name = "myfunction.zip"
if os.path.exists(zip_name):
    os.remove(zip_name)

for root,dirs,files in os.walk('.'):
    with ZipFile(zip_name,'w') as zf:
        for fn in files:
            zf.write(fn)

# upload the zip file
g.upload(zip_name)

Execute workflows

Once a workflow has been created and its functions have been uploaded, it can be deployed and executed using the client SDK. The deploy(timeout=None) function can use a timeout. If timeout=None, it will immediately return after requesting deployment. If timeout=0, it will wait indefinitely for the workflow to change its status to "deployed". Any timeout > 0 will wait for that many seconds and throw an exception if the workflow hasn't reached the status "deployed" by then.

The execute(data,timeout=60) function invokes a deployed workflow. Here, timeout is passed to the Python requests HTTP transaction that invokes the workflow execution.

Using the above echo workflow example, we have created an echo_wf workflow object, which can be deployed and executed as follows:

wf.deploy(timeout=0) # wait until wf.status=='deployed'
print("Sending Hello")
result = wf.execute("Hello")
print(f"Received {result}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mfn_sdk-0.8.9.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

mfn_sdk-0.8.9-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file mfn_sdk-0.8.9.tar.gz.

File metadata

  • Download URL: mfn_sdk-0.8.9.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for mfn_sdk-0.8.9.tar.gz
Algorithm Hash digest
SHA256 02953f43a433f0a462c4d65ebfb57883f21ff58203cf1c6ba29c064eb28a90b2
MD5 a0b293324230e50ba0c659ab67d921bd
BLAKE2b-256 1719b20674815f8aaaedc5c20d662deb45e183e0068fba316d0c8bce6aae08df

See more details on using hashes here.

File details

Details for the file mfn_sdk-0.8.9-py3-none-any.whl.

File metadata

  • Download URL: mfn_sdk-0.8.9-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for mfn_sdk-0.8.9-py3-none-any.whl
Algorithm Hash digest
SHA256 c7e1e5b180334b2f98de47170659bf49c04d07c69778189e6d153456b24535c3
MD5 b5f1c97a6b3f6dc39b2360285c5cc98f
BLAKE2b-256 1eaed45b4a620f696d12d3fc33d8a19f0ead5de1bf1145f78b815b2100349ec8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page