Skip to content

Commit

Permalink
First test!
Browse files Browse the repository at this point in the history
  • Loading branch information
nycholas committed Dec 14, 2012
1 parent b2140bd commit 3dd442d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
28 changes: 28 additions & 0 deletions README.rst
Expand Up @@ -4,6 +4,34 @@ Flask JSON-RPC
A basic JSON-RPC implementation for your Flask-powered sites based on [django-json-rpc](https://github.com/samuraisam/django-json-rpc).


Running
*******

'''

$ pip install Flask
$ python run_test.py
* Running on http://0.0.0.0:5000/


Testing
*******

'''
$ curl -i -X POST -d '{"jsonrpc": "2.0", "method": "App.index", "params": {}, "id": "1"}' http://localhost:5000/api
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 77
Server: Werkzeug/0.8.3 Python/2.7.3
Date: Fri, 14 Dec 2012 19:26:56 GMT

{
"jsonrpc": "2.0",
"id": "1",
"result": "Welcome to Flask JSON-RPC"
}


Dependecies
***********

Expand Down
3 changes: 2 additions & 1 deletion flask_jsonrpc/__init__.py
Expand Up @@ -146,7 +146,8 @@ def __init__(self, app=None, rule='/api', site=default_site):
self.app = None

def init_app(self, app):
app.add_url_rule(self.rule + '/<method>', '', _site_api, methods=['POST'])
app.add_url_rule(self.rule, '', _site_api, methods=['POST'])
app.add_url_rule(self.rule + '/<method>', '', _site_api, methods=['GET'])

def method(self, name, authenticated=False, safe=False, validate=False, **options):
def decorator(f):
Expand Down
22 changes: 13 additions & 9 deletions flask_jsonrpc/site.py
Expand Up @@ -98,9 +98,13 @@ def __init__(self):
def register(self, name, method):
self.urls[unicode(name)] = method

def extract_id_request(self, raw_data):
def extract_id_request(self, immutable_multi_dict):
raw_data = None
if not immutable_multi_dict is None and immutable_multi_dict.keys():
raw_data = immutable_multi_dict.keys()[0]
if not raw_data is None and raw_data.find('id') != -1:
find_id = re.findall(r'["|\']id["|\']:["|\'](.+?)["|\']', raw_data, re.U)
find_id = re.findall(r'["|\']id["|\']:["|\'](.+?)["|\']',
raw_data.replace(' ', ''), re.U)
return find_id[0] if find_id else None
return None

Expand Down Expand Up @@ -132,9 +136,9 @@ def response_dict(self, request, D, is_batch=False, version_hint='1.0'):
version = version_hint
response = self.empty_response(version=version)
apply_version = {
'2.0': lambda f, r, p: f(r, **encode_kw(p)) if type(p) is dict else f(r, *p),
'1.1': lambda f, r, p: f(r, *encode_arg11(p), **encode_kw(encode_kw11(p))),
'1.0': lambda f, r, p: f(r, *p)
'2.0': lambda f, r, p: f(**encode_kw(p)) if type(p) is dict else f(*p),
'1.1': lambda f, r, p: f(*encode_arg11(p), **encode_kw(encode_kw11(p))),
'1.0': lambda f, r, p: f(*p)
}

try:
Expand Down Expand Up @@ -222,15 +226,15 @@ def dispatch(self, request, method=''):
raise RequestPostError
else:
try:
D = json.loads(request.data)
D = json.loads(request.form.keys()[0])
except:
raise InvalidRequestError

if type(D) is list:
response = [self.response_dict(request, d, is_batch=True, json_encoder=json.JSONEncoder)[0] for d in D]
response = [self.response_dict(request, d, is_batch=True)[0] for d in D]
status = 200
else:
response, status = self.response_dict(request, D, json_encoder=json.JSONEncoder)
response, status = self.response_dict(request, D)
if response is None and (not u'id' in D or D[u'id'] is None): # a notification
response_json = jsonify('')
response_json.status = status
Expand All @@ -251,7 +255,7 @@ def dispatch(self, request, method=''):
status = other_error.status

# extract id the request
json_request_id = self.extract_id_request(request.data)
json_request_id = self.extract_id_request(request.form)
response['id'] = json_request_id

return response
Expand Down
2 changes: 1 addition & 1 deletion run_test.py
Expand Up @@ -14,4 +14,4 @@ def index():


if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
app.run(host='0.0.0.0', debug=0)
Empty file modified setup.py 100644 → 100755
Empty file.

0 comments on commit 3dd442d

Please sign in to comment.