Update
Some checks failed
Test / Builder (push) Failing after 29s

This commit is contained in:
2026-01-22 18:50:42 +03:00
parent 8f29893531
commit 7e45059889
4 changed files with 56 additions and 14 deletions

View File

@@ -2,6 +2,7 @@ __author__ = 'RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru' __email__ = 'remizoffalex@mail.ru'
import json import json
import asyncio
import jinja2 import jinja2
import logging import logging
import aiohttp_jinja2 import aiohttp_jinja2
@@ -21,21 +22,43 @@ class APIHandler:
def __init__( def __init__(
self, self,
jsonrpc, jsonrpc,
debug: bool = False debug: bool = False,
**kwargs
): ):
self.jsonrpc = jsonrpc self.jsonrpc = jsonrpc
self.debug = debug self.debug = debug
self.__kwargs = kwargs
if self.debug: if self.debug:
log.debug('Connect JSON-RPC to aiohttp complete') log.debug('Connect JSON-RPC to aiohttp complete')
@aiohttp_jinja2.template('api_browse.html') @aiohttp_jinja2.template('api_browse.html')
async def get(self, request) -> Response: async def get(self, request) -> Response:
pagedata = { pagedata = {
'title': 'API Browse', 'TITLE': 'API Browse',
'request': request 'REQUEST': request,
'URL': '/api'
} }
for key in self.__kwargs:
pagedata[key] = self.__kwargs[key]
return pagedata return pagedata
# @aiohttp_jinja2.template('api_browse.js')
async def app_js(self, request) -> Response:
pagedata = {
'TITLE': 'API Browse',
'REQUEST': request,
'URL': '/api'
}
for key in self.__kwargs:
pagedata[key] = self.__kwargs[key]
response = await aiohttp_jinja2.render_template_async(
'api_browse.js',
request,
pagedata
)
response.headers['Content-Type'] = 'application/javascript; charset=utf-8'
return response
async def post(self, request) -> Response: async def post(self, request) -> Response:
try: try:
if self.debug: if self.debug:
@@ -61,6 +84,16 @@ class APIHandler:
) )
def template_init(app):
aiohttp_jinja2.setup(
app,
enable_async=True,
loader=jinja2.FileSystemLoader(
workdir / 'templates'
)
)
def api_init(app, jsonrpc: JSONRPC, rule: str = '/api'): def api_init(app, jsonrpc: JSONRPC, rule: str = '/api'):
aiohttp_jinja2.setup( aiohttp_jinja2.setup(
app, app,

View File

@@ -1,15 +1,18 @@
{% extends "skeleton.html" %} {% extends "skeleton.html" %}
{% block content %} {% block content %}
<h3>API (JSON-RPC 2.0)</h3> <h3>API (JSON-RPC 3.0)</h3>
<hr /> <hr />
<p>Браузер для API (JSON-RPC 2.0) поможет просмотреть список поддерживаемых методов, позволит отправить запросы, получить данные и отобразить результаты.</p> <p>Браузер для API (JSON-RPC 3.0) поможет просмотреть список поддерживаемых методов, позволит отправить запросы, получить данные и отобразить результаты.</p>
{% include '/inc/js-stub.html' %} <p>Нужна поддержка JavaScript</p>
{% endblock %} {% endblock %}
<label>request</label>
<pre>{{ REQUEST }}</pre>
{% block breadcrumb %} {% block breadcrumb %}
<ul> <ul>
<li><a href="/"><i class="fa fa-home"></i></a></li> <li><a href="/"><i class="fa fa-home"></i></a></li>

View File

@@ -33,7 +33,7 @@ function APIBrowse() {
function breadcrumbs_render() { function breadcrumbs_render() {
let result = m('ul', {class: 'breadcrumb mt-3'}, [ let result = m('ul', {class: 'breadcrumb mt-3'}, [
m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))), m('li', {class: 'breadcrumb-item'}, m(m.route.Link, {href: '/'}, m('i', {class: 'fa fa-home'}))),
m('li', {class: 'breadcrumb-item active'}, 'API (JSON-RPC 2.0)'), m('li', {class: 'breadcrumb-item active'}, 'API (JSON-RPC 3.0)'),
]); ]);
return result; return result;
}; };
@@ -60,10 +60,10 @@ function APIBrowse() {
} }
console.log(params); console.log(params);
m.request({ m.request({
url: '/api', url: '{{ URL }}',
method: "POST", method: "POST",
body: { body: {
"jsonrpc": "2.0", "jsonrpc": "3.0",
"method": data.current.name, "method": data.current.name,
"params": params, "params": params,
"id": 1 "id": 1
@@ -76,10 +76,10 @@ function APIBrowse() {
}; };
function methods_get() { function methods_get() {
m.request({ m.request({
url: '/api', url: '{{ URL }}',
method: "POST", method: "POST",
body: { body: {
"jsonrpc": "2.0", "jsonrpc": "3.0",
"method": "api.methods", "method": "api.methods",
"id": 1 "id": 1
} }
@@ -145,7 +145,7 @@ function APIBrowse() {
m('pre', `$ curl -i -X POST \ m('pre', `$ curl -i -X POST \
-H "Content-Type: application/json; indent=4" \ -H "Content-Type: application/json; indent=4" \
-d '{ -d '{
"jsonrpc": "2.0", "jsonrpc": "3.0",
"method": "${ data.current.name }", "method": "${ data.current.name }",
"params": { params }, "params": { params },
"id": "1" "id": "1"
@@ -180,11 +180,11 @@ function APIBrowse() {
m('button', {type: "button", class: "btn btn-outline-secondary btn-lg me-2", onclick: function() { panel_show(data.filter.data) }}, m('button', {type: "button", class: "btn btn-outline-secondary btn-lg me-2", onclick: function() { panel_show(data.filter.data) }},
m('i', {class: "fa fa-filter"}) m('i', {class: "fa fa-filter"})
), ),
'API (JSON-RPC 2.0)', 'API (JSON-RPC 3.0)',
]) ])
), ),
m('hr'), m('hr'),
m('p', 'Браузер для API (JSON-RPC 2.0) поможет просмотреть список поддерживаемых методов, позволит отправить запросы, получить данные и отобразить результаты.'), m('p', 'Браузер для API (JSON-RPC 3.0) поможет просмотреть список поддерживаемых методов, позволит отправить запросы, получить данные и отобразить результаты.'),
m(data.filter), m(data.filter),
m('div', {class: 'row', style: 'min-height: 300px;'}, m('div', {class: 'row', style: 'min-height: 300px;'},
m('div', {class: 'col-md-4 overflow-auto position-relative'}, m('div', {class: 'col-md-4 overflow-auto position-relative'},

View File

@@ -0,0 +1,6 @@
let vroot = document.getElementById("app");
m.route.prefix = '';
m.mount(
vroot,
APIBrowse
);