def __py_method_init(self): name = self._py_name() service_endpoint_name = decamelize(PyService.py_name(self)).rsplit( '_', 1)[0] service_qname = PyService.py_qname(self) return { '__init__': """\ def __init__(self, api_url, headers=None): %(service_qname)s.__init__(self) if headers is None: headers = {} else: if not isinstance(headers, dict): raise TypeError(headers) headers = headers.copy() api_url = api_url.rstrip('/') if not api_url.endswith('/jsonrpc/%(service_endpoint_name)s'): api_url += '/jsonrpc/%(service_endpoint_name)s' self.__api_url = api_url.rstrip('/') parsed_api_url = urlparse(api_url) parsed_api_url_netloc = parsed_api_url.netloc.split('@', 1) if len(parsed_api_url_netloc) == 2: username_password = parsed_api_url_netloc[0].split(':', 1) if len(username_password) == 2: username, password = username_password netloc = parsed_api_url_netloc[1] headers['Authorization'] = \\ 'Basic ' + \\ base64.b64encode( "%%s:%%s" %% ( username, password ) ) self.__api_url = \\ parsed_api_url.scheme + '://' + netloc + \\ parsed_api_url.path + \\ parsed_api_url.query # auth_handler = urllib2.HTTPBasicAuthHandler() # auth_handler.add_password(realm='Realm', # uri=self.__api_url, # user=username, # passwd=password) # opener = urllib2.build_opener(auth_handler) # urllib2.install_opener(opener) self.__headers = headers self.__logger = logging.getLogger(self.__class__.__module__ + '.' + self.__class__.__name__) self.__next_id = 1 """ % locals() }
def _py_method_init(self): api_url_default = self._parent_generator()._api_url_default if api_url_default is not None: api_url_parameter = 'api_url=None' set_api_url_default = """ if api_url is None: api_url = %(api_url_default)s""" % locals() else: api_url_parameter = 'api_url' set_api_url_default = '' name = self.py_name() service_endpoint_name = decamelize(PyService.py_name(self)).rsplit( '_', 1)[0] service_qname = PyService.py_qname(self) return { '__init__': """\ def __init__(self, %(api_url_parameter)s, headers=None): %(service_qname)s.__init__(self)%(set_api_url_default)s if headers is None: headers = {} else: if not isinstance(headers, dict): raise TypeError(headers) headers = headers.copy() api_url = api_url.rstrip('/') if not api_url.endswith('/jsonrpc/%(service_endpoint_name)s'): api_url += '/jsonrpc/%(service_endpoint_name)s' self.__api_url = api_url.rstrip('/') parsed_api_url = urlparse(api_url) parsed_api_url_netloc = parsed_api_url.netloc.split('@', 1) if len(parsed_api_url_netloc) == 2: username_password = parsed_api_url_netloc[0].split(':', 1) if len(username_password) == 2: username, password = username_password netloc = parsed_api_url_netloc[1] headers['Authorization'] = \\ 'Basic ' + \\ base64.b64encode( "%%s:%%s" %% ( username, password ) ) self.__api_url = \\ parsed_api_url.scheme + '://' + netloc + \\ parsed_api_url.path + \\ parsed_api_url.query self.__headers = headers self.__next_id = 1 """ % locals() }
def _py_method_init(self): api_url_default = self._parent_generator()._api_url_default if api_url_default is not None: api_url_parameter = 'api_url=None' set_api_url_default = """ if api_url is None: api_url = %(api_url_default)s""" % locals() else: api_url_parameter = 'api_url' set_api_url_default = '' name = self.py_name() service_endpoint_name = decamelize(PyService.py_name(self)).rsplit('_', 1)[0] service_qname = PyService.py_qname(self) return {'__init__': """\ def __init__(self, %(api_url_parameter)s, headers=None): %(service_qname)s.__init__(self)%(set_api_url_default)s if headers is None: headers = {} else: if not isinstance(headers, dict): raise TypeError(headers) headers = headers.copy() api_url = api_url.rstrip('/') if not api_url.endswith('/jsonrpc/%(service_endpoint_name)s'): api_url += '/jsonrpc/%(service_endpoint_name)s' self.__api_url = api_url.rstrip('/') parsed_api_url = urlparse(api_url) parsed_api_url_netloc = parsed_api_url.netloc.split('@', 1) if len(parsed_api_url_netloc) == 2: username_password = parsed_api_url_netloc[0].split(':', 1) if len(username_password) == 2: username, password = username_password netloc = parsed_api_url_netloc[1] headers['Authorization'] = \\ 'Basic ' + \\ base64.b64encode( "%%s:%%s" %% ( username, password ) ) self.__api_url = \\ parsed_api_url.scheme + '://' + netloc + \\ parsed_api_url.path + \\ parsed_api_url.query self.__headers = headers self.__next_id = 1 """ % locals()}
def __py_method_init(self): name = self._py_name() service_endpoint_name = decamelize(PyService.py_name(self)).rsplit('_', 1)[0] service_qname = PyService.py_qname(self) return {'__init__': """\ def __init__(self, api_url, headers=None): %(service_qname)s.__init__(self) if headers is None: headers = {} else: if not isinstance(headers, dict): raise TypeError(headers) headers = headers.copy() api_url = api_url.rstrip('/') if not api_url.endswith('/jsonrpc/%(service_endpoint_name)s'): api_url += '/jsonrpc/%(service_endpoint_name)s' self.__api_url = api_url.rstrip('/') parsed_api_url = urlparse(api_url) parsed_api_url_netloc = parsed_api_url.netloc.split('@', 1) if len(parsed_api_url_netloc) == 2: username_password = parsed_api_url_netloc[0].split(':', 1) if len(username_password) == 2: username, password = username_password netloc = parsed_api_url_netloc[1] headers['Authorization'] = \\ 'Basic ' + \\ base64.b64encode( "%%s:%%s" %% ( username, password ) ) self.__api_url = \\ parsed_api_url.scheme + '://' + netloc + \\ parsed_api_url.path + \\ parsed_api_url.query # auth_handler = urllib2.HTTPBasicAuthHandler() # auth_handler.add_password(realm='Realm', # uri=self.__api_url, # user=username, # passwd=password) # opener = urllib2.build_opener(auth_handler) # urllib2.install_opener(opener) self.__headers = headers self.__logger = logging.getLogger(self.__class__.__module__ + '.' + self.__class__.__name__) self.__next_id = 1 """ % locals()}
def py_imports_definition(self): return ['import ' + PyService.py_qname(self).rsplit('.', 1)[0], 'import thryft.protocol.builtins_input_protocol', 'import thryft.protocol.builtins_output_protocol', 'import thryft.protocol.json_input_protocol', 'from urlparse import urlparse', 'import base64', 'import json', 'import logging', 'import re', 'import urllib2', ] + \ PyService.py_imports_definition(self)
def py_repr(self): methods = indent(' ' * 4, "\n".join(self._py_methods())) name = self.py_name() service_qname = PyService.py_qname(self) return """\ class %(name)s(%(service_qname)s): %(methods)s """ % locals()
def __repr__(self): methods = indent(' ' * 4, "\n".join(self.__py_methods())) name = self._py_name() service_qname = PyService.py_qname(self) return """\ class %(name)s(%(service_qname)s): %(methods)s """ % locals()
def py_imports_definition(self, caller_stack=None): libthryft_module_qname = self._parent_generator()._libthryft_module_qname imports = [ 'import ' + PyService.py_qname(self).rsplit('.', 1)[0], "import %(libthryft_module_qname)s.protocol.json_input_protocol" % locals(), "import %(libthryft_module_qname)s.protocol.json_output_protocol" % locals(), 'from urlparse import urlparse', 'import base64', 'import json', 'import urllib2', ] for function in self.functions: imports.extend(function.py_imports_definition(caller_stack=caller_stack)) imports.extend(self._parent_generator()._service_imports_definition) return imports
def py_imports_definition(self, caller_stack=None): libthryft_module_qname = self._parent_generator( )._libthryft_module_qname imports = [ 'import ' + PyService.py_qname(self).rsplit('.', 1)[0], "import %(libthryft_module_qname)s.protocol.json_input_protocol" % locals(), "import %(libthryft_module_qname)s.protocol.json_output_protocol" % locals(), 'from urlparse import urlparse', 'import base64', 'import json', 'import urllib2', ] for function in self.functions: imports.extend( function.py_imports_definition(caller_stack=caller_stack)) imports.extend( self._parent_generator()._service_imports_definition) return imports
def py_name(self): return PyService.py_name(self) + 'JsonRpcClient'
def _py_name(self): return 'JsonRpcClient' + PyService.py_name(self)