| 7 | import shutil |
| 8 | |
| 9 | class CLI: |
| 10 | def __init__(self): |
| 11 | self.client = Algorithmia.client() |
| 12 | # algo auth |
| 13 | def auth(self, apiaddress, apikey="", cacert="", profile="default", bearer=""): |
| 14 | |
| 15 | # store api key in local config file and read from it each time a client needs to be created |
| 16 | key = self.getconfigfile() |
| 17 | config = toml.load(key) |
| 18 | |
| 19 | if ('profiles' in config.keys()): |
| 20 | if (profile in config['profiles'].keys()): |
| 21 | config['profiles'][profile]['api_key'] = apikey |
| 22 | config['profiles'][profile]['api_server'] = apiaddress |
| 23 | config['profiles'][profile]['ca_cert'] = cacert |
| 24 | config['profiles'][profile]['bearer_token'] = bearer |
| 25 | |
| 26 | else: |
| 27 | config['profiles'][profile] = {'api_key':apikey,'api_server':apiaddress,'ca_cert':cacert,'bearer_token':bearer} |
| 28 | else: |
| 29 | config['profiles'] = {profile:{'api_key':apikey,'api_server':apiaddress,'ca_cert':cacert,'bearer_token':bearer }} |
| 30 | |
| 31 | with open(key, "w") as key: |
| 32 | toml.dump(config,key) |
| 33 | |
| 34 | self.ls(path = None,client = CLI().getClient(profile)) |
| 35 | |
| 36 | # algo run <algo> <args..> run the the specified algo |
| 37 | def runalgo(self, options, client): |
| 38 | algo_input = None |
| 39 | |
| 40 | algo = client.algo(options.algo) |
| 41 | url = client.apiAddress + algo.url |
| 42 | result = None |
| 43 | content = None |
| 44 | |
| 45 | algo.set_options(timeout=options.timeout, stdout=options.debug) |
| 46 | |
| 47 | # handle input type flags |
| 48 | if (options.data != None): |
| 49 | # data |
| 50 | algo_input = options.data |
| 51 | |
| 52 | result = algo.pipe(algo_input) |
| 53 | |
| 54 | elif (options.text != None): |
| 55 | # text |
| 56 | algo_input = options.text |
| 57 | key = self.getAPIkey(options.profile) |
| 58 | content = 'text/plain' |
| 59 | algo_input = algo_input.encode('utf-8') |
| 60 | |
| 61 | elif (options.json != None): |
| 62 | # json |
| 63 | algo_input = options.json |
| 64 | key = self.getAPIkey(options.profile) |
| 65 | content = 'application/json' |
| 66 |
no outgoing calls