|
| 1 | +import json |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +try: |
| 6 | + from urllib.parse import urlparse, urlencode |
| 7 | + from urllib.request import urlopen, Request, build_opener, HTTPHandler |
| 8 | + from urllib.error import HTTPError |
| 9 | +except ImportError: |
| 10 | + from urlparse import urlparse |
| 11 | + from urllib import urlencode |
| 12 | + from urllib2 import urlopen, Request, HTTPError, build_opener, HTTPHandler |
| 13 | + |
| 14 | + |
| 15 | +EMAIL = os.environ['CLARIFAI_USER_EMAIL'] |
| 16 | +PASSWORD = os.environ['CLARIFAI_USER_PASSWORD'] |
| 17 | + |
| 18 | + |
| 19 | +BASE = 'https://api.clarifai.com/v2' |
| 20 | + |
| 21 | + |
| 22 | +def _request(method, url, payload={}, headers={}): |
| 23 | + opener = build_opener(HTTPHandler) |
| 24 | + full_url = '%s%s' % (BASE, url) |
| 25 | + request = Request(full_url, data=json.dumps(payload).encode()) |
| 26 | + for k in headers.keys(): |
| 27 | + request.add_header(k, headers[k]) |
| 28 | + request.get_method = lambda: method |
| 29 | + return json.loads(opener.open(request).read().decode()) |
| 30 | + |
| 31 | + |
| 32 | +def create_app(env_name): |
| 33 | + session_token, user_id = _login() |
| 34 | + |
| 35 | + url = '/users/%s/apps' % user_id |
| 36 | + payload = {'apps': [{'name': 'auto-created-in-%s-ci-test-run' % env_name}]} |
| 37 | + |
| 38 | + response = _request(method='POST', url=url, payload=payload, headers=_auth_headers(session_token)) |
| 39 | + |
| 40 | + _raise_on_http_error(response) |
| 41 | + data = response |
| 42 | + app_id = data['apps'][0]['id'] |
| 43 | + |
| 44 | + # This print needs to be present so we can read the value in CI. |
| 45 | + print(app_id) |
| 46 | + |
| 47 | + |
| 48 | +def create_key(app_id): |
| 49 | + session_token, user_id = _login() |
| 50 | + |
| 51 | + url = '/users/%s/keys' % user_id |
| 52 | + payload = { |
| 53 | + 'keys': [{ |
| 54 | + 'description': 'Auto-created in a CI test run', |
| 55 | + 'scopes': ['All'], |
| 56 | + 'apps': [{'id': app_id, 'user_id': user_id}] |
| 57 | + }] |
| 58 | + } |
| 59 | + response = _request(method='POST', url=url, payload=payload, headers=_auth_headers(session_token)) |
| 60 | + _raise_on_http_error(response) |
| 61 | + data = response |
| 62 | + key_id = data['keys'][0]['id'] |
| 63 | + |
| 64 | + # This print needs to be present so we can read the value in CI. |
| 65 | + print(key_id) |
| 66 | + |
| 67 | + |
| 68 | +def delete(app_id): |
| 69 | + session_token, user_id = _login() |
| 70 | + |
| 71 | + # All the related keys will be deleted automatically when the app is deleted |
| 72 | + _delete_app(session_token, user_id, app_id) |
| 73 | + |
| 74 | + |
| 75 | +def create_sample_workflow(api_key): |
| 76 | + url = '/workflows' |
| 77 | + payload = { |
| 78 | + 'workflows': [ |
| 79 | + { |
| 80 | + 'id': 'food-and-general', |
| 81 | + 'nodes': [ |
| 82 | + { |
| 83 | + 'id': 'food-workflow-node', |
| 84 | + 'model': { |
| 85 | + 'id': 'bd367be194cf45149e75f01d59f77ba7', |
| 86 | + 'model_version': { |
| 87 | + 'id': 'dfebc169854e429086aceb8368662641' |
| 88 | + } |
| 89 | + } |
| 90 | + }, |
| 91 | + { |
| 92 | + 'id': 'general-workflow-node', |
| 93 | + 'model': { |
| 94 | + 'id': 'aaa03c23b3724a16a56b629203edc62c', |
| 95 | + 'model_version': { |
| 96 | + 'id': 'aa9ca48295b37401f8af92ad1af0d91d' |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + ] |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | + response = _request(method='POST', url=url, payload=payload, headers=_auth_headers_for_api_key_key(api_key)) |
| 105 | + _raise_on_http_error(response) |
| 106 | + |
| 107 | + |
| 108 | +def _delete_app(session_token, user_id, app_id): |
| 109 | + url = '/users/%s/apps/%s' % (user_id, app_id) |
| 110 | + response = _request(method='DELETE', url=url, headers=_auth_headers(session_token)) |
| 111 | + _raise_on_http_error(response) |
| 112 | + |
| 113 | + |
| 114 | +def _auth_headers(session_token): |
| 115 | + headers = {'Content-Type': 'application/json', 'X-Clarifai-Session-Token': session_token} |
| 116 | + return headers |
| 117 | + |
| 118 | + |
| 119 | +def _auth_headers_for_api_key_key(api_key): |
| 120 | + headers = {'Content-Type': 'application/json', 'Authorization': 'Key ' + api_key} |
| 121 | + return headers |
| 122 | + |
| 123 | + |
| 124 | +def _login(): |
| 125 | + url = '/login' |
| 126 | + payload = {'email': EMAIL, 'password': PASSWORD} |
| 127 | + response = _request(method='POST', url=url, payload=payload) |
| 128 | + _raise_on_http_error(response) |
| 129 | + data = response |
| 130 | + user_id = data['v2_user_id'] |
| 131 | + session_token = data['session_token'] |
| 132 | + return session_token, user_id |
| 133 | + |
| 134 | + |
| 135 | +def _raise_on_http_error(response): |
| 136 | + # TODO: Make this work with urllib. |
| 137 | + # if int(response.status_code) // 100 != 2: |
| 138 | + # raise Exception('Unexpected response %s: %s' % (response.status_code, response.text)) |
| 139 | + pass |
| 140 | + |
| 141 | + |
| 142 | +def run(arguments): |
| 143 | + command = arguments[0] if arguments else '--help' |
| 144 | + if command == '--create-app': |
| 145 | + if len(arguments) != 2: |
| 146 | + raise Exception('--create-app takes one argument') |
| 147 | + |
| 148 | + env_name = arguments[1] |
| 149 | + create_app(env_name) |
| 150 | + elif command == '--create-key': |
| 151 | + if len(arguments) != 2: |
| 152 | + raise Exception('--create-key takes one argument') |
| 153 | + |
| 154 | + app_id = arguments[1] |
| 155 | + create_key(app_id) |
| 156 | + elif command == '--delete-app': |
| 157 | + if len(arguments) != 2: |
| 158 | + raise Exception('--delete-app takes one argument') |
| 159 | + app_id = arguments[1] |
| 160 | + delete(app_id) |
| 161 | + elif command == '--create-workflow': |
| 162 | + if len(arguments) != 2: |
| 163 | + raise Exception('--create-workflow takes one argument') |
| 164 | + api_key = arguments[1] |
| 165 | + create_sample_workflow(api_key) |
| 166 | + elif command == '--help': |
| 167 | + print('''DESCRIPTION: Creates and delete applications and API keys |
| 168 | +ARGUMENTS: |
| 169 | +--create-app [env_name] ... Creates a new application. |
| 170 | +--create-key [app_id] ... Creates a new API key. |
| 171 | +--delete-app [app_id] ... Deletes an application (API keys that use it are deleted as well). |
| 172 | +--create-workflow [api_key] ... Creates a sample workflow to be used in int. tests. |
| 173 | +--help ... This text.''') |
| 174 | + else: |
| 175 | + print('Unknown argument. Please see --help') |
| 176 | + exit(1) |
| 177 | + |
| 178 | + |
| 179 | +if __name__ == '__main__': |
| 180 | + run(arguments=sys.argv[1:]) |
0 commit comments