Skip to content

Client libraries

UbiOps offers client libraries that allow for easy integration of UbiOps functionality into your project. The libraries offer the same functionality as the API. More advanced information about these client libraries can be found on our Github page.

Installation

The UbiOps client library (Python) is available on PyPi and can be installed using PIP:

pip install ubiops

Usage

To test if everything works properly, you can run the snippet below. Please, fill in your own API token (<YOUR_API_TOKEN>). Documentation on creating an API token can be found here.

import ubiops

configuration = ubiops.Configuration()
configuration.api_key['Authorization'] = 'Token <YOUR_API_TOKEN>'

client = ubiops.ApiClient(configuration)
api = ubiops.api.CoreApi(client)

print(api.service_status())
client.close()

Documentation

More comprehensive information about the client library functionality can be found on Github.

Example deployment

The examples folder in the Github repository contains Jupyter notebooks which show how to create a deployment and pipeline on UbiOps using the Python client library.

Example usage inside deployment

The UbiOps client library can be used inside a deployment package. This is useful, for example, if you want to make a request to another deployment.

An example deployment, which returns the number of deployments in your project, is shown below. It uses the context variable provided in the __init__ method to get the name of your project where the deployment is running.

Upload this example to a deployment without input fields, and with a structured output field n_deployments. An environment variable will be used to pass the API_TOKEN. Therefore, create an environment variable API_TOKEN for your deployment.

import ubiops
from os import environ


class Deployment:

    def __init__(self, base_directory, context):
        api_token = environ['API_TOKEN']  # formatted like 'Token .....'

        conf = ubiops.Configuration()
        conf.api_key['Authorization'] = api_token
        self.client = ubiops.ApiClient(conf)
        self.core_api = ubiops.api.CoreApi(self.client)

        self.context = context

    def request(self, data):
        deployments = self.core_api.deployments_list(
            project_name=self.context['project']
        )

        # You could create more api calls here.
        # For example, creating a batch request to deployment 'my-deployment':
        # request_results = self.core_api.batch_deployment_requests_create(
        #     project_name=self.context['project'],
        #     deployment_name='my-deployment',
        #     data=[data]
        # )

        # Here we will close the opened connection to the api.
        # When you use self.core_api again, a new connection will be opened.
        self.client.close()

        return {
            "n_deployments": len(deployments)
        }