Mercurial > hg > AuthRPC
changeset 9:23743b0f67ab
Moved client.ServerProxy into client.__init__
author | Ben Croston <ben@croston.org> |
---|---|
date | Sun, 04 Sep 2011 23:44:50 +0100 |
parents | 685479d1f0a7 |
children | 58e764e39492 |
files | wibble/client/ServerProxy.py wibble/client/ServerProxy3.py wibble/client/__init__.py wibble/client/thing.py wibble/tests.py |
diffstat | 5 files changed, 194 insertions(+), 369 deletions(-) [+] |
line wrap: on
line diff
--- a/wibble/client/ServerProxy.py Sun Sep 04 23:37:41 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,190 +0,0 @@ -#!/usr/bin/env python - -# Copyright (c) 2011 Ben Croston -# -# Permission is hereby granted, free of charge, to any person obtaining a copy of -# this software and associated documentation files (the "Software"), to deal in -# the Software without restriction, including without limitation the rights to -# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -# of the Software, and to permit persons to whom the Software is furnished to do -# so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -from uuid import uuid4 -from urlparse import urlparse -import json -import httplib -import copy -import socket -import hashlib -import platform - -if platform.python_version().startswith('3'): - IS_PY3 = True -else: - IS_PY3 = False - -class _Method(object): - def __init__(self, call, name, username=None, password=None): - self.call = call - self.name = name - self._username = username - self._password = password - - def __call__(self, *args, **kwargs): - request = {} - request['id'] = str(uuid4()) - request['method'] = self.name - - if len(kwargs) is not 0: - params = copy.copy(kwargs) - index = 0 - for arg in args: - params[str(index)] = arg - index = index + 1 - elif len(args) is not 0: - params = copy.copy(args) - else: - params = None - request['params'] = params - - if self._username is not None: - request['username'] = self._username - if self._password is not None: - if IS_PY3: - request['password'] = hashlib.md5(self._password.encode()).hexdigest() - else: - request['password'] = hashlib.md5(self._password).hexdigest() - - resp = self.call(json.dumps(request)) - if resp is not None and resp['error'] is None and resp['id'] == request['id']: - return resp['result'] - else: - raise Exception('This is not supposed to happen -- btc') ######## - - def __getattr__(self, name): - return _Method(self.call, "%s.%s" % (self.name, name), self._username, self._password) - -class _JSONRPCTransport(object): - headers = {'Content-Type':'application/json', - 'Accept':'application/json'} - - def __init__(self, uri, proxy_uri=None, user_agent=None): - self.headers['User-Agent'] = user_agent if user_agent is not None else 'jsonrpclib' - if proxy_uri is not None: - self.connection_url = urlparse(proxy_uri) - self.request_path = uri - else: - self.connection_url = urlparse(uri) - self.request_path = self.connection_url.path - - def request(self, request_body): - if self.connection_url.scheme == 'http': - if self.connection_url.port is None: - port = 80 - else: - port = self.connection_url.port - connection = httplib.HTTPConnection(self.connection_url.hostname+':'+str(port)) - elif self.connection_url.scheme == 'https': - if self.connection_url.port is None: - port = 443 - else: - port = self.connection_url.port - connection = httplib.HTTPSConnection(self.connection_url.hostname+':'+str(port)) - else: - raise Exception('unsupported transport') - connection.request('POST', self.request_path, body=request_body, headers=self.headers) - return connection.getresponse() - -class BadRequestException(Exception): - """HTTP 400 - Bad Request""" - def __init__(self): - Exception.__init__(self,'HTTP 400 - Bad Request') - -class UnauthorisedException(Exception): - """HTTP 401 - Unauthorised""" - def __init__(self): - Exception.__init__(self,'HTTP 401 - Unauthorised') - -class ForbiddenException(Exception): - """HTTP 403 - Forbidden""" - def __init__(self): - Exception.__init__(self,'HTTP 403 - Forbidden') - -class NotFoundException(Exception): - """HTTP 404 - Not Found""" - def __init__(self): - Exception.__init__(self,'HTTP 404 - Not Found') - -class NetworkSocketException(Exception): - def __init__(self): - Exception.__init__(self,'Network socket exception') - -class BadGatewayException(Exception): - """HTTP 502 - Bad Gateway""" - def __init__(self): - Exception.__init__(self,'HTTP 502 - Bad Gateway') - -class ServerProxy(object): - def __init__(self, uri=None, transport=None, proxy_uri=None, user_agent=None, username=None, password=None): - if uri is None and transport is None: - raise Exception('either uri or transport needs to be specified') - - if transport is None: - transport = _JSONRPCTransport(uri, proxy_uri=proxy_uri, user_agent=user_agent) - self.__transport = transport - self._username = username - self._password = password - - def __request(self, request): - # call a method on the remote server - try: - response = self.__transport.request(request) - except socket.error: - raise NetworkSocketException - if response.status == 200: - if IS_PY3: - return json.loads(response.read().decode()) - else: - return json.loads(response.read()) - elif response.status == 400: - raise BadRequestException - elif response.status == 401: - raise UnauthorisedException - elif response.status == 403: - raise ForbiddenException - elif response.status == 404: - raise NotFoundException - elif response.status == 500: - if IS_PY3: - msg = json.loads(response.read().decode()) - else: - msg = json.loads(response.read()) - raise Exception('JSONRPCError\n%s'%msg['error']['error']) - elif response.status == 502: - raise BadGatewayException - else: - raise Exception('HTTP Status %s'%response.status) - - def __repr__(self): - return ( - "<ServerProxy for %s%s>" % - (self.__host, self.__handler) - ) - - __str__ = __repr__ - - def __getattr__(self, name): - # magic method dispatcher - return _Method(self.__request, name, self._username, self._password) -
--- a/wibble/client/ServerProxy3.py Sun Sep 04 23:37:41 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,172 +0,0 @@ -#!/usr/bin/env python3 -from uuid import uuid4 -from urllib.parse import urlparse -import json -import http.client -import copy -import socket -import hashlib - -class _Method(object): - def __init__(self, call, name, username=None, password=None): - self.call = call - self.name = name - self._username = username - self._password = password - - def __call__(self, *args, **kwargs): - request = {} - request['id'] = str(uuid4()) - request['method'] = self.name - - if len(kwargs) is not 0: - params = copy.copy(kwargs) - index = 0 - for arg in args: - params[str(index)] = arg - index = index + 1 - elif len(args) is not 0: - params = copy.copy(args) - else: - params = None - request['params'] = params - - if self._username is not None: - request['username'] = self._username - if self._password is not None: - request['password'] = hashlib.md5(self._password.encode()).hexdigest() - - resp = self.call(json.dumps(request)) - if resp is not None and resp['error'] is None and resp['id'] == request['id']: - return resp['result'] - else: - raise Exception('This is not supposed to happen -- btc') ######## - - def __getattr__(self, name): - return _Method(self.call, "%s.%s" % (self.name, name), self._username, self._password) - -class _JSONRPCTransport(object): - headers = {'Content-Type':'application/json', - 'Accept':'application/json'} - - def __init__(self, uri, proxy_uri=None, user_agent=None): - self.headers['User-Agent'] = user_agent if user_agent is not None else 'jsonrpclib' - if proxy_uri is not None: - self.connection_url = urlparse(proxy_uri) - self.request_path = uri - else: - self.connection_url = urlparse(uri) - self.request_path = self.connection_url.path - - def request(self, request_body): - if self.connection_url.scheme == 'http': - if self.connection_url.port is None: - port = 80 - else: - port = self.connection_url.port - connection = http.client.HTTPConnection(self.connection_url.hostname+':'+str(port)) - elif self.connection_url.scheme == 'https': - if self.connection_url.port is None: - port = 443 - else: - port = self.connection_url.port - connection = http.client.HTTPSConnection(self.connection_url.hostname+':'+str(port)) - else: - raise Exception('unsupported transport') - - connection.request('POST', self.request_path, body=request_body, headers=self.headers) - return connection.getresponse() - -class BadRequestException(Exception): - """HTTP 400 - Bad Request""" - def __init__(self): - Exception.__init__(self,'HTTP 400 - Bad Request') - -class UnauthorisedException(Exception): - """HTTP 401 - Unauthorised""" - def __init__(self): - Exception.__init__(self,'HTTP 401 - Unauthorised') - -class ForbiddenException(Exception): - """HTTP 403 - Forbidden""" - def __init__(self): - Exception.__init__(self,'HTTP 403 - Forbidden') - -class NotFoundException(Exception): - """HTTP 404 - Not Found""" - def __init__(self): - Exception.__init__(self,'HTTP 404 - Not Found') - -class NetworkSocketException(Exception): - def __init__(self): - Exception.__init__(self,'Network socket exception') - -class BadGatewayException(Exception): - """HTTP 502 - Bad Gateway""" - def __init__(self): - Exception.__init__(self,'HTTP 502 - Bad Gateway') - -class ServerProxy(object): - def __init__(self, uri=None, transport=None, proxy_uri=None, user_agent=None, username=None, password=None): - if uri is None and transport is None: - raise Exception('either uri or transport needs to be specified') - - if transport is None: - transport = _JSONRPCTransport(uri, proxy_uri=proxy_uri, user_agent=user_agent) - self.__transport = transport - self._username = username - self._password = password - - def __request(self, request): - # call a method on the remote server - try: - response = self.__transport.request(request) - except socket.error: - raise NetworkSocketException - if response.status == 200: - return json.loads(response.read().decode()) - elif response.status == 400: - raise BadRequestException - elif response.status == 401: - raise UnauthorisedException - elif response.status == 403: - raise ForbiddenException - elif response.status == 404: - raise NotFoundException - elif response.status == 500: - msg = json.loads(response.read().decode()) - raise Exception('JSONRPCError\n%s'%msg['error']['error']) - elif response.status == 502: - raise BadGatewayException - else: - raise Exception('HTTP Status %s'%response.status) - - def __repr__(self): - return ( - "<ServerProxy for %s%s>" % - (self.__host, self.__handler) - ) - - __str__ = __repr__ - - def __getattr__(self, name): - # magic method dispatcher - return _Method(self.__request, name, self._username, self._password) - -if __name__ == '__main__': - #### btc fixme - jsonrpc_client = ServerProxy('http://localhost:1337/', username='testuser', password='', user_agent='Py3NotInternetExploiter') - #jsonrpc_client = ServerProxy('https://www.croston.org/test/index.py', - # username='testuser', - # password='', - # user_agent='Py3NotInternetExploiter') - assert jsonrpc_client.api.mymethod() == jsonrpc_client.mymethod() - try: - print(jsonrpc_client.wibble('this should fail')) - except BadRequestException: - pass # test passed - else: - raise Exception('Test failed (calling unknown method)') - - print('All tests passed') -
--- a/wibble/client/__init__.py Sun Sep 04 23:37:41 2011 +0100 +++ b/wibble/client/__init__.py Sun Sep 04 23:44:50 2011 +0100 @@ -0,0 +1,190 @@ +#!/usr/bin/env python + +# Copyright (c) 2011 Ben Croston +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is furnished to do +# so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from uuid import uuid4 +from urlparse import urlparse +import json +import httplib +import copy +import socket +import hashlib +import platform + +if platform.python_version().startswith('3'): + IS_PY3 = True +else: + IS_PY3 = False + +class _Method(object): + def __init__(self, call, name, username=None, password=None): + self.call = call + self.name = name + self._username = username + self._password = password + + def __call__(self, *args, **kwargs): + request = {} + request['id'] = str(uuid4()) + request['method'] = self.name + + if len(kwargs) is not 0: + params = copy.copy(kwargs) + index = 0 + for arg in args: + params[str(index)] = arg + index = index + 1 + elif len(args) is not 0: + params = copy.copy(args) + else: + params = None + request['params'] = params + + if self._username is not None: + request['username'] = self._username + if self._password is not None: + if IS_PY3: + request['password'] = hashlib.md5(self._password.encode()).hexdigest() + else: + request['password'] = hashlib.md5(self._password).hexdigest() + + resp = self.call(json.dumps(request)) + if resp is not None and resp['error'] is None and resp['id'] == request['id']: + return resp['result'] + else: + raise Exception('This is not supposed to happen -- btc') ######## + + def __getattr__(self, name): + return _Method(self.call, "%s.%s" % (self.name, name), self._username, self._password) + +class _JSONRPCTransport(object): + headers = {'Content-Type':'application/json', + 'Accept':'application/json'} + + def __init__(self, uri, proxy_uri=None, user_agent=None): + self.headers['User-Agent'] = user_agent if user_agent is not None else 'jsonrpclib' + if proxy_uri is not None: + self.connection_url = urlparse(proxy_uri) + self.request_path = uri + else: + self.connection_url = urlparse(uri) + self.request_path = self.connection_url.path + + def request(self, request_body): + if self.connection_url.scheme == 'http': + if self.connection_url.port is None: + port = 80 + else: + port = self.connection_url.port + connection = httplib.HTTPConnection(self.connection_url.hostname+':'+str(port)) + elif self.connection_url.scheme == 'https': + if self.connection_url.port is None: + port = 443 + else: + port = self.connection_url.port + connection = httplib.HTTPSConnection(self.connection_url.hostname+':'+str(port)) + else: + raise Exception('unsupported transport') + connection.request('POST', self.request_path, body=request_body, headers=self.headers) + return connection.getresponse() + +class BadRequestException(Exception): + """HTTP 400 - Bad Request""" + def __init__(self): + Exception.__init__(self,'HTTP 400 - Bad Request') + +class UnauthorisedException(Exception): + """HTTP 401 - Unauthorised""" + def __init__(self): + Exception.__init__(self,'HTTP 401 - Unauthorised') + +class ForbiddenException(Exception): + """HTTP 403 - Forbidden""" + def __init__(self): + Exception.__init__(self,'HTTP 403 - Forbidden') + +class NotFoundException(Exception): + """HTTP 404 - Not Found""" + def __init__(self): + Exception.__init__(self,'HTTP 404 - Not Found') + +class NetworkSocketException(Exception): + def __init__(self): + Exception.__init__(self,'Network socket exception') + +class BadGatewayException(Exception): + """HTTP 502 - Bad Gateway""" + def __init__(self): + Exception.__init__(self,'HTTP 502 - Bad Gateway') + +class ServerProxy(object): + def __init__(self, uri=None, transport=None, proxy_uri=None, user_agent=None, username=None, password=None): + if uri is None and transport is None: + raise Exception('either uri or transport needs to be specified') + + if transport is None: + transport = _JSONRPCTransport(uri, proxy_uri=proxy_uri, user_agent=user_agent) + self.__transport = transport + self._username = username + self._password = password + + def __request(self, request): + # call a method on the remote server + try: + response = self.__transport.request(request) + except socket.error: + raise NetworkSocketException + if response.status == 200: + if IS_PY3: + return json.loads(response.read().decode()) + else: + return json.loads(response.read()) + elif response.status == 400: + raise BadRequestException + elif response.status == 401: + raise UnauthorisedException + elif response.status == 403: + raise ForbiddenException + elif response.status == 404: + raise NotFoundException + elif response.status == 500: + if IS_PY3: + msg = json.loads(response.read().decode()) + else: + msg = json.loads(response.read()) + raise Exception('JSONRPCError\n%s'%msg['error']['error']) + elif response.status == 502: + raise BadGatewayException + else: + raise Exception('HTTP Status %s'%response.status) + + def __repr__(self): + return ( + "<ServerProxy for %s%s>" % + (self.__host, self.__handler) + ) + + __str__ = __repr__ + + def __getattr__(self, name): + # magic method dispatcher + return _Method(self.__request, name, self._username, self._password) +
--- a/wibble/client/thing.py Sun Sep 04 23:37:41 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,3 +0,0 @@ -#!/usr/bin/python -print 'thing' -
--- a/wibble/tests.py Sun Sep 04 23:37:41 2011 +0100 +++ b/wibble/tests.py Sun Sep 04 23:44:50 2011 +0100 @@ -34,7 +34,7 @@ ##### client vvv ##### class NotAuthTest(unittest.TestCase): def runTest(self): - from client.ServerProxy import ServerProxy, UnauthorisedException + from client import ServerProxy, UnauthorisedException self.client = ServerProxy('http://localhost:1337/', username='testuser', password='s3cr3t', @@ -44,7 +44,7 @@ class WibbleTests(unittest.TestCase): def setUp(self): - from client.ServerProxy import ServerProxy + from client import ServerProxy self.client = ServerProxy('http://localhost:1337/', username='testuser', password='s3cr3t', @@ -61,7 +61,7 @@ class BadRequestTest(WibbleTests): def runTest(self): - from client.ServerProxy import BadRequestException + from client import BadRequestException with self.assertRaises(BadRequestException): self.client.FunctionDoesNotExist() @@ -98,7 +98,7 @@ if __name__ == '__main__': import sys - if platform.python_version().startswith('2 ') and 'serve' in sys.argv: + if platform.python_version().startswith('2') and 'serve' in sys.argv: print 'Listening on port 1337 (Ctrl-C qo quit)...' server = make_server() try: