Successfully send post request to rust
This commit is contained in:
parent
054c30a695
commit
d1ada88657
@ -18,8 +18,8 @@ import flask_cors # type: ignore
|
|||||||
from flask import Response, request
|
from flask import Response, request
|
||||||
from waitress.server import create_server
|
from waitress.server import create_server
|
||||||
|
|
||||||
import aqt
|
|
||||||
import anki.backend_pb2 as pb
|
import anki.backend_pb2 as pb
|
||||||
|
import aqt
|
||||||
from anki import hooks
|
from anki import hooks
|
||||||
from anki.rsbackend import from_json_bytes
|
from anki.rsbackend import from_json_bytes
|
||||||
from anki.utils import devMode
|
from anki.utils import devMode
|
||||||
@ -260,16 +260,10 @@ def graph_preferences() -> bytes:
|
|||||||
return aqt.mw.col.backend.graphs_preferences()
|
return aqt.mw.col.backend.graphs_preferences()
|
||||||
|
|
||||||
|
|
||||||
def set_graph_preferences() -> bytes:
|
def set_graph_preferences() -> None:
|
||||||
data = from_json_bytes(request.data)
|
input = pb.GraphsPreferencesOut()
|
||||||
|
input.ParseFromString(request.data)
|
||||||
underscore_data = {
|
aqt.mw.col.backend.set_graphs_preferences(input=input)
|
||||||
"calendar_first_day_of_week": data["calendarFirstDayOfWeek"],
|
|
||||||
"card_counts_separate_inactive": data["cardCountsSeparateInactive"],
|
|
||||||
}
|
|
||||||
|
|
||||||
data = pb.GraphsPreferencesOut(**underscore_data)
|
|
||||||
return aqt.mw.col.backend.set_graphs_preferences(input=data)
|
|
||||||
|
|
||||||
|
|
||||||
def congrats_info() -> bytes:
|
def congrats_info() -> bytes:
|
||||||
@ -277,14 +271,14 @@ def congrats_info() -> bytes:
|
|||||||
return info.SerializeToString()
|
return info.SerializeToString()
|
||||||
|
|
||||||
|
|
||||||
post_handlers = dict(
|
post_handlers = {
|
||||||
graphData=graph_data,
|
"graphData": graph_data,
|
||||||
graphPreferences=graph_preferences,
|
"graphPreferences": graph_preferences,
|
||||||
setGraphPreferences=set_graph_preferences,
|
"setGraphPreferences": set_graph_preferences,
|
||||||
# pylint: disable=unnecessary-lambda
|
# pylint: disable=unnecessary-lambda
|
||||||
i18nResources=lambda: aqt.mw.col.backend.i18n_resources(),
|
"i18nResources": lambda: aqt.mw.col.backend.i18n_resources(),
|
||||||
congratsInfo=congrats_info,
|
"congratsInfo": congrats_info,
|
||||||
)
|
}
|
||||||
|
|
||||||
|
|
||||||
def handle_post(path: str) -> Response:
|
def handle_post(path: str) -> Response:
|
||||||
@ -293,12 +287,15 @@ def handle_post(path: str) -> Response:
|
|||||||
return flask.make_response("Collection not open", HTTPStatus.NOT_FOUND)
|
return flask.make_response("Collection not open", HTTPStatus.NOT_FOUND)
|
||||||
|
|
||||||
if path in post_handlers:
|
if path in post_handlers:
|
||||||
data = post_handlers[path]()
|
if data := post_handlers[path]():
|
||||||
response = flask.make_response(data)
|
response = flask.make_response(data)
|
||||||
response.headers["Content-Type"] = "application/binary"
|
response.headers["Content-Type"] = "application/binary"
|
||||||
return response
|
else:
|
||||||
|
response = flask.make_response("", HTTPStatus.NO_CONTENT)
|
||||||
else:
|
else:
|
||||||
return flask.make_response(
|
response = flask.make_response(
|
||||||
f"Unhandled post to {path}",
|
f"Unhandled post to {path}",
|
||||||
HTTPStatus.FORBIDDEN,
|
HTTPStatus.FORBIDDEN,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return response
|
||||||
|
@ -27,7 +27,7 @@ export async function getGraphPreferences(): Promise<pb.BackendProto.GraphsPrefe
|
|||||||
|
|
||||||
export async function setGraphPreferences(prefs: pb.BackendProto.GraphsPreferencesOut): Promise<void> {
|
export async function setGraphPreferences(prefs: pb.BackendProto.GraphsPreferencesOut): Promise<void> {
|
||||||
return (async () => {
|
return (async () => {
|
||||||
await postRequest("/_anki/setGraphPreferences", JSON.stringify({ ...prefs }))
|
await postRequest("/_anki/setGraphPreferences", pb.BackendProto.GraphsPreferencesOut.encode(prefs).finish())
|
||||||
})()
|
})()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user