Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First test!

  • Loading branch information...
commit 3dd442d91636cad79e3a28cdcf9f1556aabed974 1 parent b2140bd
@nycholas nycholas authored
View
28 README.rst
@@ -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
***********
View
3  flask_jsonrpc/__init__.py
@@ -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):
View
22 flask_jsonrpc/site.py
@@ -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
@@ -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:
@@ -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
@@ -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
View
2  run_test.py
@@ -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)
View
0  setup.py 100644 → 100755
File mode changed
Please sign in to comment.
Something went wrong with that request. Please try again.