Update
This commit is contained in:
@@ -1,35 +0,0 @@
|
|||||||
@ECHO OFF
|
|
||||||
|
|
||||||
pushd %~dp0
|
|
||||||
|
|
||||||
REM Command file for Sphinx documentation
|
|
||||||
|
|
||||||
if "%SPHINXBUILD%" == "" (
|
|
||||||
set SPHINXBUILD=sphinx-build
|
|
||||||
)
|
|
||||||
set SOURCEDIR=source
|
|
||||||
set BUILDDIR=build
|
|
||||||
|
|
||||||
if "%1" == "" goto help
|
|
||||||
|
|
||||||
%SPHINXBUILD% >NUL 2>NUL
|
|
||||||
if errorlevel 9009 (
|
|
||||||
echo.
|
|
||||||
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
|
|
||||||
echo.installed, then set the SPHINXBUILD environment variable to point
|
|
||||||
echo.to the full path of the 'sphinx-build' executable. Alternatively you
|
|
||||||
echo.may add the Sphinx directory to PATH.
|
|
||||||
echo.
|
|
||||||
echo.If you don't have Sphinx installed, grab it from
|
|
||||||
echo.https://www.sphinx-doc.org/
|
|
||||||
exit /b 1
|
|
||||||
)
|
|
||||||
|
|
||||||
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
|
||||||
goto end
|
|
||||||
|
|
||||||
:help
|
|
||||||
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
|
||||||
|
|
||||||
:end
|
|
||||||
popd
|
|
||||||
@@ -30,4 +30,4 @@ def raise_error() -> bool:
|
|||||||
|
|
||||||
jsonrpc['raise.error'] = raise_error
|
jsonrpc['raise.error'] = raise_error
|
||||||
|
|
||||||
app.run(host='0.0.0.0', debug=True)
|
app.run(host='0.0.0.0', port=5500, debug=True)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
Flask
|
Flask
|
||||||
=====
|
=====
|
||||||
|
|
||||||
.. literalinclude:: flask.py
|
.. literalinclude:: example.py
|
||||||
|
|||||||
19
docs/source/client-usage.py
Normal file
19
docs/source/client-usage.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
from jsonrpc.client import Client
|
||||||
|
|
||||||
|
|
||||||
|
request = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "app.endpoint",
|
||||||
|
"params": {"a": 1, "b": 2},
|
||||||
|
"id": "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
client = Client('http://127.0.0.1:5000/api')
|
||||||
|
response = client(request)
|
||||||
|
|
||||||
|
assert 'result' in response, response['error']['message']
|
||||||
|
if 'result' in response:
|
||||||
|
print(response['result'])
|
||||||
|
elif 'error' in response:
|
||||||
|
print(response['result'])
|
||||||
|
print(response)
|
||||||
@@ -1,24 +1,8 @@
|
|||||||
Использование
|
Использование
|
||||||
=============
|
=============
|
||||||
|
|
||||||
.. code-block:: python
|
.. literalinclude:: usage.py
|
||||||
|
|
||||||
from jsonrpc import JSONRPC
|
.. literalinclude:: server-usage.py
|
||||||
|
|
||||||
|
.. literalinclude:: client-usage.py
|
||||||
jsonrpc = JSONRPC()
|
|
||||||
|
|
||||||
@jsonrpc.method('app.endpoint')
|
|
||||||
def app_endpoint(a: int, b: int) -> int:
|
|
||||||
result = a + b
|
|
||||||
return result
|
|
||||||
|
|
||||||
request = {
|
|
||||||
"jsonrpc": "2.0",
|
|
||||||
"method": "app.endpoint",
|
|
||||||
"params": {"a": 1, "b": 2},
|
|
||||||
"id": "1"
|
|
||||||
}
|
|
||||||
response = jsonrpc(request)
|
|
||||||
|
|
||||||
print(response)
|
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ class Response:
|
|||||||
|
|
||||||
|
|
||||||
class Method:
|
class Method:
|
||||||
def __init__(self, function, pre = None, debug: bool = False):
|
def __init__(self, function, middlewares = [], debug: bool = False):
|
||||||
self.function = function
|
self.function = function
|
||||||
self.pre = pre
|
self.middlewares = middlewares
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
|
|
||||||
def __call__(self, query):
|
def __call__(self, query):
|
||||||
@@ -38,11 +38,11 @@ class Method:
|
|||||||
params = query['params']
|
params = query['params']
|
||||||
if self.debug:
|
if self.debug:
|
||||||
log.error(params)
|
log.error(params)
|
||||||
if isinstance(self.pre, list):
|
if isinstance(self.middlewares, list):
|
||||||
for item in self.pre:
|
for item in self.middlewares:
|
||||||
item(query)
|
item(query)
|
||||||
elif callable(self.pre):
|
elif callable(self.middlewares):
|
||||||
self.pre(query)
|
self.middlewares(query)
|
||||||
|
|
||||||
|
|
||||||
if params is None:
|
if params is None:
|
||||||
@@ -76,7 +76,7 @@ class JSONRPC:
|
|||||||
self.methods = {}
|
self.methods = {}
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
|
|
||||||
def method(self, name: str, pre = None):
|
def method(self, name: str, middlewares = None):
|
||||||
"""Декоратор метода
|
"""Декоратор метода
|
||||||
"""
|
"""
|
||||||
assert len(name) > 0, 'Не указано имя метода'
|
assert len(name) > 0, 'Не указано имя метода'
|
||||||
@@ -84,7 +84,7 @@ class JSONRPC:
|
|||||||
def wrap(function):
|
def wrap(function):
|
||||||
method = Method(
|
method = Method(
|
||||||
function = function,
|
function = function,
|
||||||
pre = pre
|
middlewares = middlewares
|
||||||
)
|
)
|
||||||
self.methods[name] = method
|
self.methods[name] = method
|
||||||
return function
|
return function
|
||||||
@@ -216,8 +216,8 @@ class JSONRPC:
|
|||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.methods)
|
return len(self.methods)
|
||||||
|
|
||||||
def __setitem__(self, key, function, pre=None):
|
def __setitem__(self, key, function, middlewares=None):
|
||||||
method = Method(function=function, pre=pre)
|
method = Method(function=function, middlewares=middlewares)
|
||||||
self.methods[key] = method
|
self.methods[key] = method
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
|
|||||||
@@ -5,16 +5,15 @@ import jinja2
|
|||||||
import pathlib
|
import pathlib
|
||||||
import aiohttp_jinja2
|
import aiohttp_jinja2
|
||||||
|
|
||||||
from aiohttp.web import View, Response
|
from aiohttp.web import Response, json_response
|
||||||
|
|
||||||
from .. import JSONRPC
|
from .. import JSONRPC
|
||||||
|
|
||||||
|
|
||||||
pathlib.Path(__file__).parent.resolve()
|
pathlib.Path(__file__).parent.resolve()
|
||||||
|
|
||||||
class APIHandler:
|
class APIView:
|
||||||
def __init__(self, jsonrpc):
|
def __init__(self, jsonrpc):
|
||||||
print(self)
|
|
||||||
self.jsonrpc = jsonrpc
|
self.jsonrpc = jsonrpc
|
||||||
|
|
||||||
@aiohttp_jinja2.template('api_browse.html')
|
@aiohttp_jinja2.template('api_browse.html')
|
||||||
@@ -27,8 +26,8 @@ class APIHandler:
|
|||||||
|
|
||||||
async def post(self, request) -> Response:
|
async def post(self, request) -> Response:
|
||||||
json_data = await request.json()
|
json_data = await request.json()
|
||||||
result = jsonrpc(json_data)
|
result = self.jsonrpc(json_data)
|
||||||
return Response(result=result)
|
return json_response(result)
|
||||||
|
|
||||||
|
|
||||||
def api_init(app, jsonrpc: JSONRPC, rule: str = '/api'):
|
def api_init(app, jsonrpc: JSONRPC, rule: str = '/api'):
|
||||||
@@ -39,6 +38,6 @@ def api_init(app, jsonrpc: JSONRPC, rule: str = '/api'):
|
|||||||
pathlib.Path(__file__).parent.resolve() / 'templates'
|
pathlib.Path(__file__).parent.resolve() / 'templates'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
handler = APIHandler(jsonrpc)
|
handler = APIView(jsonrpc)
|
||||||
app.router.add_route('GET', rule, handler.get)
|
app.router.add_route('GET', rule, handler.get)
|
||||||
app.router.add_route('POST', rule, handler.post)
|
app.router.add_route('POST', rule, handler.post)
|
||||||
|
|||||||
Reference in New Issue
Block a user