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
|
||||
|
||||
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
|
||||
=====
|
||||
|
||||
.. 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
|
||||
|
||||
|
||||
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)
|
||||
.. literalinclude:: client-usage.py
|
||||
|
||||
@@ -27,9 +27,9 @@ class Response:
|
||||
|
||||
|
||||
class Method:
|
||||
def __init__(self, function, pre = None, debug: bool = False):
|
||||
def __init__(self, function, middlewares = [], debug: bool = False):
|
||||
self.function = function
|
||||
self.pre = pre
|
||||
self.middlewares = middlewares
|
||||
self.debug = debug
|
||||
|
||||
def __call__(self, query):
|
||||
@@ -38,11 +38,11 @@ class Method:
|
||||
params = query['params']
|
||||
if self.debug:
|
||||
log.error(params)
|
||||
if isinstance(self.pre, list):
|
||||
for item in self.pre:
|
||||
if isinstance(self.middlewares, list):
|
||||
for item in self.middlewares:
|
||||
item(query)
|
||||
elif callable(self.pre):
|
||||
self.pre(query)
|
||||
elif callable(self.middlewares):
|
||||
self.middlewares(query)
|
||||
|
||||
|
||||
if params is None:
|
||||
@@ -76,7 +76,7 @@ class JSONRPC:
|
||||
self.methods = {}
|
||||
self.debug = debug
|
||||
|
||||
def method(self, name: str, pre = None):
|
||||
def method(self, name: str, middlewares = None):
|
||||
"""Декоратор метода
|
||||
"""
|
||||
assert len(name) > 0, 'Не указано имя метода'
|
||||
@@ -84,7 +84,7 @@ class JSONRPC:
|
||||
def wrap(function):
|
||||
method = Method(
|
||||
function = function,
|
||||
pre = pre
|
||||
middlewares = middlewares
|
||||
)
|
||||
self.methods[name] = method
|
||||
return function
|
||||
@@ -216,8 +216,8 @@ class JSONRPC:
|
||||
def __len__(self):
|
||||
return len(self.methods)
|
||||
|
||||
def __setitem__(self, key, function, pre=None):
|
||||
method = Method(function=function, pre=pre)
|
||||
def __setitem__(self, key, function, middlewares=None):
|
||||
method = Method(function=function, middlewares=middlewares)
|
||||
self.methods[key] = method
|
||||
|
||||
def __delitem__(self, key):
|
||||
|
||||
@@ -5,16 +5,15 @@ import jinja2
|
||||
import pathlib
|
||||
import aiohttp_jinja2
|
||||
|
||||
from aiohttp.web import View, Response
|
||||
from aiohttp.web import Response, json_response
|
||||
|
||||
from .. import JSONRPC
|
||||
|
||||
|
||||
pathlib.Path(__file__).parent.resolve()
|
||||
|
||||
class APIHandler:
|
||||
class APIView:
|
||||
def __init__(self, jsonrpc):
|
||||
print(self)
|
||||
self.jsonrpc = jsonrpc
|
||||
|
||||
@aiohttp_jinja2.template('api_browse.html')
|
||||
@@ -27,8 +26,8 @@ class APIHandler:
|
||||
|
||||
async def post(self, request) -> Response:
|
||||
json_data = await request.json()
|
||||
result = jsonrpc(json_data)
|
||||
return Response(result=result)
|
||||
result = self.jsonrpc(json_data)
|
||||
return json_response(result)
|
||||
|
||||
|
||||
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'
|
||||
)
|
||||
)
|
||||
handler = APIHandler(jsonrpc)
|
||||
handler = APIView(jsonrpc)
|
||||
app.router.add_route('GET', rule, handler.get)
|
||||
app.router.add_route('POST', rule, handler.post)
|
||||
|
||||
Reference in New Issue
Block a user