def analyze(self, uri, post): """ Actual code to submit the request This also saves the returned request id to a file and a queue Using queue to support threading. """ response = self.request_uri(uri, post) log.info("Response :"+ response) try: response = self.get_response(response) except SchmapAPIException,e : log.info(str(e))
def get_response(self, response): """ Handles the response """ if (response == ""): log.info("Empty Response") raise SchmapAPIException(7000) elif (response.startswith("<!DOCTYPE HTML")): log.debug("No the expected response") raise SchmapAPIException(7001) else: try: json_response = json.loads(response) except json.decoder.JSONDecodeError, e: raise SchmapAPIException(7003) try: if (json_response['code'] == 0): return json_response else: raise SchmapAPIException(json_response['code']) except KeyError,e: raise SchmapAPIException(7000)
def check_status(self, request_id): """ Checks the status of a posted request recursively sleeping for n seconds. After a pre-determined time, it throws an exception. You can then catch up with the rest of data using catch_up method. """ self.counter = self.counter + 1 if request_id < 0: raise SchmapAPIException(7004) uri = self.base_uri + "get_status?request_id="+str(request_id) response = self.get_response(self.request_uri(uri)) p_complete = response['percent_complete'] if (p_complete != 100): log.info("Completed "+str(p_complete)+"% for request_id "+str(request_id)) time.sleep(self.sleep_time) if( self.counter > ( self.max_wait_time / self.sleep_time)): raise SchmapAPIException(7005) self.check_status(request_id) else: log.info("Completed "+str(p_complete)+"% for request_id "+str(request_id)) return 0
for line in open(filename): request_id,list_name = line.strip().split('##') fetch() def save_to_file(self, response,filename): """ Save data to file with a file name """ try: os.mkdir(self.out_dir) except OSError, e: log.debug("Folder already exists, "+ str(e)) try: fp = open(self.out_dir+"/"+str(filename), 'a') fp.write(response) fp.close() except IOError,e: log.info("Cannot create file"+str(e)) def get_response(self, response): """ Handles the response """ if (response == ""): log.info("Empty Response") raise SchmapAPIException(7000) elif (response.startswith("<!DOCTYPE HTML")): log.debug("No the expected response") raise SchmapAPIException(7001) else: try: json_response = json.loads(response) except json.decoder.JSONDecodeError, e: raise SchmapAPIException(7003)