Permalink
Browse files

Adding some examples and doc

  • Loading branch information...
1 parent bf726b4 commit 4da6917a1aa02c0d64524db7660f3c815c6c2a7f @nycholas nycholas committed Jul 7, 2014
@@ -0,0 +1,95 @@
+auth
+====
+
+A basic method authenticator.
+
+
+Testing your service
+********************
+
+1. Running
+
+::
+
+ $ python auth.py
+ * Running on http://0.0.0.0:5000/
+
+
+2. Testing
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "App.index", "params": {"username": "flask", "password": "JSON-RPC"}, "id": "1"}' http://localhost:5000/api
+ HTTP/1.0 200 OK
+ Content-Type: application/json
+ Content-Length: 78
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:49:37 GMT
+
+ {
+ "id": "1",
+ "jsonrpc": "2.0",
+ "result": "Welcome to Flask JSON-RPC"
+ }
+
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -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: 502
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:50:14 GMT
+
+ {
+ "error": {
+ "code": -32600,
+ "data": null,
+ "executable": "/usr/bin/python2",
+ "message": "InvalidRequestError: Expecting ':' delimiter: line 1 column 59 (char 58)",
+ "name": "InvalidRequestError",
+ "stack": "Traceback (most recent call last):\n File \"/home/nycholas/project/src/o_lalertom/flask/flask-jsonrpc/examples/../flask_jsonrpc/site.py\", line 281, in dispatch\n raise InvalidRequestError(e.message)\nInvalidRequestError\n"
+ },
+ "id": "1",
+ "result": null
+ }
+
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "App.echo", "params": {"usernme": "flask", "password": "JSON-RPC", "name": "Flask"}, "id": "1"}' http://localhost:5000/api
+ HTTP/1.0 200 OK
+ Content-Type: application/json
+ Content-Length: 64
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:51:15 GMT
+
+ {
+ "id": "1",
+ "jsonrpc": "2.0",
+ "result": "Hello Flask"
+ }
+
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "App.echo", "params": {"username": "flask", "password": "JSON-RPC"}, "id": "1"}' http://localhost:5000/api
+ HTTP/1.0 200 OK
+ Content-Type: application/json
+ Content-Length: 680
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:50:38 GMT
+
+ {
+ "error": {
+ "code": -32602,
+ "data": null,
+ "executable": "/usr/bin/python2",
+ "message": "InvalidParamsError: Not eough params provided for App.echo(String, String, name=str) -> str",
+ "name": "InvalidParamsError",
+ "stack": "Traceback (most recent call last):\n File \"/home/nycholas/project/src/o_lalertom/flask/flask-jsonrpc/examples/../flask_jsonrpc/site.py\", line 197, in response_dict\n validate_params(method, D)\n File \"/home/nycholas/project/src/o_lalertom/flask/flask-jsonrpc/examples/../flask_jsonrpc/site.py\", line 95, in validate_params\n .format(method.json_sig))\nInvalidParamsError\n"
+ },
+ "id": "1",
+ "jsonrpc": "2.0"
+ }
@@ -0,0 +1,56 @@
+authbackend
+===========
+
+A backend method authenticator.
+
+
+Testing your service
+********************
+
+1. Running
+
+::
+
+ $ python authbackend.py
+ * Running on http://0.0.0.0:5000/
+
+
+2. Testing
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "App.index", "params": {"username": "flask", "password": "JSON-RPC"}, "id": "1"}' http://localhost:5000/api
+ HTTP/1.0 200 OK
+ Content-Type: application/json
+ Content-Length: 78
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:53:01 GMT
+
+ {
+ "id": "1",
+ "jsonrpc": "2.0",
+ "result": "Welcome to Flask JSON-RPC"
+ }
+
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "App.index", "params": {"username": "flask"}, "id": "1"}' http://localhost:5000/api
+ HTTP/1.0 200 OK
+ Content-Type: application/json
+ Content-Length: 850
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 13:00:57 GMT
+
+ {
+ "error": {
+ "code": 500,
+ "data": null,
+ "executable": "/usr/bin/python2",
+ "message": "OtherError: global name 'InvalidParamsError' is not defined",
+ "name": "OtherError",
+ "stack": "Traceback (most recent call last):\n File \"/home/nycholas/project/src/o_lalertom/flask/flask-jsonrpc/examples/../flask_jsonrpc/site.py\", line 208, in response_dict\n R = apply_version[version](method, D['params'])\n File \"/home/nycholas/project/src/o_lalertom/flask/flask-jsonrpc/examples/../flask_jsonrpc/site.py\", line 168, in <lambda>\n '2.0': lambda f, p: f(**encode_kw(p)) if type(p) is dict else f(*p),\n File \"authbackend.py\", line 64, in _f\n raise InvalidParamsError('Authenticated methods require at least '\nNameError: global name 'InvalidParamsError' is not defined\n"
+ },
+ "id": "1",
+ "jsonrpc": "2.0"
+ }
@@ -0,0 +1,56 @@
+decorator
+=========
+
+The basic method decorated dealing any extra parameter and do some thing.
+
+
+Testing your service
+********************
+
+1. Running
+
+::
+
+ $ python decorator.py
+ * Running on http://0.0.0.0:5000/
+
+
+2. Testing
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "App.index", "params": {}, "id": "1", "terminal_id": 1}' http://localhost:5000/api
+ HTTP/1.0 200 OK
+ Content-Type: application/json
+ Content-Length: 67
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:31:50 GMT
+
+ {
+ "id": "1",
+ "jsonrpc": "2.0",
+ "result": "Terminal ID: 1"
+ }
+
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "App.index", "params": {}, "id": "1", "terminal_id": 0}' http://localhost:5000/api
+ HTTP/1.0 200 OK
+ Content-Type: application/json
+ Content-Length: 750
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:36:48 GMT
+
+ {
+ "error": {
+ "code": 500,
+ "data": null,
+ "executable": "/usr/bin/python2",
+ "message": "OtherError: Invalid terminal ID",
+ "name": "OtherError",
+ "stack": "Traceback (most recent call last):\n File \"/home/nycholas/project/src/o_lalertom/flask/flask-jsonrpc/examples/../flask_jsonrpc/site.py\", line 208, in response_dict\n R = apply_version[version](method, D['params'])\n File \"/home/nycholas/project/src/o_lalertom/flask/flask-jsonrpc/examples/../flask_jsonrpc/site.py\", line 168, in <lambda>\n '2.0': lambda f, p: f(**encode_kw(p)) if type(p) is dict else f(*p),\n File \"decorator.py\", line 53, in wrapped\n raise OtherError('Invalid terminal ID')\nOtherError\n"
+ },
+ "id": "1",
+ "jsonrpc": "2.0"
+ }
@@ -0,0 +1,64 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (c) 2012-2014, Cenobit Technologies, Inc. http://cenobit.es/
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+# * Neither the name of the Cenobit Technologies nor the names of
+# its contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+import os
+import sys
+
+from flask import Flask, request
+
+PROJECT_DIR, PROJECT_MODULE_NAME = os.path.split(
+ os.path.dirname(os.path.realpath(__file__))
+)
+
+FLASK_JSONRPC_PROJECT_DIR = os.path.join(PROJECT_DIR, os.pardir)
+if os.path.exists(FLASK_JSONRPC_PROJECT_DIR) \
+ and not FLASK_JSONRPC_PROJECT_DIR in sys.path:
+ sys.path.append(FLASK_JSONRPC_PROJECT_DIR)
+
+from flask_jsonrpc import JSONRPC
+from flask_jsonrpc.exceptions import OtherError
+
+app = Flask(__name__)
+jsonrpc = JSONRPC(app, '/api')
+
+def check_terminal_id(fn):
+ def wrapped():
+ terminal_id = int(request.json.get('terminal_id', 0))
+ if terminal_id <= 0:
+ raise OtherError('Invalid terminal ID')
+ return fn()
+ return wrapped
+
+@jsonrpc.method('App.index')
+@check_terminal_id
+def index():
+ return u'Terminal ID: {0}'.format(request.json['terminal_id'])
+
+
+if __name__ == '__main__':
+ app.run(host='0.0.0.0', debug=True)
@@ -0,0 +1,33 @@
+hrx
+===
+
+A minimal application with HRX (Ajax).
+
+
+Testing your service
+********************
+
+1. Running
+
+::
+
+ $ python hrx.py
+ * Running on http://0.0.0.0:5000/
+
+
+2. Testing
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "Hello.index", "id": "1"}' http://localhost:5000/api
+ HTTP/1.0 200 OK
+ Content-Type: application/json
+ Content-Length: 74
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 13:03:23 GMT
+
+ {
+ "id": "1",
+ "jsonrpc": "2.0",
+ "result": "Welcome to Hello API!"
+ }
@@ -0,0 +1,82 @@
+minimal
+=======
+
+A minimal application.
+
+
+Testing your service
+********************
+
+1. Running
+
+::
+
+ $ python minimal.py
+ * Running on http://0.0.0.0:5000/
+
+
+2. Testing
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -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: 78
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:40:08 GMT
+
+ {
+ "id": "1",
+ "jsonrpc": "2.0",
+ "result": "Welcome to Flask JSON-RPC"
+ }
+
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{jsonrpc": "2.0", "method": "App.hello", "params": ["Flask"], "id": "1"}' http://localhost:5000/api
+ HTTP/1.0 200 OK
+ Content-Type: application/json
+ Content-Length: 64
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:41:08 GMT
+
+ {
+ "id": "1",
+ "jsonrpc": "2.0",
+ "result": "Hello Flask"
+ }
+
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "App.notify"}' http://localhost:5000/api
+ HTTP/1.0 204 NO CONTENT
+ Content-Type: application/json
+ Content-Length: 0
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:41:49 GMT
+
+
+::
+
+ $ curl -i -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "App.fails", "params": ["Flask"], "id": "1"}' http://localhost:5000/api
+ HTTP/1.0 200 OK
+ Content-Type: application/json
+ Content-Length: 704
+ Server: Werkzeug/0.8.3 Python/2.7.7
+ Date: Mon, 07 Jul 2014 12:42:40 GMT
+
+ {
+ "error": {
+ "code": 500,
+ "data": null,
+ "executable": "/usr/bin/python2",
+ "message": "OtherError: ",
+ "name": "OtherError",
+ "stack": "Traceback (most recent call last):\n File \"/home/nycholas/project/src/o_lalertom/flask/flask-jsonrpc/examples/../flask_jsonrpc/site.py\", line 208, in response_dict\n R = apply_version[version](method, D['params'])\n File \"/home/nycholas/project/src/o_lalertom/flask/flask-jsonrpc/examples/../flask_jsonrpc/site.py\", line 168, in <lambda>\n '2.0': lambda f, p: f(**encode_kw(p)) if type(p) is dict else f(*p),\n File \"minimal.py\", line 78, in fails\n raise ValueError\nValueError\n"
+ },
+ "id": "1",
+ "jsonrpc": "2.0"
+ }
Oops, something went wrong.

0 comments on commit 4da6917

Please sign in to comment.