Documentation

Pushing to the Code Engine Programmatically

Abstract

SummaryAn explanation of transforms (python code stored in modules) in the Code Engine and how get a specific transform, get all of the transforms, or reset or remove a transform.  

You can use the API to break up your code into modules in the Code Engine. Code sub-modules can be used to encapsulate logic, just as a Python module would be used. It can also be used as a means to store configuration, allowing the user to easily update this configuration via code. For more information about working with code modules, see Splitting your code into modules in the Code Engine documentation.

Note

Sub-modules should not exceed 10,000 lines of code.

  • You can start by getting the current code from the Code Engine with api.get_code_engine_module(module_name='main'). Pass a module name if you want a specific module, otherwise it will return the main code block (which is also the code you view in the UI).

  • You can get all the modules defined in the system using api.get_code_engine_code(). This returns a dictionary which reflects modules names and their content.

  • Deploy code to the Code Engine using api.set_code_engine_code(modules)

    • "modules" is a python map of a module name (string) to code (string)

    • On success the function returns an HttpResponse object with status code 2XX.

      On failure, an exception is raised.

    • Note that you can delete a module via delete_code_engine_module(module_name).

  • Example:

    import alooma
    
    ## Using API Key ##
    APIKEY = "<YOUR_API_KEY>" # get this from your Alooma admin
    ACCOUNTNAME = "<DEPLOYMENT NAME>" # Used to specify which deployment
                                      # to use if you have more than one
    
    api = alooma.Client(api_key=APIKEY, account_name=ACCOUNTNAME)
    
    # Define and submit a code module named 'submodule'.
    #   this module contains a dict called 'transform_names'
    #   and a method called 'transform_ft_to_mtr'
    
    my_submodule = {"submodule" : """transform_names = {'John': 'Jonathan',
                       'Bob': 'Robert',
                       'Rick': 'Richard'}
    
    def transform_ft_to_mtr(x):
        return x * 0.3048"""}
    
    api.set_code_engine_code(my_submodule)
    
    # Now edit the main code to use 'submodule' by importing it.
    # You can also perform this step via the Alooma UI
    # by editing the code in the Code Engine.
    
    my_main = {"main" : """
    from . import submodule
    
    def transform(event):
      event['name'] = submodule.transform_names.get(event['name'], event['name'])
      # The above line is equivalent to: # if event['name'] in submodule.transform_names:
      #     event['name'] = submodule.transform_names[event['name']]
    
      if 'value' in event:
        event['value'] = submodule.transform_ft_to_mtr(event['value'])
      return event"""}
    
    api.set_code_engine_code(my_main)

As any good developer knows, you don't just go and deploy code without testing it! Learn more about how to test your Code Engine code using alooma.py.

Search results

    No results found