def _post(self, parameters): if isinstance(parameters, dict): payload = parameters else: raise karaerror.KaraExecutorError( "ofrg fetch executor only apply a dict type parameters.") from pprint import pprint as pp pp(payload) last_querystring = '&'.join( ['='.join([k, str(v)]) for k, v in payload.items()]) self.raw = dict() self.raw['querystring'] = self.url + '?' + last_querystring self.raw['payload'] = payload # TODO: don't use eval, has other better style? method = eval("requests." + self.method.lower()) self.raw['response'] = result = method(self.url, params=payload) result_string = result.text.encode('utf-8') json_obj = json.loads(result_string) if not json_obj['success'] or json_obj['code'] != '1': raise karaerror.KaraExecutorError("ofrg fetch order failure.") self.raw['count'] = json_obj['totalCount'] self.raw[ 'real_success'] = True if json_obj['totalCount'] > 0 else False for data in json_obj['data']: self.raw[data['orderId']] = self.FetchObject( billid=data['orderId'], supuid=data['supUid']) return self.raw['response']
class KernelCallbackHTTPExecutor(abstract_executor.AbstractHTTPExecutor): def __init__(self, path, parameters_data, method='POST'): self.host = kara.conf.uts.kernel.host self.port = kara.conf.uts.kernel.port self.path = path self.method = method self.parameters = parameters_data self.extra_params = {} def _data_from_file(self): return isinstance(self.parameters, basestring) and self.parameters.endswith(".py") def _post(self, *args, **kwargs): # import sys; import pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace() # XXX BREAKPOINT payload = self._AbstractHTTPExecutor__build_params() if not self._data_from_file(): payload['template_str'] = json.dumps(payload) self.logging("data from dict.") self.logging("payload: " + payload['template_str']) try: self.raw = dict() self.raw['payload'] = json.loads( payload['template_str'], object_hook=lambda d: Namespace(**d)) request_sub_obj = self.raw['payload'] except Exception, e: raise karaerror.KaraExecutorError( "kernel callback http executor handle template data exception." ) # TODO: don't use eval, has other better style? method = eval("requests." + self.method.lower()) self.raw['response'] = method(self.url, json=json.loads(payload['template_str'])) self.logging("response text \n" + self.raw['response'].text) if self.raw['response'].status_code != 200: raise karaerror.KaraExecutorError( "kernel callback http executor invoke response failure.") try: self.raw['resp_json'] = json.loads( self.raw['response'].text, object_hook=lambda d: Namespace(**d)) self.raw['code'] = self.raw['resp_json'].code except Exception, e: raise karaerror.KaraExecutorError( "kernel callback http executor handle response exception.")
def _post(self, *args, **kwargs): # import sys; import pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace() # XXX BREAKPOINT payload = self._AbstractHTTPExecutor__build_params() if not self._data_from_file(): payload['template_str'] = json.dumps(payload) self.logging("data from dict.") self.logging("payload: " + payload['template_str']) try: self.raw = dict() self.raw['payload'] = json.loads( payload['template_str'], object_hook=lambda d: Namespace(**d)) request_sub_obj = self.raw['payload'].payRequest for item in kara.conf.misc.kernel_payment_keys: if hasattr(request_sub_obj, item): self.raw[item] = getattr(request_sub_obj, item) method_sub_obj = self.raw['payload'].payMethod for item in kara.conf.misc.kernel_method_keys: if hasattr(method_sub_obj, item): self.raw[item] = getattr(method_sub_obj, item) except Exception, e: raise karaerror.KaraExecutorError( "kernel normal http executor handle template data exception.")
def _post(self, *args, **kwargs): payload = self._AbstractHTTPExecutor__build_params() if self.sign_keys: sign_list = [ self.parameters.get(key, '') for key in self.sign_keys ] sign_str = ''.join(sign_list).decode("utf-8").encode( "GBK") + self.post_code md5_obj = hashlib.md5() md5_obj.update(sign_str) payload[self.sign_name] = md5_obj.hexdigest().upper() last_querystring = '&'.join([ '='.join([k, v]) for k, v in payload.items() if isinstance(v, str) ]) self.raw = dict() self.raw['querystring'] = self.url + '?' + last_querystring self.raw['payload'] = payload # TODO: don't use eval, has other better style? method = eval("requests." + self.method.lower()) self.raw['response'] = method(self.url, params=payload) self.raw['resp_text'] = self.raw['response'].text.encode( "utf-8").replace("GB2312", "utf8").replace("gb2312", "utf8") # import pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace() # XXX BREAKPOINT if self.raw['response'].status_code != 200: raise karaerror.KaraExecutorError( "opaStd http executor invoke failure! status code is't 200.") return self.raw['response']
def invoke(self, *args, **kwargs): if self.method == 'POST': return self._post(*args, **kwargs) elif self.method == 'GET': return self._get(*args, **kwargs) elif self.method == 'LOGIN': return self._login(*args, **kwargs) else: raise karaerror.KaraExecutorError("http method do not support.")
def _post(self, *args, **kwargs): # import pudb; pudb.set_trace() # XXX BREAKPOIN payload = self._AbstractHTTPExecutor__build_params() last_querystring = '&'.join([ '='.join([k, v]) for k, v in payload.items() if isinstance(v, str) ]) self.raw = dict() self.raw['querystring'] = self.url + '?' + last_querystring self.raw['payload'] = payload # TODO: don't use eval, has other better style? method = eval("requests." + self.method.lower()) self.raw['response'] = method(self.url, params=payload) # lxml has encoding declare! self.raw['resp_text'] = self.raw['response'].text.encode( "utf-8").replace("GB2312", "utf8").replace("gb2312", "utf8") # import pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace() # XXX BREAKPOINT if self.raw['response'].status_code != 200: raise karaerror.KaraExecutorError( "opaStd http executor invoke failure! status code is't 200.") if self.fetch_billid: if "<retcode>1</retcode>" not in self.raw[ 'resp_text'] or "<err_msg></err_msg>" not in self.raw[ 'resp_text']: raise karaerror.KaraExecutorError( "opaStd http executor invoke result roughly validate in executor failure." ) root = tree.fromstring(self.raw['response'].content) self.raw['billid'] = root.xpath("//orderid")[0].text self.raw['oflinkid'] = self.raw['payload']['sporder_id'] self.raw['userid'] = self.raw['payload']['userid'] if 'game_userid' in self.raw['payload']: self.raw['game_userid'] = self.raw['payload']['game_userid'] return self.raw['response']
def _post(self, *args, **kwargs): # import sys; import pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace() # XXX BREAKPOINT payload = self._AbstractHTTPExecutor__build_params() if not self._data_from_file(): payload['template_str'] = json.dumps(payload) self.logging("data from dict.") self.logging("payload: " + payload['template_str']) try: self.raw = dict() self.raw['payload'] = json.loads( payload['template_str'], object_hook=lambda d: Namespace(**d)) request_sub_obj = self.raw['payload'] except Exception, e: raise karaerror.KaraExecutorError( "kernel callback http executor handle template data exception." )
def _post(self, parameters): payload = self._default_payload payload.update(parameters) last_querystring = '&'.join(['='.join([k, str(v)]) for k, v in payload.items()]) self.raw = dict() self.raw['querystring'] = self.url + '?' + last_querystring self.raw['payload'] = payload # TODO: don't use eval, has other better style? method = eval("requests." + self.method.lower()) self.raw['response'] = result = method(self.url, params=payload) result.string = result.text.encode('utf-8') if result.string != "success": raise karaerror.KaraExecutorError("ofrg callback failure.") self.raw['resp_text'] = result.string return self.raw['response']
def __build_params(self): # import sys; import pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace() # XXX BREAKPOINT if isinstance(self.parameters, basestring) and self.parameters.endswith(".py"): try: opa_ver_path = kara.conf.params.opa.version if self.parameters.startswith( 'opa_') else '' ret_locals = self.extra_params.copy() execfile( join(kara.KARA_HOME, kara.PARAM_FILE_DIR, opa_ver_path, self.parameters), globals(), ret_locals) except Exception, e: raise karaerror.KaraExecutorError(e.message) # OLD code: # payload = vars().copy() # del payload['self'] # NEW code: # for with extra parameters! payload = ret_locals.copy()
try: opa_ver_path = kara.conf.params.opa.version if self.parameters.startswith( 'opa_') else '' ret_locals = self.extra_params.copy() execfile( join(kara.KARA_HOME, kara.PARAM_FILE_DIR, opa_ver_path, self.parameters), globals(), ret_locals) except Exception, e: raise karaerror.KaraExecutorError(e.message) # OLD code: # payload = vars().copy() # del payload['self'] # NEW code: # for with extra parameters! payload = ret_locals.copy() # import sys; import pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace() # XXX BREAKPOINT elif isinstance(self.parameters, dict): payload = self.parameters else: raise karaerror.KaraExecutorError( "parameters is't a dict or a validate python file.") return payload if __name__ == "__main__": pass # vim: set ft=python ai nu et ts=4 sw=4 tw=120: