added serialized api work

This commit is contained in:
Kevin Froman 2021-01-27 18:15:06 +00:00
parent 6625b7ce19
commit a2da6b8c89
2 changed files with 32 additions and 17 deletions

View File

@ -10,7 +10,7 @@ from httpapi import miscclientapi, onionrsitesapi, apiutils
from httpapi import themeapi from httpapi import themeapi
from httpapi import fileoffsetreader from httpapi import fileoffsetreader
from httpapi.sse.private import private_sse_blueprint from httpapi.sse.private import private_sse_blueprint
from httpapi.serializedapi import serialized_api_bp
""" """
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -44,6 +44,7 @@ def register_private_blueprints(private_api, app):
app.register_blueprint(themeapi.theme_blueprint) app.register_blueprint(themeapi.theme_blueprint)
app.register_blueprint(private_sse_blueprint) app.register_blueprint(private_sse_blueprint)
app.register_blueprint(fileoffsetreader.offset_reader_api) app.register_blueprint(fileoffsetreader.offset_reader_api)
app.register_blueprint(serialized_api_bp)
def _add_events_bp(): def _add_events_bp():
while True: while True:

View File

@ -3,7 +3,7 @@
view and interact with onionr sites view and interact with onionr sites
""" """
from flask import Blueprint, Response, request, abort, g from flask import Blueprint, Response, request, abort, g
import ujson as json import json
""" """
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -32,15 +32,29 @@ def serialized(name: str) -> Response:
attr = getattr(initial, i) attr = getattr(initial, i)
if callable(attr): if callable(attr):
data = json.loads(request.get_json(force=True)) try:
js = request.get_json(force=True)
print('json', js, type(js))
data = json.loads(js)
args = data['args'] args = data['args']
del data['args'] del data['args']
except (TypeError, ValueError) as e:
print(repr(e))
data = {}
args = []
print('data', data)
if data: if data:
print(*args, **data) print(*args, **data)
return Response(attr(*args, **data)) resp = attr(*args, **data)
if isinstance(resp, int):
resp = str(resp)
return Response(resp)
else: else:
print(*args, **data) print(*args, **data)
return Response(attr(*args)) resp = attr(*args)
if isinstance(resp, int):
resp = str(resp)
return Response(resp, content_type='application/octet-stream')
else: else:
if isinstance(attr, int): if isinstance(attr, int):
attr = str(attr) attr = str(attr)