Commit ff7baf34 authored by Sam Rushing's avatar Sam Rushing

client: add support for Basic Auth

client: getattr proxy object
parent 499d1860
......@@ -3,6 +3,7 @@
import json
import urlparse
import coro
import base64
from coro.http.client import client as http_client
from coro.http.protocol import header_set
......@@ -27,20 +28,49 @@ class json_rpc_handler:
request.push (json.dumps (rd))
request.done()
class Error (Exception):
pass
class proxy:
def __init__ (self, remote, name):
self.remote = remote
self.name = name
def __call__ (self, *args, **kwargs):
return self.remote.invoke (self.name, *args, **kwargs)
class json_rpc_remote:
def __init__ (self, url):
def __init__ (self, url, auth_info=None):
self.url = url
self.url_ob = urlparse.urlparse (url)
assert (self.url_ob.scheme == 'http')
self.counter = 0
if auth_info:
# basic only
self.auth = base64.b64encode ('%s:%s' % auth_info)
else:
self.auth = None
def invoke (self, name, *args):
def invoke (self, name, *args, **kwargs):
c = http_client (self.url_ob.hostname, self.url_ob.port)
jreq = json.dumps ({'method': name, 'params':list(args), 'id':self.counter})
if kwargs:
assert (not args) # no way to mix positional & named args
params = kwargs
else:
params = list (args)
jreq = json.dumps ({'method': name, 'params':params, 'id':self.counter})
self.counter += 1
if self.auth:
req = c.POST (self.url_ob.path, jreq, Authorization='Basic %s' % (self.auth,))
else:
req = c.POST (self.url_ob.path, jreq)
if req.reply_code == '200':
jrep = json.loads (req.content)
return jrep['result']
else:
raise Error ((req.reply_code, req.content))
def __getattr__ (self, name):
return proxy (self, name)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment