Skip to content

UbiOps client library - Python

Client Library to interact with the UbiOps API (v2.1).

Examples

Examples can be found here.

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):
        self.context = 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)

    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', using the default version:
        #
        # 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)
        }