Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/authproxy.py
Show First 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | if isinstance(o, decimal.Decimal): | ||||
return str(o) | return str(o) | ||||
raise TypeError(repr(o) + " is not JSON serializable") | raise TypeError(repr(o) + " is not JSON serializable") | ||||
class AuthServiceProxy(): | class AuthServiceProxy(): | ||||
__id_count = 0 | __id_count = 0 | ||||
# ensure_ascii: escape unicode as \uXXXX, passed to json.dumps | # ensure_ascii: escape unicode as \uXXXX, passed to json.dumps | ||||
def __init__(self, service_url, service_name=None, timeout=HTTP_TIMEOUT, connection=None, ensure_ascii=True): | def __init__(self, service_url, service_name=None, | ||||
timeout=HTTP_TIMEOUT, connection=None, ensure_ascii=True): | |||||
self.__service_url = service_url | self.__service_url = service_url | ||||
self._service_name = service_name | self._service_name = service_name | ||||
self.ensure_ascii = ensure_ascii # can be toggled on the fly by tests | self.ensure_ascii = ensure_ascii # can be toggled on the fly by tests | ||||
self.__url = urllib.parse.urlparse(service_url) | self.__url = urllib.parse.urlparse(service_url) | ||||
port = 80 if self.__url.port is None else self.__url.port | port = 80 if self.__url.port is None else self.__url.port | ||||
user = None if self.__url.username is None else self.__url.username.encode( | user = None if self.__url.username is None else self.__url.username.encode( | ||||
'utf8') | 'utf8') | ||||
passwd = None if self.__url.password is None else self.__url.password.encode( | passwd = None if self.__url.password is None else self.__url.password.encode( | ||||
Show All 12 Lines | def __init__(self, service_url, service_name=None, | ||||
self.__url.hostname, port, timeout=timeout) | self.__url.hostname, port, timeout=timeout) | ||||
def __getattr__(self, name): | def __getattr__(self, name): | ||||
if name.startswith('__') and name.endswith('__'): | if name.startswith('__') and name.endswith('__'): | ||||
# Python internal stuff | # Python internal stuff | ||||
raise AttributeError | raise AttributeError | ||||
if self._service_name is not None: | if self._service_name is not None: | ||||
name = "{}.{}".format(self._service_name, name) | name = "{}.{}".format(self._service_name, name) | ||||
return AuthServiceProxy(self.__service_url, name, connection=self.__conn) | return AuthServiceProxy( | ||||
self.__service_url, name, connection=self.__conn) | |||||
def _request(self, method, path, postdata): | def _request(self, method, path, postdata): | ||||
''' | ''' | ||||
Do a HTTP request, with retry if we get disconnected (e.g. due to a timeout). | Do a HTTP request, with retry if we get disconnected (e.g. due to a timeout). | ||||
This is a workaround for https://bugs.python.org/issue3566 which is fixed in Python 3.5. | This is a workaround for https://bugs.python.org/issue3566 which is fixed in Python 3.5. | ||||
''' | ''' | ||||
headers = {'Host': self.__url.hostname, | headers = {'Host': self.__url.hostname, | ||||
'User-Agent': USER_AGENT, | 'User-Agent': USER_AGENT, | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | def _get_response(self): | ||||
if "error" in response and response["error"] is None: | if "error" in response and response["error"] is None: | ||||
log.debug("<-{}- [{:.6f}] {}".format(response["id"], elapsed, json.dumps( | log.debug("<-{}- [{:.6f}] {}".format(response["id"], elapsed, json.dumps( | ||||
response["result"], default=EncodeDecimal, ensure_ascii=self.ensure_ascii))) | response["result"], default=EncodeDecimal, ensure_ascii=self.ensure_ascii))) | ||||
else: | else: | ||||
log.debug("<-- [{:.6f}] {}".format(elapsed, responsedata)) | log.debug("<-- [{:.6f}] {}".format(elapsed, responsedata)) | ||||
return response | return response | ||||
def __truediv__(self, relative_uri): | def __truediv__(self, relative_uri): | ||||
return AuthServiceProxy("{}/{}".format(self.__service_url, relative_uri), self._service_name, connection=self.__conn) | return AuthServiceProxy("{}/{}".format(self.__service_url, | ||||
relative_uri), self._service_name, connection=self.__conn) |