22
33extern sol::state Lua;
44
5+ std::vector<RemoteRequest> Remote::s_Container = {};
6+
7+ /* ** Lua ***/
58sol::table Remote::fetchHTTP (const std::string& url, sol::table params, sol::table auth)
69{
7- cpr::Parameters cprParams;
8- std::string authUser ( " " ), authPassword ( " " );
10+ cpr::Parameters cprParams = GetParameters (params) ;
11+ cpr::Authentication cprAuth = GetAuthentication (auth );
912
10- for (auto [key, value] : params)
13+ cpr::Response r = cpr::Get (
14+ cpr::Url (url),
15+ cprParams,
16+ cprAuth
17+ );
18+
19+ sol::table resultTable = Lua.create_table ();
20+ resultTable[" status_code" ] = r.status_code ;
21+ resultTable[" error" ] = r.error .code ;
22+ resultTable[" error_message" ] = r.error .message ;
23+ resultTable[" header" ] = sol::as_table (r.header );
24+ resultTable[" text" ] = r.text ;
25+ return resultTable;
26+ }
27+
28+ void Remote::fetchHTTPAsync (sol::function handler, const std::string& url, sol::table params, sol::table auth)
29+ {
30+ cpr::Parameters cprParams = GetParameters (params);
31+ cpr::Authentication cprAuth = GetAuthentication (auth);
32+ s_Container.emplace_back (handler, RemoteRequestType::GET, url, cprParams, cpr::Header{}, cpr::Body (" " ), cpr::Payload{}, cprAuth);
33+ }
34+
35+ sol::table Remote::postHTTP (const std::string& url, sol::table data)
36+ {
37+ cpr::Parameters cprParams;
38+ if (data[" params" ].valid ())
1139 {
12- cpr::Parameter cprParameter = cpr::Parameter (key.as <std::string>(), value.as <std::string>());
13- cprParams.Add (cprParameter);
40+ for (auto [key, value] : data.get <sol::table>(" params" ))
41+ {
42+ cprParams.Add (cpr::Parameter (key.as <std::string>(), value.as <std::string>()));
43+ }
1444 }
1545
16- if (auth.size () == 2 )
46+ cpr::Header cprHeader;
47+ if (data[" header" ].valid ())
1748 {
18- if (auth[" user" ].valid ())
19- authUser = auth[" user" ].get <std::string>();
20-
21- if (auth[" password" ].valid ())
22- authPassword = auth[" password" ].get <std::string>();
49+ for (auto [key, value] : data.get <sol::table>(" header" ))
50+ {
51+ const std::string strKey = key.as <std::string>();
52+ const std::string strVal = value.as <std::string>();
53+ cprHeader.insert ({ strKey, strVal });
54+ }
2355 }
2456
25- cpr::Authentication cprAuth (authUser, authPassword);
57+ cpr::Body cprBody (data.get_or <std::string>(" body" , " " ));
58+
59+ cpr::Payload cprPayload ({});
60+ if (data[" payload" ].valid ())
61+ {
62+ for (auto [key, value] : data.get <sol::table>(" payload" ))
63+ {
64+ cprPayload.Add (cpr::Pair (key.as <std::string>(), value.as <std::string>()));
65+ }
66+ }
2667
27- cpr::Response r = cpr::Get (
68+ cpr::Response r = cpr::Post (
2869 cpr::Url (url),
70+ cprHeader,
71+ cprBody,
2972 cprParams,
30- cprAuth
73+ cprPayload
3174 );
3275
3376 sol::table resultTable = Lua.create_table ();
@@ -39,9 +82,104 @@ sol::table Remote::fetchHTTP(const std::string& url, sol::table params, sol::tab
3982 return resultTable;
4083}
4184
85+ void Remote::postHTTPAsync (sol::function handler, const std::string& url, sol::table data)
86+ {
87+ cpr::Parameters cprParams;
88+ if (data[" params" ].valid ())
89+ {
90+ for (auto [key, value] : data.get <sol::table>(" params" ))
91+ {
92+ cprParams.Add (cpr::Parameter (key.as <std::string>(), value.as <std::string>()));
93+ }
94+ }
95+
96+ cpr::Header cprHeader;
97+ if (data[" header" ].valid ())
98+ {
99+ for (auto [key, value] : data.get <sol::table>(" header" ))
100+ {
101+ const std::string strKey = key.as <std::string>();
102+ const std::string strVal = value.as <std::string>();
103+ cprHeader.insert ({ strKey, strVal });
104+ }
105+ }
106+
107+ cpr::Body cprBody (data.get_or <std::string>(" body" , " " ));
108+
109+ cpr::Payload cprPayload ({});
110+ if (data[" payload" ].valid ())
111+ {
112+ for (auto [key, value] : data.get <sol::table>(" payload" ))
113+ {
114+ cprPayload.Add (cpr::Pair (key.as <std::string>(), value.as <std::string>()));
115+ }
116+ }
117+
118+ s_Container.emplace_back (handler, RemoteRequestType::POST, url, cprParams, cprHeader, cprBody, cprPayload, cpr::Authentication (" " , " " ));
119+ }
120+
121+ /* ** Internal ***/
42122void Remote::Init (sol::state* Lua)
43123{
124+ s_Container.reserve (32 );
125+
44126 sol::usertype<Remote> userdata = Lua->new_usertype <Remote>(" Remote" );
45127
46- userdata[" fetch" ] = &Remote::fetchHTTP;
47- }
128+ userdata[" fetch" ] = sol::overload (&Remote::fetchHTTP, &Remote::fetchHTTPAsync);
129+ userdata[" post" ] = sol::overload (&Remote::postHTTP, &Remote::postHTTPAsync);
130+ }
131+
132+ void Remote::Process (float elapsedTime)
133+ {
134+ if (s_Container.size () == 0 ) return ;
135+ using namespace std ::chrono_literals;
136+
137+ for (auto & it = s_Container.begin (); it != s_Container.end (); it++)
138+ {
139+ if (it->future .wait_for (0s) == std::future_status::ready)
140+ {
141+ auto r = it->future .get ();
142+ sol::table resultTable = Lua.create_table ();
143+ resultTable[" status_code" ] = r.status_code ;
144+ resultTable[" error" ] = r.error .code ;
145+ resultTable[" error_message" ] = r.error .message ;
146+ resultTable[" header" ] = sol::as_table (r.header );
147+ resultTable[" text" ] = r.text ;
148+ it->handler (resultTable);
149+
150+ it = s_Container.erase (it);
151+ if (it == s_Container.end ())
152+ break ;
153+ }
154+ }
155+ }
156+
157+ cpr::Parameters Remote::GetParameters (sol::table& params)
158+ {
159+ cpr::Parameters cprParams;
160+
161+ for (auto [key, value] : params)
162+ {
163+ cpr::Parameter cprParameter = cpr::Parameter (key.as <std::string>(), value.as <std::string>());
164+ cprParams.Add (cprParameter);
165+ }
166+
167+ return cprParams;
168+ }
169+
170+ cpr::Authentication Remote::GetAuthentication (sol::table& auth)
171+ {
172+ std::string authUser (" " ), authPassword (" " );
173+
174+ if (auth.size () == 2 )
175+ {
176+ if (auth[" user" ].valid ())
177+ authUser = auth[" user" ].get <std::string>();
178+
179+ if (auth[" password" ].valid ())
180+ authPassword = auth[" password" ].get <std::string>();
181+ }
182+
183+ cpr::Authentication cprAuth (authUser, authPassword);
184+ return cprAuth;
185+ }
0 commit comments