| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import argparse |
| 4 |
import os |
| 5 |
import sys |
| 6 |
|
| 7 |
import globus_sdk |
| 8 |
from globus_sdk.token_storage import SimpleJSONFileAdapter |
| 9 |
|
| 10 |
MY_FILE_ADAPTER = SimpleJSONFileAdapter(os.path.expanduser("~/.sdk-manage-flow.json")) |
| 11 |
|
| 12 |
SCOPES = [globus_sdk.FlowsClient.scopes.manage_flows] |
| 13 |
RESOURCE_SERVER = globus_sdk.FlowsClient.resource_server |
| 14 |
|
| 15 |
# tutorial client ID |
| 16 |
# we recommend replacing this with your own client for any production use-cases |
| 17 |
CLIENT_ID = "61338d24-54d5-408f-a10d-66c06b59f6d2" |
| 18 |
|
| 19 |
NATIVE_CLIENT = globus_sdk.NativeAppAuthClient(CLIENT_ID) |
| 20 |
|
| 21 |
|
| 22 |
def do_login_flow(): |
| 23 |
NATIVE_CLIENT.oauth2_start_flow(requested_scopes=SCOPES, refresh_tokens=True) |
| 24 |
authorize_url = NATIVE_CLIENT.oauth2_get_authorize_url() |
| 25 |
print(f"Please go to this URL and login:\n\n{authorize_url}\n") |
| 26 |
auth_code = input("Please enter the code here: ").strip() |
| 27 |
tokens = NATIVE_CLIENT.oauth2_exchange_code_for_tokens(auth_code) |
| 28 |
return tokens |
| 29 |
|
| 30 |
|
| 31 |
def get_authorizer(): |
| 32 |
# try to load the tokens from the file, possibly returning None |
| 33 |
if MY_FILE_ADAPTER.file_exists(): |
| 34 |
tokens = MY_FILE_ADAPTER.get_token_data(RESOURCE_SERVER) |
| 35 |
else: |
| 36 |
tokens = None |
| 37 |
|
| 38 |
if tokens is None: |
| 39 |
# do a login flow, getting back initial tokens |
| 40 |
response = do_login_flow() |
| 41 |
# now store the tokens and pull out the correct token |
| 42 |
MY_FILE_ADAPTER.store(response) |
| 43 |
tokens = response.by_resource_server[RESOURCE_SERVER] |
| 44 |
|
| 45 |
return globus_sdk.RefreshTokenAuthorizer( |
| 46 |
tokens["refresh_token"], |
| 47 |
NATIVE_CLIENT, |
| 48 |
access_token=tokens["access_token"], |
| 49 |
expires_at=tokens["expires_at_seconds"], |
| 50 |
on_refresh=MY_FILE_ADAPTER.on_refresh, |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
def get_flows_client(): |
| 55 |
return globus_sdk.FlowsClient(authorizer=get_authorizer()) |
| 56 |
|
| 57 |
|
| 58 |
def create_flow(args): |
| 59 |
flows_client = get_flows_client() |
| 60 |
print( |
| 61 |
flows_client.create_flow( |
| 62 |
title=args.title, |
| 63 |
definition={ |
| 64 |
"StartAt": "DoIt", |
| 65 |
"States": { |
| 66 |
"DoIt": { |
| 67 |
"Type": "Action", |
| 68 |
"ActionUrl": "https://actions.globus.org/hello_world", |
| 69 |
"Parameters": { |
| 70 |
"echo_string": "Hello, Asynchronous World!", |
| 71 |
}, |
| 72 |
"End": True, |
| 73 |
} |
| 74 |
}, |
| 75 |
}, |
| 76 |
input_schema={}, |
| 77 |
subtitle="A flow created by the SDK tutorial", |
| 78 |
) |
| 79 |
) |
| 80 |
|
| 81 |
|
| 82 |
def delete_flow(args): |
| 83 |
flows_client = get_flows_client() |
| 84 |
print(flows_client.delete_flow(args.flow_id)) |
| 85 |
|
| 86 |
|
| 87 |
def list_flows(): |
| 88 |
flows_client = get_flows_client() |
| 89 |
for flow in flows_client.list_flows(filter_roles="flow_owner"): |
| 90 |
print(f"title: {flow['title']}") |
| 91 |
print(f"id: {flow['id']}") |
| 92 |
print() |
| 93 |
|
| 94 |
|
| 95 |
def main(): |
| 96 |
parser = argparse.ArgumentParser() |
| 97 |
parser.add_argument("action", choices=["create", "delete", "list"]) |
| 98 |
parser.add_argument("-f", "--flow-id", help="Flow ID for delete") |
| 99 |
parser.add_argument("-t", "--title", help="Name for create") |
| 100 |
args = parser.parse_args() |
| 101 |
|
| 102 |
try: |
| 103 |
if args.action == "create": |
| 104 |
if args.title is None: |
| 105 |
parser.error("create requires --title") |
| 106 |
create_flow(args) |
| 107 |
elif args.action == "delete": |
| 108 |
if args.flow_id is None: |
| 109 |
parser.error("delete requires --flow-id") |
| 110 |
delete_flow(args) |
| 111 |
elif args.action == "list": |
| 112 |
list_flows() |
| 113 |
else: |
| 114 |
raise NotImplementedError() |
| 115 |
except globus_sdk.FlowsAPIError as e: |
| 116 |
print(f"API Error: {e.code} {e.message}") |
| 117 |
print(e.text) |
| 118 |
sys.exit(1) |
| 119 |
|
| 120 |
|
| 121 |
if __name__ == "__main__": |
| 122 |
main() |