# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda root]
#     language: python
#     name: conda-root-py
# ---

# # Introduction
#
# Let's start with a example of an Alpine API session.
#
# 1. Initialize a session.
# 1. Take a tour of some commands.
# 1. Run a workflow and download the results.

# Import the Python Alpine API and some other useful packages.

import alpine as AlpineAPI

from pprint import pprint
import json

# ### Setup
#
# Have access to a workflow on your Alpine instance that you can run. You'll need a few pieces of information in order to log in and run the workflow. First, find the URL of the open workflow. It should look something like:
#
# `https://<AlpineHost>:<PortNum>/#workflows/<WorkflowID>`
#
# You'll also need your Alpine username and password.
#
# I've stored my connection information in a configuration file named `alpine_login.conf` that looks something like this:
# ```JSON
#     {
#         "host": "AlpineHost",
#         "port": "PortNum",
#         "username": "fakename",
#         "password": "<PASSWORD>"
#     }
# ```

# +
filename = "alpine_login.conf"

with open(filename, "r") as f:
    data = f.read()

conn_info = json.loads(data)

host = conn_info["host"]
port = conn_info["port"]
username = conn_info["username"]
password = conn_info["password"]
# -

# Here are the names of a workspace and a workflow within it that we want to run.

test_workspace_name = "API Sample Workspace"
test_workflow_name = "Data ETL"

# Create a session and log in the user.

session = AlpineAPI.APIClient(host, port, username, password)

# ### Use the API

# Get information about the Alpine instance.

pprint(session.get_license())

pprint(session.get_version())

# Find information about the logged-in user.

pprint(session.get_status())

# Find information on all users.

len(session.user.get_list())

# Find your user ID and then use it to update your user data.

user_id = session.user.get_id(username)

pprint(session.user.update(user_id, title = "Assistant to the Regional Manager"))

# A similar set of commands can be used to create and update workspaces and the membership of each workspace.

test_workspace_id = session.workspace.get_id(test_workspace_name)
session.workspace.member.add(test_workspace_id, user_id);

# ### Run a workflow
#
# To run a workflow use the Process subclass of the Workfile class. The `wait_until_finished` method will periodically query the status of the running workflow and returns control to the user when the workflow has completed.

# +
workflow_id = session.workfile.get_id(workfile_name = "Data ETL",
                                      workspace_id = test_workspace_id)

process_id = session.workfile.process.run(workflow_id)

session.workfile.process.wait_until_finished(workflow_id = workflow_id,
                                             process_id = process_id,
                                             verbose = True,
                                             query_time = 5)
# -

# We can download results using the `download_results` method. The workflow results contain a summary of the output of each operator as well as metadata about the workflow run.

flow_results = session.workfile.process.download_results(workflow_id, process_id)
pprint(flow_results, depth=2)



StarcoderdataJupyterScriptsDedupFiltered