annotate wibble/client/__init__.py @ 11:dbd55e6af4f6

Added test
author Ben Croston <ben@croston.org>
date Mon, 05 Sep 2011 12:13:45 +0100
parents 23743b0f67ab
children 21eb67081c43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
1 #!/usr/bin/env python
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
2
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
3 # Copyright (c) 2011 Ben Croston
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
4 #
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
6 # this software and associated documentation files (the "Software"), to deal in
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
7 # the Software without restriction, including without limitation the rights to
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
8 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
9 # of the Software, and to permit persons to whom the Software is furnished to do
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
10 # so, subject to the following conditions:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
11 #
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
12 # The above copyright notice and this permission notice shall be included in all
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
13 # copies or substantial portions of the Software.
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
14 #
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
21 # SOFTWARE.
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
22
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
23 from uuid import uuid4
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
24 from urlparse import urlparse
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
25 import json
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
26 import httplib
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
27 import copy
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
28 import socket
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
29 import hashlib
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
30 import platform
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
31
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
32 if platform.python_version().startswith('3'):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
33 IS_PY3 = True
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
34 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
35 IS_PY3 = False
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
36
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
37 class _Method(object):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
38 def __init__(self, call, name, username=None, password=None):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
39 self.call = call
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
40 self.name = name
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
41 self._username = username
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
42 self._password = password
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
43
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
44 def __call__(self, *args, **kwargs):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
45 request = {}
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
46 request['id'] = str(uuid4())
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
47 request['method'] = self.name
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
48
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
49 if len(kwargs) is not 0:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
50 params = copy.copy(kwargs)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
51 index = 0
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
52 for arg in args:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
53 params[str(index)] = arg
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
54 index = index + 1
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
55 elif len(args) is not 0:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
56 params = copy.copy(args)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
57 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
58 params = None
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
59 request['params'] = params
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
60
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
61 if self._username is not None:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
62 request['username'] = self._username
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
63 if self._password is not None:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
64 if IS_PY3:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
65 request['password'] = hashlib.md5(self._password.encode()).hexdigest()
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
66 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
67 request['password'] = hashlib.md5(self._password).hexdigest()
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
68
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
69 resp = self.call(json.dumps(request))
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
70 if resp is not None and resp['error'] is None and resp['id'] == request['id']:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
71 return resp['result']
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
72 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
73 raise Exception('This is not supposed to happen -- btc') ########
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
74
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
75 def __getattr__(self, name):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
76 return _Method(self.call, "%s.%s" % (self.name, name), self._username, self._password)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
77
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
78 class _JSONRPCTransport(object):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
79 headers = {'Content-Type':'application/json',
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
80 'Accept':'application/json'}
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
81
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
82 def __init__(self, uri, proxy_uri=None, user_agent=None):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
83 self.headers['User-Agent'] = user_agent if user_agent is not None else 'jsonrpclib'
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
84 if proxy_uri is not None:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
85 self.connection_url = urlparse(proxy_uri)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
86 self.request_path = uri
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
87 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
88 self.connection_url = urlparse(uri)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
89 self.request_path = self.connection_url.path
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
90
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
91 def request(self, request_body):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
92 if self.connection_url.scheme == 'http':
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
93 if self.connection_url.port is None:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
94 port = 80
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
95 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
96 port = self.connection_url.port
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
97 connection = httplib.HTTPConnection(self.connection_url.hostname+':'+str(port))
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
98 elif self.connection_url.scheme == 'https':
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
99 if self.connection_url.port is None:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
100 port = 443
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
101 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
102 port = self.connection_url.port
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
103 connection = httplib.HTTPSConnection(self.connection_url.hostname+':'+str(port))
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
104 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
105 raise Exception('unsupported transport')
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
106 connection.request('POST', self.request_path, body=request_body, headers=self.headers)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
107 return connection.getresponse()
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
108
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
109 class BadRequestException(Exception):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
110 """HTTP 400 - Bad Request"""
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
111 def __init__(self):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
112 Exception.__init__(self,'HTTP 400 - Bad Request')
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
113
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
114 class UnauthorisedException(Exception):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
115 """HTTP 401 - Unauthorised"""
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
116 def __init__(self):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
117 Exception.__init__(self,'HTTP 401 - Unauthorised')
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
118
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
119 class ForbiddenException(Exception):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
120 """HTTP 403 - Forbidden"""
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
121 def __init__(self):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
122 Exception.__init__(self,'HTTP 403 - Forbidden')
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
123
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
124 class NotFoundException(Exception):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
125 """HTTP 404 - Not Found"""
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
126 def __init__(self):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
127 Exception.__init__(self,'HTTP 404 - Not Found')
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
128
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
129 class NetworkSocketException(Exception):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
130 def __init__(self):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
131 Exception.__init__(self,'Network socket exception')
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
132
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
133 class BadGatewayException(Exception):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
134 """HTTP 502 - Bad Gateway"""
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
135 def __init__(self):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
136 Exception.__init__(self,'HTTP 502 - Bad Gateway')
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
137
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
138 class ServerProxy(object):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
139 def __init__(self, uri=None, transport=None, proxy_uri=None, user_agent=None, username=None, password=None):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
140 if uri is None and transport is None:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
141 raise Exception('either uri or transport needs to be specified')
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
142
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
143 if transport is None:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
144 transport = _JSONRPCTransport(uri, proxy_uri=proxy_uri, user_agent=user_agent)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
145 self.__transport = transport
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
146 self._username = username
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
147 self._password = password
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
148
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
149 def __request(self, request):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
150 # call a method on the remote server
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
151 try:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
152 response = self.__transport.request(request)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
153 except socket.error:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
154 raise NetworkSocketException
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
155 if response.status == 200:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
156 if IS_PY3:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
157 return json.loads(response.read().decode())
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
158 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
159 return json.loads(response.read())
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
160 elif response.status == 400:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
161 raise BadRequestException
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
162 elif response.status == 401:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
163 raise UnauthorisedException
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
164 elif response.status == 403:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
165 raise ForbiddenException
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
166 elif response.status == 404:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
167 raise NotFoundException
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
168 elif response.status == 500:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
169 if IS_PY3:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
170 msg = json.loads(response.read().decode())
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
171 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
172 msg = json.loads(response.read())
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
173 raise Exception('JSONRPCError\n%s'%msg['error']['error'])
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
174 elif response.status == 502:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
175 raise BadGatewayException
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
176 else:
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
177 raise Exception('HTTP Status %s'%response.status)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
178
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
179 def __repr__(self):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
180 return (
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
181 "<ServerProxy for %s%s>" %
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
182 (self.__host, self.__handler)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
183 )
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
184
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
185 __str__ = __repr__
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
186
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
187 def __getattr__(self, name):
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
188 # magic method dispatcher
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
189 return _Method(self.__request, name, self._username, self._password)
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 4
diff changeset
190