def pipe(self, input1): if self.output_type == OutputType.raw: return self._postRawOutput(input1) elif self.output_type == OutputType.void: return self._postVoidOutput(input1) else: return AlgoResponse.create_algo_response(self.client.postJsonHelper(self.url, input1, **self.query_parameters))
def runalgo(self, options, client): algo_input = None algo = client.algo(options.algo) url = client.apiAddress + algo.url result = None content = None algo.set_options(timeout=options.timeout, stdout=options.debug) #handle input type flags if(options.data != None): #data algo_input = options.data result = algo.pipe(algo_input) elif(options.text != None): #text algo_input = options.text key = self.getAPIkey(options.profile) content = 'text/plain' algo_input = algo_input.encode('utf-8') elif(options.json != None): #json algo_input = options.json key = self.getAPIkey(options.profile) content = 'application/json' elif(options.binary != None): #binary algo_input = bytes(options.binary) key = self.getAPIkey(options.profile) content = 'application/octet-stream' elif(options.data_file != None): #data file algo_input = open(options.data_file,"r").read() result = algo.pipe(algo_input) elif(options.text_file != None): #text file algo_input = open(options.text_file,"r").read() key = self.getAPIkey(options.profile) content = 'text/plain' algo_input = algo_input.encode('utf-8') elif(options.json_file != None): #json file #read json file and run algo with that input bypassing the auto detection of input type in pipe with open(options.json_file,"r") as f: algo_input = f.read() key = self.getAPIkey(options.profile) content = 'application/json' algo_input = json.dumps(algo_input).encode('utf-8') elif(options.binary_file != None): #binary file with open(options.binary_file,"rb") as f: algo_inputs = bytes(f.read()) key = self.getAPIkey(options.profile) content = 'application/octet-stream' else: output = "no valid input detected" if(content != None): result = AlgoResponse.create_algo_response(requests.post(url, data=algo_input, headers={'Authorization':key,'Content-Type':content}, params= algo.query_parameters).json()) if(result != None): output = result.result #handle output flags #output to file if there is an output file specified if(options.output != None): outputFile = options.output try: if isinstance(result.result, bytearray) or isinstance(result.result, bytes): out = open(outputFile,"wb") out.write(result.result) out.close() else: out = open(outputFile,"w") out.write(result.result) out.close() output = "" except Exception as error: print(error) return output