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.hpp>
4#include <ossia/network/http/http_client_request.hpp>
5
6#include <ossia-qt/protocols/utils.hpp>
7#include <ossia-qt/qml_protocols.hpp>
8
9#include <verdigris>
10
11namespace ossia::qt
12{
13
14struct qml_protocols_http_answer;
15struct qml_protocols_http_error;
16using request_type
17 = ossia::net::http_get_request<qml_protocols_http_answer, qml_protocols_http_error>;
18
19struct qml_protocols_http_answer
20{
21 static constexpr int reserve_expect = 65536 * 8;
22 QPointer<qml_protocols> self{};
23 QJSValue v;
24 void operator()(auto& req, std::string_view str)
25 {
26 ossia::qt::run_async(self.get(), [self = self, v = v, s = QString::fromUtf8(str)] {
27 if(self)
28 if(v.isCallable())
29 v.call({s});
30 });
31 }
32};
33struct qml_protocols_http_error
34{
35 void operator()(auto& self) { }
36};
37
38// --- New fetch() types (full HTTP client) ---
39
40struct qml_protocols_fetch_answer;
41struct qml_protocols_fetch_error;
42using fetch_request_type = ossia::net::http_client_request<
43 qml_protocols_fetch_answer, qml_protocols_fetch_error>;
44
45struct qml_protocols_fetch_answer
46{
47 static constexpr int reserve_expect = 65536 * 8;
48 QPointer<qml_protocols> self{};
49 QJSValue onResponse;
50 void operator()(auto& req, int status, std::string_view str)
51 {
52 ossia::qt::run_async(
53 self.get(),
54 [self = self, v = onResponse, status, s = QString::fromUtf8(str)] {
55 if(self)
56 if(v.isCallable())
57 {
58 auto engine = qjsEngine(self.get());
59 if(engine)
60 v.call({QJSValue(status), engine->toScriptValue(s)});
61 }
62 });
63 }
64};
65
66struct qml_protocols_fetch_error
67{
68 QPointer<qml_protocols> self{};
69 QJSValue onError;
70 void operator()(auto& req, std::string_view msg)
71 {
72 ossia::qt::run_async(
73 self.get(), [self = self, v = onError, m = QString::fromUtf8(msg)] {
74 if(self)
75 if(v.isCallable())
76 v.call({m});
77 });
78 }
79};
80
81}
Definition qml_device.cpp:43