Skip to content

Commit

Permalink
added JsonRPC client
Browse files Browse the repository at this point in the history
  • Loading branch information
fiorix committed Nov 22, 2010
1 parent fc2f691 commit 5bf383d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cyclone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
# under the License.

__author__ = "Alexandre Fiori"
__version__ = "0.4"
__version__ = "0.5-rc1"
36 changes: 35 additions & 1 deletion cyclone/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import functools
from twisted.internet import reactor
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Protocol
Expand All @@ -24,7 +25,8 @@
from cyclone.tw.client import Agent
from cyclone.tw.http_headers import Headers
from cyclone.tw.iweb import IBodyProducer
from cyclone.web import _utf8
from cyclone.web import _utf8, HTTPError
from cyclone import escape

agent = Agent(reactor)

Expand Down Expand Up @@ -99,3 +101,35 @@ def fetch(url, *args, **kwargs):
c = HTTPClient(url,*args, **kwargs)
d = c.fetch()
return d

class JsonRPC:
def __init__(self, url):
self.__rpcId = 0
self.__rpcUrl = url

def __getattr__(self, attr):
return functools.partial(self.__rpcRequest, attr)

def __rpcRequest(self, method, *args):
q = escape.json_encode({"method":method, "params":args, "id":self.__rpcId})
self.__rpcId += 1
r = Deferred()
d = fetch(self.__rpcUrl, method="POST", postdata=q)

def _success(response, deferred):
if response.code == 200:
data = escape.json_decode(response.body)
error = data.get("error")
if error:
deferred.errback(Exception(error))
else:
deferred.callback(data.get("result"))
else:
deferred.errback(HTTPError(response.code, response.phrase))

def _failure(failure, deferred):
deferred.errback(failure)

d.addCallback(_success, r)
d.addErrback(_failure, r)
return r
33 changes: 33 additions & 0 deletions demos/rpc/jsonrpc_async_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# coding: utf-8
#
# Copyright 2010 Alexandre Fiori
# based on the original Tornado by Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import cyclone.httpclient
from twisted.internet import defer, reactor

@defer.inlineCallbacks
def main():
cli = cyclone.httpclient.JsonRPC("http://localhost:8888/jsonrpc")
print "echo:", (yield cli.echo("foo bar"))
print "sort:", (yield cli.sort(["foo", "bar"]))
print "count:", (yield cli.count(["foo", "bar"]))
print "geoip_lookup:", (yield cli.geoip_lookup("google.com"))
reactor.stop()

if __name__ == "__main__":
main()
reactor.run()
File renamed without changes.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setuptools.setup(
name="cyclone",
version="0.4",
version="0.5-rc1",
packages=["cyclone", "cyclone.tw", "cyclone.redis"],
# install_requires=["twisted"],
author="fiorix",
Expand Down

0 comments on commit 5bf383d

Please sign in to comment.