OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
qml_http_request.hpp
1#pragma once
2#include <ossia/network/context.hpp>
3#include <ossia/network/http/http_client_request.hpp>
4
5#include <ossia-qt/protocols/utils.hpp>
6#include <ossia-qt/qml_protocols.hpp>
7
8#include <verdigris>
9
10namespace ossia::qt
11{
12
13struct qml_protocols_http_answer;
14struct qml_protocols_http_error;
15using request_type = ossia::net::http_client_request<
16 qml_protocols_http_answer, qml_protocols_http_error>;
17
18struct qml_protocols_http_answer
19{
20 static constexpr int reserve_expect = 65536 * 8;
21 QPointer<qml_protocols> self{};
22 QJSValue v;
23
24 // The legacy callback takes the body alone: every final response is
25 // delivered to it, as dropping non-2xx ones would leave the script no way to
26 // observe that anything happened at all. The status goes to the log.
27 void operator()(auto& req, int status, std::string_view str)
28 {
29 if(status < 200 || status >= 300)
30 ossia::logger().error("HTTP Error: status code {}", status);
31
32 ossia::qt::run_async(self.get(), [self = self, v = v, s = QString::fromUtf8(str)] {
33 if(self)
34 if(v.isCallable())
35 v.call({s});
36 });
37 }
38};
39
40// No error channel towards the script: a failure is reported to the log, never
41// to the success callback with a made-up body.
42struct qml_protocols_http_error
43{
44 void operator()(auto& req, std::string_view msg)
45 {
46 ossia::logger().error("HTTP request failed: {}", msg);
47 }
48};
49
50// --- New fetch() types (full HTTP client) ---
51
52struct qml_protocols_fetch_answer;
53struct qml_protocols_fetch_error;
54using fetch_request_type = ossia::net::http_client_request<
55 qml_protocols_fetch_answer, qml_protocols_fetch_error>;
56
57struct qml_protocols_fetch_answer
58{
59 static constexpr int reserve_expect = 65536 * 8;
60 QPointer<qml_protocols> self{};
61 QJSValue onResponse;
62 void operator()(auto& req, int status, std::string_view str)
63 {
64 ossia::qt::run_async(
65 self.get(),
66 [self = self, v = onResponse, status, s = QString::fromUtf8(str)] {
67 if(self)
68 if(v.isCallable())
69 {
70 auto engine = qjsEngine(self.get());
71 if(engine)
72 v.call({QJSValue(status), engine->toScriptValue(s)});
73 }
74 });
75 }
76};
77
78struct qml_protocols_fetch_error
79{
80 QPointer<qml_protocols> self{};
81 QJSValue onError;
82 void operator()(auto& req, std::string_view msg)
83 {
84 ossia::qt::run_async(
85 self.get(), [self = self, v = onError, m = QString::fromUtf8(msg)] {
86 if(self)
87 if(v.isCallable())
88 v.call({m});
89 });
90 }
91};
92
93}
Definition qml_device.cpp:43
spdlog::logger & logger() noexcept
Where the errors will be logged. Default is stderr.
Definition context.cpp:120