mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1 | |
| 2 | """ |
| 3 | Copyright (c) 2007 Jan-Klaas Kollhof |
| 4 | |
| 5 | This file is part of jsonrpc. |
| 6 | |
| 7 | jsonrpc is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU Lesser General Public License as published by |
| 9 | the Free Software Foundation; either version 2.1 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This software is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public License |
| 18 | along with this software; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | """ |
| 21 | |
| 22 | import urllib2 |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 23 | |
| 24 | class JSONRPCException(Exception): |
| 25 | pass |
| 26 | |
| 27 | class ServiceProxy(object): |
| 28 | def __init__(self, serviceURL, serviceName=None, headers=None): |
| 29 | self.__serviceURL = serviceURL |
| 30 | self.__serviceName = serviceName |
showard | 8e855a2 | 2008-04-15 17:32:20 +0000 | [diff] [blame] | 31 | self.__headers = headers or {} |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 32 | |
| 33 | def __getattr__(self, name): |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 34 | if self.__serviceName is not None: |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 35 | name = "%s.%s" % (self.__serviceName, name) |
| 36 | return ServiceProxy(self.__serviceURL, name, self.__headers) |
| 37 | |
| 38 | def __call__(self, *args, **kwargs): |
jadmanski | 656b3b8 | 2010-01-27 23:42:27 +0000 | [diff] [blame] | 39 | # pull in simplejson imports lazily so that the library isn't required |
| 40 | # unless you actually need to do encoding and decoding |
| 41 | from simplejson import decoder, encoder |
| 42 | |
jadmanski | 08ff9ad | 2010-01-27 23:42:42 +0000 | [diff] [blame^] | 43 | postdata = encoder.JSONEncoder().encode({"method": self.__serviceName, |
| 44 | 'params': args + (kwargs,), |
| 45 | 'id':'jsonrpc'}) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 46 | request = urllib2.Request(self.__serviceURL, data=postdata, |
| 47 | headers=self.__headers) |
| 48 | respdata = urllib2.urlopen(request).read() |
| 49 | try: |
jadmanski | 08ff9ad | 2010-01-27 23:42:42 +0000 | [diff] [blame^] | 50 | resp = decoder.JSONDecoder().decode(respdata) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 51 | except ValueError: |
| 52 | raise JSONRPCException('Error decoding JSON reponse:\n' + respdata) |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 53 | if resp['error'] is not None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 54 | error_message = (resp['error']['name'] + ': ' + |
| 55 | resp['error']['message'] + '\n' + |
| 56 | resp['error']['traceback']) |
| 57 | raise JSONRPCException(error_message) |
| 58 | else: |
| 59 | return resp['result'] |