| 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 urllib |
|---|
| 23 |
from jsonrpc.json import dumps, loads |
|---|
| 24 |
|
|---|
| 25 |
class JSONRPCException(Exception): |
|---|
| 26 |
def __init__(self, rpcError): |
|---|
| 27 |
Exception.__init__(self) |
|---|
| 28 |
self.error = rpcError |
|---|
| 29 |
|
|---|
| 30 |
class ServiceProxy(object): |
|---|
| 31 |
def __init__(self, serviceURL, serviceName=None): |
|---|
| 32 |
self.__serviceURL = serviceURL |
|---|
| 33 |
self.__serviceName = serviceName |
|---|
| 34 |
|
|---|
| 35 |
def __getattr__(self, name): |
|---|
| 36 |
if self.__serviceName != None: |
|---|
| 37 |
name = "%s.%s" % (self.__serviceName, name) |
|---|
| 38 |
return ServiceProxy(self.__serviceURL, name) |
|---|
| 39 |
|
|---|
| 40 |
def __call__(self, *args): |
|---|
| 41 |
postdata = dumps({"method": self.__serviceName, 'params': args, 'id':'jsonrpc'}) |
|---|
| 42 |
respdata = urllib.urlopen(self.__serviceURL, postdata).read() |
|---|
| 43 |
resp = loads(respdata) |
|---|
| 44 |
if resp['error'] != None: |
|---|
| 45 |
raise JSONRPCException(resp['error']) |
|---|
| 46 |
else: |
|---|
| 47 |
return resp['result'] |
|---|
| 48 |
|
|---|
| 49 |
|
|---|