How to use customConvertJson method in autotest

Best Python code snippet using autotest_python

serviceHandler.py

Source:serviceHandler.py Github

copy

Full Screen

...24except django_exceptions.ImproperlyConfigured:25 from json import encoder26 json_encoder = encoder.JSONEncoder()27json_decoder = decoder.JSONDecoder()28def customConvertJson(value):29 """\30 Recursively process JSON values and do type conversions.31 -change floats to ints32 -change unicodes to strs33 """34 if isinstance(value, float):35 return int(value)36 elif isinstance(value, unicode):37 return str(value)38 elif isinstance(value, list):39 return [customConvertJson(item) for item in value]40 elif isinstance(value, dict):41 new_dict = {}42 for key, val in value.iteritems():43 new_key = customConvertJson(key)44 new_val = customConvertJson(val)45 new_dict[new_key] = new_val46 return new_dict47 else:48 return value49def ServiceMethod(fn):50 fn.IsServiceMethod = True51 return fn52class ServiceException(Exception):53 pass54class ServiceRequestNotTranslatable(ServiceException):55 pass56class BadServiceRequest(ServiceException):57 pass58class ServiceMethodNotFound(ServiceException):59 pass60class ServiceHandler(object):61 def __init__(self, service):62 self.service=service63 @classmethod64 def blank_result_dict(cls):65 return {'id': None, 'result': None, 'err': None, 'err_traceback': None}66 def dispatchRequest(self, request):67 """68 Invoke a json RPC call from a decoded json request.69 @param request: a decoded json_request70 @returns a dictionary with keys id, result, err and err_traceback71 """72 results = self.blank_result_dict()73 try:74 results['id'] = self._getRequestId(request)75 methName = request['method']76 args = request['params']77 except KeyError:78 raise BadServiceRequest(request)79 metadata = request.copy()80 metadata['_type'] = 'rpc'81 metadata['rpc_server'] = socket.gethostname()82 try:83 meth = self.findServiceEndpoint(methName)84 results['result'] = self.invokeServiceEndpoint(meth, args)85 except Exception, err:86 results['err_traceback'] = traceback.format_exc()87 results['err'] = err88 return results89 def _getRequestId(self, request):90 try:91 return request['id']92 except KeyError:93 raise BadServiceRequest(request)94 def handleRequest(self, jsonRequest):95 request = self.translateRequest(jsonRequest)96 results = self.dispatchRequest(request)97 return self.translateResult(results)98 @staticmethod99 def translateRequest(data):100 try:101 req = json_decoder.decode(data)102 except:103 raise ServiceRequestNotTranslatable(data)104 req = customConvertJson(req)105 return req106 def findServiceEndpoint(self, name):107 try:108 meth = getattr(self.service, name)109 return meth110 except AttributeError:111 raise ServiceMethodNotFound(name)112 def invokeServiceEndpoint(self, meth, args):113 return meth(*args)114 @staticmethod115 def translateResult(result_dict):116 """117 @param result_dict: a dictionary containing the result, error, traceback118 and id....

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful