Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Prerequisites

It is assumed the VSCode Plugin and uip-cli version 2.0.0 are installed.

Ideally, Universal Controller and Universal Agent 7.4.0.0 should be installed, but any version after 7.0.0.0 is sufficient.

All work will be done in a Python 3.7.16 virtual environment. Make sure the uip-cli is installed in the virtual environment.

Introduction

We will create an Extension that uses the requests module.

Step 1 - Initializing the Extension

Open VSCode to an empty folder (e.g. /tmp/tutorial) and activate the virtual environment you wish to use.

Initialize the ue-task extension by clicking:

For this tutorial, all parameters are suitable so, just pressing 'Enter' to select the default for each parameter is sufficient. 

Once initialized, you should now have a file called requirements.txt in your working folder:

Open the file and add requests==2.28.2 (version is optional) as follows:

requirements.txt
#
# Specify any third-party Python modules that should be bundled with the
# extension. uip-cli will automatically download and bundle the modules
# upon running `uip build` or `uip push`. 
#
# Refer to https://pip.pypa.io/en/stable/reference/requirements-file-format/
# for the expected format.
#

requests==2.28.2

Step 2 - Using requests module

Open extension.py and modify it as follows:

extension.py
from __future__ import (print_function)
from universal_extension import UniversalExtension
from universal_extension import ExtensionResult
from universal_extension import logger
import requests


class Extension(UniversalExtension):
    """Required class that serves as the entry point for the extension
    """

    def __init__(self):
        """Initializes an instance of the 'Extension' class
        """
        # Call the base class initializer
        super(Extension, self).__init__()

    def extension_start(self, fields):
        """Required method that serves as the starting point for work performed
        for a task instance.

        Parameters
        ----------
        fields : dict
            populated with field values from the associated task instance
            launched in the Controller

        Returns
        -------
        ExtensionResult
            once the work is done, an instance of ExtensionResult must be
            returned. See the documentation for a full list of parameters that
            can be passed to the ExtensionResult class constructor
        """

        resp = requests.get(
            'https://httpbin.org/basic-auth/user/pass',
            auth=('user', 'pass')
        )
        print(resp.status_code)

        # Return the result with a payload containing a Hello message...
        return ExtensionResult(
            unv_output='Hello Extension!'
        )

On line 5, we import the requests module

On lines 36-40, we call requests.get on a sample url and print the status code

Step 3 - Pushing out Extension

We are ready to test out our extension!

Assuming this Extension has not been pushed out to the Controller, go ahead and run

You may need to configure the username, password, and url first.

< Previous     Next >




  • No labels