|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import os |
| 6 | +import sys |
| 7 | + |
| 8 | +try: |
| 9 | + from urllib.error import HTTPError |
| 10 | + from urllib.request import HTTPHandler, Request, build_opener |
| 11 | +except ImportError: |
| 12 | + from urllib2 import HTTPError, HTTPHandler, Request, build_opener |
| 13 | + |
| 14 | +EMAIL = os.environ["CLARIFAI_USER_EMAIL"] |
| 15 | +PASSWORD = os.environ["CLARIFAI_USER_PASSWORD"] |
| 16 | + |
| 17 | + |
| 18 | +def _assert_response_success(response): |
| 19 | + assert "status" in response, f"Invalid response {response}" |
| 20 | + assert "code" in response["status"], f"Invalid response {response}" |
| 21 | + assert response["status"]["code"] == 10000, f"Invalid response {response}" |
| 22 | + |
| 23 | + |
| 24 | +def _request(method, url, payload={}, headers={}): |
| 25 | + base_url = os.environ.get("CLARIFAI_GRPC_BASE", "api.clarifai.com") |
| 26 | + |
| 27 | + opener = build_opener(HTTPHandler) |
| 28 | + full_url = f"https://{base_url}/v2{url}" |
| 29 | + request = Request(full_url, data=json.dumps(payload).encode()) |
| 30 | + for k in headers.keys(): |
| 31 | + request.add_header(k, headers[k]) |
| 32 | + request.get_method = lambda: method |
| 33 | + try: |
| 34 | + response = opener.open(request).read().decode() |
| 35 | + except HTTPError as e: |
| 36 | + error_body = e.read().decode() |
| 37 | + try: |
| 38 | + error_body = json.dumps(json.loads(error_body), indent=4) |
| 39 | + except Exception: |
| 40 | + pass |
| 41 | + raise Exception("ERROR after a HTTP request to: %s %s" % (method, full_url) + |
| 42 | + ". Response: %d %s:\n%s" % (e.code, e.reason, error_body)) |
| 43 | + return json.loads(response) |
| 44 | + |
| 45 | + |
| 46 | +def login(): |
| 47 | + url = "/login" |
| 48 | + payload = {"email": EMAIL, "password": PASSWORD} |
| 49 | + data = _request(method="POST", url=url, payload=payload) |
| 50 | + _assert_response_success(data) |
| 51 | + |
| 52 | + assert "v2_user_id" in data, f"Invalid response {data}" |
| 53 | + user_id = data["v2_user_id"] |
| 54 | + assert user_id, f"Invalid response {data}" |
| 55 | + |
| 56 | + assert "session_token" in data, f"Invalid response {data}" |
| 57 | + session_token = data["session_token"] |
| 58 | + assert session_token, f"Invalid response {data}" |
| 59 | + |
| 60 | + return session_token, user_id |
| 61 | + |
| 62 | + |
| 63 | +def _auth_headers(session_token): |
| 64 | + headers = {"Content-Type": "application/json", "X-Clarifai-Session-Token": session_token} |
| 65 | + return headers |
| 66 | + |
| 67 | + |
| 68 | +def create_pat(): |
| 69 | + session_token, user_id = login() |
| 70 | + os.environ["CLARIFAI_USER_ID"] = user_id |
| 71 | + |
| 72 | + url = "/users/%s/keys" % user_id |
| 73 | + payload = { |
| 74 | + "keys": [{ |
| 75 | + "description": "Auto-created in a CI test run", |
| 76 | + "scopes": ["All"], |
| 77 | + "type": "personal_access_token", |
| 78 | + "apps": [], |
| 79 | + }] |
| 80 | + } |
| 81 | + data = _request(method="POST", url=url, payload=payload, headers=_auth_headers(session_token)) |
| 82 | + _assert_response_success(data) |
| 83 | + |
| 84 | + assert "keys" in data, f"Invalid response {data}" |
| 85 | + assert len(data["keys"]) == 1, f"Invalid response {data}" |
| 86 | + assert "id" in data["keys"][0], f"Invalid response {data}" |
| 87 | + pat_id = data["keys"][0]["id"] |
| 88 | + assert pat_id, f"Invalid response {data}" |
| 89 | + |
| 90 | + # This print needs to be present so we can read the value in CI. |
| 91 | + print(pat_id) |
| 92 | + |
| 93 | + |
| 94 | +def run(arguments): |
| 95 | + if arguments.email: |
| 96 | + global EMAIL |
| 97 | + EMAIL = arguments.email # override the default testing email |
| 98 | + if arguments.password: |
| 99 | + global PASSWORD |
| 100 | + PASSWORD = arguments.password # override the default testing password |
| 101 | + # these options are mutually exclusive |
| 102 | + if arguments.create_pat: |
| 103 | + create_pat() |
| 104 | + elif arguments.get_userid: |
| 105 | + _, user_id = login() |
| 106 | + # This print needs to be present so we can read the value in CI. |
| 107 | + print(user_id) |
| 108 | + else: |
| 109 | + print(f"No relevant arguments specified. Run {sys.argv[0]} --help to see available options") |
| 110 | + exit(1) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + parser = argparse.ArgumentParser( |
| 115 | + description="Create Applications, Keys, and Workflows for testing.") |
| 116 | + parser.add_argument( |
| 117 | + "--user-email", |
| 118 | + dest="email", |
| 119 | + help= |
| 120 | + "The email of the account for which the command will run. (Defaults to ${CLARIFAI_USER_EMAIL})", |
| 121 | + ) |
| 122 | + parser.add_argument( |
| 123 | + "--user-password", |
| 124 | + dest="password", |
| 125 | + help= |
| 126 | + "The password of the account for which the command will run. (Defaults to ${CLARIFAI_USER_PASSWORD})", |
| 127 | + ) |
| 128 | + group = parser.add_mutually_exclusive_group() |
| 129 | + group.add_argument("--create-pat", action="store_true", help=" Creates a new PAT key.") |
| 130 | + group.add_argument("--get-userid", action="store_true", help=" Gets the user id.") |
| 131 | + |
| 132 | + args = parser.parse_args() |
| 133 | + run(args) |
0 commit comments