Skip to content

Commit

Permalink
swift_rpc client add base64 support
Browse files Browse the repository at this point in the history
  • Loading branch information
rfyiamcool committed Nov 12, 2015
1 parent 97f29f7 commit b5135c2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
19 changes: 14 additions & 5 deletions swift_rpc/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import json
import base64
import requests
from urlparse import urljoin

class _RPC(object):
__HEADERS__ = {'User-Agent': 'swift_rpc','Content-Type':'application/json'}

def __init__(self, server, name):
def __init__(self, server, name, encryption):
self.encryption = encryption
self.__HEADERS__ = {'User-Agent': 'swift_rpc','Content-Type':'application/json'}
if encryption:
self.__HEADERS__ = {'User-Agent': 'swift_rpc','Content-Type':'application/json','Encryption':encryption}

self._name = name
self._url = urljoin(server, name)

def __call__(self, *args, **kwargs):
params = {}
params['args'] = args
params['kwargs'] = kwargs
post_data = json.dumps(params)
if self.encryption:
post_data = base64.encodestring(post_data)
try:
resp = requests.get(self._url, data=json.dumps(params), headers=self.__HEADERS__)
resp = requests.get(self._url, data=post_data, headers=self.__HEADERS__)
except Exception as e:
raise RPCClient.FailedCall(e)

Expand Down Expand Up @@ -43,7 +51,8 @@ class MissingMethod(Exception): pass
'_getAttributeNames',
]

def __init__(self, server, unallowed_calls=[], load_remotes=True):
def __init__(self, server,encryption=None, unallowed_calls=[], load_remotes=True):
self.encryption = encryption
if server.startswith('http'):
self._server = server
else:
Expand All @@ -53,7 +62,7 @@ def __init__(self, server, unallowed_calls=[], load_remotes=True):
self.__loadremoteroutes()

def __send(self, name):
return _RPC(self._server, name)
return _RPC(self._server, name, self.encryption)

def __remoteroutes(self):
return self._getroutes()
Expand Down
1 change: 0 additions & 1 deletion swift_rpc/server/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def args_kwargs(self):
if self.request.headers.get('Content-Type') == "application/json":
de_string = self.request.body
if self.request.headers.get('Encryption') == "base64":
print 123
de_string = base64.decodestring(self.request.body)
data = json.loads(de_string)
args = data.get('args',[])
Expand Down
2 changes: 1 addition & 1 deletion test_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from swift_rpc.client import RPCClient

client = RPCClient('localhost:8080')
client = RPCClient('localhost:8080',encryption='base64')
print client.test('hi')
print client.test_args('welcome','xiaorui.cc',name='nima')

0 comments on commit b5135c2

Please sign in to comment.