MinitScript  0.9.31 PRE-BETA
NetworkMethods.cpp
Go to the documentation of this file.
1 #include <span>
2 
3 #include <unordered_map>
4 
11 
12 using std::span;
13 using std::unordered_map;
14 
16 
18 
22 
23 void NetworkMethods::registerConstants(MinitScript* minitScript) {
24  minitScript->setConstant("$http::HTTP_STATUS_CONTINUE", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_CONTINUE)));
25  minitScript->setConstant("$http::HTTP_STATUS_SWITCHINGPROTOCOLS", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_SWITCHINGPROTOCOLS)));
26  minitScript->setConstant("$http::HTTP_STATUS_PROCESSING", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_PROCESSING)));
27  minitScript->setConstant("$http::HTTP_STATUS_EARLYHINTS", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_EARLYHINTS)));
28  minitScript->setConstant("$http::HTTP_STATUS_OK", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_OK)));
29  minitScript->setConstant("$http::HTTP_STATUS_CREATED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_CREATED)));
30  minitScript->setConstant("$http::HTTP_STATUS_ACCEPTED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_ACCEPTED)));
31  minitScript->setConstant("$http::HTTP_STATUS_NONAUTHORITATIVEINFORMATION", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_NONAUTHORITATIVEINFORMATION)));
32  minitScript->setConstant("$http::HTTP_STATUS_NOCONTENT", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_NOCONTENT)));
33  minitScript->setConstant("$http::HTTP_STATUS_RESETCONTENT", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_RESETCONTENT)));
34  minitScript->setConstant("$http::HTTP_STATUS_PARTIALCONTENT", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_PARTIALCONTENT)));
35  minitScript->setConstant("$http::HTTP_STATUS_MULTISTATUS", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_MULTISTATUS)));
36  minitScript->setConstant("$http::HTTP_STATUS_ALREADYREPORTED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_ALREADYREPORTED)));
37  minitScript->setConstant("$http::HTTP_STATUS_IMUSED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_IMUSED)));
38  minitScript->setConstant("$http::HTTP_STATUS_MULTIPLECHOICES", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_MULTIPLECHOICES)));
39  minitScript->setConstant("$http::HTTP_STATUS_MOVEDPERMANENTLY", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_MOVEDPERMANENTLY)));
40  minitScript->setConstant("$http::HTTP_STATUS_FOUND", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_FOUND)));
41  minitScript->setConstant("$http::HTTP_STATUS_SEEOTHER", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_SEEOTHER)));
42  minitScript->setConstant("$http::HTTP_STATUS_NOTMODIFIED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_NOTMODIFIED)));
43  minitScript->setConstant("$http::HTTP_STATUS_USEPROXY", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_USEPROXY)));
44  minitScript->setConstant("$http::HTTP_STATUS_TEMPORARYREDIRECT", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_TEMPORARYREDIRECT)));
45  minitScript->setConstant("$http::HTTP_STATUS_PERMANENTREDIRECT", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_PERMANENTREDIRECT)));
46  minitScript->setConstant("$http::HTTP_STATUS_BADREQUEST", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_BADREQUEST)));
47  minitScript->setConstant("$http::HTTP_STATUS_UNAUTHORIZED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_UNAUTHORIZED)));
48  minitScript->setConstant("$http::HTTP_STATUS_PAYMENTREQUIRED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_PAYMENTREQUIRED)));
49  minitScript->setConstant("$http::HTTP_STATUS_FORBIDDEN", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_FORBIDDEN)));
50  minitScript->setConstant("$http::HTTP_STATUS_NOTFOUND", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_NOTFOUND)));
51  minitScript->setConstant("$http::HTTP_STATUS_METHODNOTALLOWED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_METHODNOTALLOWED)));
52  minitScript->setConstant("$http::HTTP_STATUS_NOTACCEPTABLE", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_NOTACCEPTABLE)));
53  minitScript->setConstant("$http::HTTP_STATUS_PROXYAUTHENTICATIONREQUIRED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_PROXYAUTHENTICATIONREQUIRED)));
54  minitScript->setConstant("$http::HTTP_STATUS_REQUESTTIMEOUT", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_REQUESTTIMEOUT)));
55  minitScript->setConstant("$http::HTTP_STATUS_CONFLICT", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_CONFLICT)));
56  minitScript->setConstant("$http::HTTP_STATUS_GONE", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_GONE)));
57  minitScript->setConstant("$http::HTTP_STATUS_LENGTHREQUIRED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_LENGTHREQUIRED)));
58  minitScript->setConstant("$http::HTTP_STATUS_PRECONDITIONFAILED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_PRECONDITIONFAILED)));
59  minitScript->setConstant("$http::HTTP_STATUS_CONTENTTOOLARGE", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_CONTENTTOOLARGE)));
60  minitScript->setConstant("$http::HTTP_STATUS_PAYLOADTOOLARGE", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_PAYLOADTOOLARGE)));
61  minitScript->setConstant("$http::HTTP_STATUS_URITOOLONG", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_URITOOLONG)));
62  minitScript->setConstant("$http::HTTP_STATUS_UNSUPPORTEDMEDIATYPE", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_UNSUPPORTEDMEDIATYPE)));
63  minitScript->setConstant("$http::HTTP_STATUS_RANGENOTSATISFIABLE", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_RANGENOTSATISFIABLE)));
64  minitScript->setConstant("$http::HTTP_STATUS_EXPECTATIONFAILED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_EXPECTATIONFAILED)));
65  minitScript->setConstant("$http::HTTP_STATUS_IMATEAPOT", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_IMATEAPOT)));
66  minitScript->setConstant("$http::HTTP_STATUS_MISDIRECTEDREQUEST", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_MISDIRECTEDREQUEST)));
67  minitScript->setConstant("$http::HTTP_STATUS_UNPROCESSABLECONTENT", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_UNPROCESSABLECONTENT)));
68  minitScript->setConstant("$http::HTTP_STATUS_UNPROCESSABLEENTITY", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_UNPROCESSABLEENTITY)));
69  minitScript->setConstant("$http::HTTP_STATUS_LOCKED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_LOCKED)));
70  minitScript->setConstant("$http::HTTP_STATUS_FAILEDDEPENDENCY", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_FAILEDDEPENDENCY)));
71  minitScript->setConstant("$http::HTTP_STATUS_TOOEARLY", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_TOOEARLY)));
72  minitScript->setConstant("$http::HTTP_STATUS_UPGRADEREQUIRED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_UPGRADEREQUIRED)));
73  minitScript->setConstant("$http::HTTP_STATUS_PRECONDITIONREQUIRED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_PRECONDITIONREQUIRED)));
74  minitScript->setConstant("$http::HTTP_STATUS_TOOMANYREQUESTS", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_TOOMANYREQUESTS)));
75  minitScript->setConstant("$http::HTTP_STATUS_REQUESTHEADERFIELDSTOOLARGE", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_REQUESTHEADERFIELDSTOOLARGE)));
76  minitScript->setConstant("$http::HTTP_STATUS_UNAVAILABLEFORLEGALREASONS", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_UNAVAILABLEFORLEGALREASONS)));
77  minitScript->setConstant("$http::HTTP_STATUS_INTERNALSERVERERROR", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_INTERNALSERVERERROR)));
78  minitScript->setConstant("$http::HTTP_STATUS_NOTIMPLEMENTED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_NOTIMPLEMENTED)));
79  minitScript->setConstant("$http::HTTP_STATUS_BADGATEWAY", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_BADGATEWAY)));
80  minitScript->setConstant("$http::HTTP_STATUS_SERVICEUNAVAILABLE", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_SERVICEUNAVAILABLE)));
81  minitScript->setConstant("$http::HTTP_STATUS_GATEWAYTIMEOUT", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_GATEWAYTIMEOUT)));
82  minitScript->setConstant("$http::HTTP_STATUS_HTTPVERSIONNOTSUPPORTED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_HTTPVERSIONNOTSUPPORTED)));
83  minitScript->setConstant("$http::HTTP_STATUS_VARIANTALSONEGOTIATES", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_VARIANTALSONEGOTIATES)));
84  minitScript->setConstant("$http::HTTP_STATUS_INSUFFICIENTSTORAGE", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_INSUFFICIENTSTORAGE)));
85  minitScript->setConstant("$http::HTTP_STATUS_LOOPDETECTED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_LOOPDETECTED)));
86  minitScript->setConstant("$http::HTTP_STATUS_NOTEXTENDED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_NOTEXTENDED)));
87  minitScript->setConstant("$http::HTTP_STATUS_NETWORKAUTHENTICATIONREQUIRED", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_NETWORKAUTHENTICATIONREQUIRED)));
88  minitScript->setConstant("$http::HTTP_STATUS_MAX", MinitScript::Variable(static_cast<int64_t>(_HTTPClient::HTTP_STATUS_MAX)));
89 }
90 
91 void NetworkMethods::registerMethods(MinitScript* minitScript) {
92  // network
93  {
94  //
95  class MethodNetworkHTTPClientHEAD: public MinitScript::Method {
96  private:
97  MinitScript* minitScript { nullptr };
98  public:
99  MethodNetworkHTTPClientHEAD(MinitScript* minitScript):
101  {
102  { .type = MinitScript::TYPE_STRING, .name = "url", .optional = false, .reference = false, .nullable = false },
103  { .type = MinitScript::TYPE_MAP, .name = "queryParameters", .optional = true, .reference = false, .nullable = true },
104  { .type = MinitScript::TYPE_MAP, .name = "headers", .optional = true, .reference = false, .nullable = true },
105  },
106  MinitScript::TYPE_MAP,
107  true
108  ),
109  minitScript(minitScript) {}
110  const string getMethodName() override {
111  return "network.httpclient.head";
112  }
113  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
114  string url;
115  if (arguments.size() <= 3 &&
116  MinitScript::getStringValue(arguments, 0, url, false) == true &&
117  (arguments.size() <= 1 || arguments[1].getType() == MinitScript::TYPE_MAP) &&
118  (arguments.size() <= 2 || arguments[2].getType() == MinitScript::TYPE_MAP)) {
119  // GET parameters
120  unordered_map<string, string> getParameters;
121  if (arguments.size() >= 2) {
122  auto getParameterMapPtr = arguments[1].getMapPointer();
123  if (getParameterMapPtr != nullptr) {
124  for (const auto& [key, value]: *getParameterMapPtr) {
125  getParameters[key] = value->getValueAsString();
126  }
127  }
128  }
129  // headers
130  unordered_map<string, string> headers;
131  if (arguments.size() >= 3) {
132  auto headersMapPtr = arguments[2].getMapPointer();
133  if (headersMapPtr != nullptr) {
134  for (const auto& [key, value]: *headersMapPtr) {
135  headers[key] = value->getValueAsString();
136  }
137  }
138  }
139  //
140  try {
141  _HTTPClient httpClient;
143  httpClient.setGETParameters(getParameters);
144  httpClient.setHeaders(headers);
145  httpClient.setURL(url);
146  httpClient.execute();
147  //
148  returnValue.setType(MinitScript::TYPE_MAP);
149  returnValue.setMapEntry("status", static_cast<int64_t>(httpClient.getStatusCode()));
150  //
151  MinitScript::Variable responseHeaders;
152  responseHeaders.setType(MinitScript::TYPE_MAP);
153  for (const auto& [headerName, headerValue]: httpClient.getResponseHeaders()) {
154  responseHeaders.setMapEntry(headerName, headerValue);
155  }
156  returnValue.setMapEntry("headers", responseHeaders);
157  } catch (_Exception& exception) {
158  _Console::printLine("An error occurred: " + string(exception.what()));
159  }
160  } else {
161  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
162  }
163  }
164  };
165  minitScript->registerMethod(new MethodNetworkHTTPClientHEAD(minitScript));
166  }
167  {
168  //
169  class MethodNetworkHTTPClientGET: public MinitScript::Method {
170  private:
171  MinitScript* minitScript { nullptr };
172  public:
173  MethodNetworkHTTPClientGET(MinitScript* minitScript):
175  {
176  { .type = MinitScript::TYPE_STRING, .name = "url", .optional = false, .reference = false, .nullable = false },
177  { .type = MinitScript::TYPE_MAP, .name = "queryParameters", .optional = true, .reference = false, .nullable = true },
178  { .type = MinitScript::TYPE_MAP, .name = "headers", .optional = true, .reference = false, .nullable = true },
179  },
180  MinitScript::TYPE_MAP,
181  true
182  ),
183  minitScript(minitScript) {}
184  const string getMethodName() override {
185  return "network.httpclient.get";
186  }
187  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
188  string url;
189  if (arguments.size() <= 3 &&
190  MinitScript::getStringValue(arguments, 0, url, false) == true &&
191  (arguments.size() <= 1 || arguments[1].getType() == MinitScript::TYPE_MAP) &&
192  (arguments.size() <= 2 || arguments[2].getType() == MinitScript::TYPE_MAP)) {
193  // GET parameters
194  unordered_map<string, string> getParameters;
195  if (arguments.size() >= 2) {
196  auto getParameterMapPtr = arguments[1].getMapPointer();
197  if (getParameterMapPtr != nullptr) {
198  for (const auto& [key, value]: *getParameterMapPtr) {
199  getParameters[key] = value->getValueAsString();
200  }
201  }
202 
203  }
204  // headers
205  unordered_map<string, string> headers;
206  if (arguments.size() >= 3) {
207  auto headersMapPtr = arguments[2].getMapPointer();
208  if (headersMapPtr != nullptr) {
209  for (const auto& [key, value]: *headersMapPtr) {
210  headers[key] = value->getValueAsString();
211  }
212  }
213  }
214  //
215  try {
216  _HTTPClient httpClient;
218  httpClient.setGETParameters(getParameters);
219  httpClient.setHeaders(headers);
220  httpClient.setURL(url);
221  httpClient.execute();
222  string response;
223  char c;
224  while (httpClient.getResponse().eof() == false) {
225  httpClient.getResponse().get(c);
226  response+= c;
227  }
228  //
229  returnValue.setType(MinitScript::TYPE_MAP);
230  returnValue.setMapEntry("status", static_cast<int64_t>(httpClient.getStatusCode()));
231  returnValue.setMapEntry("response", response);
232  //
233  MinitScript::Variable responseHeaders;
234  responseHeaders.setType(MinitScript::TYPE_MAP);
235  for (const auto& [headerName, headerValue]: httpClient.getResponseHeaders()) {
236  responseHeaders.setMapEntry(headerName, headerValue);
237  }
238  returnValue.setMapEntry("headers", responseHeaders);
239  } catch (_Exception& exception) {
240  _Console::printLine("An error occurred: " + string(exception.what()));
241  }
242  } else {
243  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
244  }
245  }
246  };
247  minitScript->registerMethod(new MethodNetworkHTTPClientGET(minitScript));
248  }
249  {
250  //
251  class MethodNetworkHTTPClientPOST: public MinitScript::Method {
252  private:
253  MinitScript* minitScript { nullptr };
254  public:
255  MethodNetworkHTTPClientPOST(MinitScript* minitScript):
257  {
258  { .type = MinitScript::TYPE_STRING, .name = "url", .optional = false, .reference = false, .nullable = false },
259  { .type = MinitScript::TYPE_PSEUDO_MIXED, .name = "data", .optional = false, .reference = false, .nullable = false },
260  { .type = MinitScript::TYPE_MAP, .name = "queryParameters", .optional = true, .reference = false, .nullable = true },
261  { .type = MinitScript::TYPE_MAP, .name = "headers", .optional = true, .reference = false, .nullable = true },
262  },
263  MinitScript::TYPE_MAP,
264  true
265  ),
266  minitScript(minitScript) {}
267  const string getMethodName() override {
268  return "network.httpclient.post";
269  }
270  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
271  string url;
272  string dataString;
273  if (arguments.size() >= 2 && arguments.size() <= 4 &&
274  MinitScript::getStringValue(arguments, 0, url, false) == true &&
275  (MinitScript::getStringValue(arguments, 1, dataString, true) == true || arguments[2].getType() == MinitScript::TYPE_MAP) &&
276  (arguments.size() <= 2 || arguments[2].getType() == MinitScript::TYPE_MAP) &&
277  (arguments.size() <= 3 || arguments[3].getType() == MinitScript::TYPE_MAP)) {
278  // post parameters
279  unordered_map<string, string> postParameters;
280  if (arguments.size() >= 2) {
281  if (arguments[1].getType() == MinitScript::TYPE_MAP) {
282  auto postParameterMapPtr = arguments[1].getMapPointer();
283  if (postParameterMapPtr != nullptr) {
284  for (const auto& [key, value]: *postParameterMapPtr) {
285  postParameters[key] = value->getValueAsString();
286  }
287  }
288  }
289  }
290  // GET parameters
291  unordered_map<string, string> getParameters;
292  if (arguments.size() >= 3) {
293  auto getParameterMapPtr = arguments[2].getMapPointer();
294  if (getParameterMapPtr != nullptr) {
295  for (const auto& [key, value]: *getParameterMapPtr) {
296  getParameters[key] = value->getValueAsString();
297  }
298  }
299  }
300  // headers
301  unordered_map<string, string> headers;
302  if (arguments.size() >= 4) {
303  auto headersMapPtr = arguments[3].getMapPointer();
304  if (headersMapPtr != nullptr) {
305  for (const auto& [key, value]: *headersMapPtr) {
306  headers[key] = value->getValueAsString();
307  }
308  }
309  }
310  //
311  try {
312  _HTTPClient httpClient;
314  httpClient.setGETParameters(getParameters);
315  httpClient.setHeaders(headers);
316  if (postParameters.empty() == false) {
317  httpClient.setPOSTParameters(postParameters);
318  } else {
319  httpClient.setBody("text/text", dataString);
320  }
321  httpClient.setURL(url);
322  httpClient.execute();
323  string response;
324  char c;
325  while (httpClient.getResponse().eof() == false) {
326  httpClient.getResponse().get(c);
327  response+= c;
328  }
329  //
330  returnValue.setType(MinitScript::TYPE_MAP);
331  returnValue.setMapEntry("status", static_cast<int64_t>(httpClient.getStatusCode()));
332  returnValue.setMapEntry("response", response);
333  //
334  MinitScript::Variable responseHeaders;
335  responseHeaders.setType(MinitScript::TYPE_MAP);
336  for (const auto& [headerName, headerValue]: httpClient.getResponseHeaders()) {
337  responseHeaders.setMapEntry(headerName, headerValue);
338  }
339  returnValue.setMapEntry("headers", responseHeaders);
340  } catch (_Exception& exception) {
341  _Console::printLine("An error occurred: " + string(exception.what()));
342  }
343  } else {
344  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
345  }
346  }
347  };
348  minitScript->registerMethod(new MethodNetworkHTTPClientPOST(minitScript));
349  }
350  {
351  //
352  class MethodNetworkHTTPClientPUT: public MinitScript::Method {
353  private:
354  MinitScript* minitScript { nullptr };
355  public:
356  MethodNetworkHTTPClientPUT(MinitScript* minitScript):
358  {
359  { .type = MinitScript::TYPE_STRING, .name = "url", .optional = false, .reference = false, .nullable = false },
360  { .type = MinitScript::TYPE_PSEUDO_MIXED, .name = "data", .optional = false, .reference = false, .nullable = false },
361  { .type = MinitScript::TYPE_MAP, .name = "queryParameters", .optional = true, .reference = false, .nullable = true },
362  { .type = MinitScript::TYPE_MAP, .name = "headers", .optional = true, .reference = false, .nullable = true },
363  },
364  MinitScript::TYPE_MAP,
365  true
366  ),
367  minitScript(minitScript) {}
368  const string getMethodName() override {
369  return "network.httpclient.put";
370  }
371  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
372  string url;
373  string dataString;
374  if (arguments.size() >= 2 && arguments.size() <= 4 &&
375  MinitScript::getStringValue(arguments, 0, url, false) == true &&
376  (MinitScript::getStringValue(arguments, 1, dataString, true) == true || arguments[2].getType() == MinitScript::TYPE_MAP) &&
377  (arguments.size() <= 2 || arguments[2].getType() == MinitScript::TYPE_MAP) &&
378  (arguments.size() <= 3 || arguments[3].getType() == MinitScript::TYPE_MAP)) {
379  // post parameters
380  unordered_map<string, string> postParameters;
381  if (arguments.size() >= 2) {
382  if (arguments[1].getType() == MinitScript::TYPE_MAP) {
383  auto postParameterMapPtr = arguments[1].getMapPointer();
384  if (postParameterMapPtr != nullptr) {
385  for (const auto& [key, value]: *postParameterMapPtr) {
386  postParameters[key] = value->getValueAsString();
387  }
388  }
389  }
390  }
391  // GET parameters
392  unordered_map<string, string> getParameters;
393  if (arguments.size() >= 3) {
394  auto getParameterMapPtr = arguments[2].getMapPointer();
395  if (getParameterMapPtr != nullptr) {
396  for (const auto& [key, value]: *getParameterMapPtr) {
397  getParameters[key] = value->getValueAsString();
398  }
399  }
400  }
401  // headers
402  unordered_map<string, string> headers;
403  if (arguments.size() >= 4) {
404  auto headersMapPtr = arguments[3].getMapPointer();
405  if (headersMapPtr != nullptr) {
406  for (const auto& [key, value]: *headersMapPtr) {
407  headers[key] = value->getValueAsString();
408  }
409  }
410  }
411  //
412  try {
413  _HTTPClient httpClient;
415  httpClient.setGETParameters(getParameters);
416  httpClient.setHeaders(headers);
417  if (postParameters.empty() == false) {
418  httpClient.setPOSTParameters(postParameters);
419  } else {
420  httpClient.setBody("text/text", dataString);
421  }
422  httpClient.setURL(url);
423  httpClient.execute();
424  string response;
425  char c;
426  while (httpClient.getResponse().eof() == false) {
427  httpClient.getResponse().get(c);
428  response+= c;
429  }
430  //
431  returnValue.setType(MinitScript::TYPE_MAP);
432  returnValue.setMapEntry("status", static_cast<int64_t>(httpClient.getStatusCode()));
433  returnValue.setMapEntry("response", response);
434  //
435  MinitScript::Variable responseHeaders;
436  responseHeaders.setType(MinitScript::TYPE_MAP);
437  for (const auto& [headerName, headerValue]: httpClient.getResponseHeaders()) {
438  responseHeaders.setMapEntry(headerName, headerValue);
439  }
440  returnValue.setMapEntry("headers", responseHeaders);
441  } catch (_Exception& exception) {
442  _Console::printLine("An error occurred: " + string(exception.what()));
443  }
444  } else {
445  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
446  }
447  }
448  };
449  minitScript->registerMethod(new MethodNetworkHTTPClientPUT(minitScript));
450  }
451  {
452  //
453  class MethodNetworkHTTPClientDELETE: public MinitScript::Method {
454  private:
455  MinitScript* minitScript { nullptr };
456  public:
457  MethodNetworkHTTPClientDELETE(MinitScript* minitScript):
459  {
460  { .type = MinitScript::TYPE_STRING, .name = "url", .optional = false, .reference = false, .nullable = false },
461  { .type = MinitScript::TYPE_MAP, .name = "queryParameters", .optional = true, .reference = false, .nullable = true },
462  { .type = MinitScript::TYPE_MAP, .name = "headers", .optional = true, .reference = false, .nullable = true },
463  },
464  MinitScript::TYPE_MAP,
465  true
466  ),
467  minitScript(minitScript) {}
468  const string getMethodName() override {
469  return "network.httpclient.delete";
470  }
471  void executeMethod(span<MinitScript::Variable>& arguments, MinitScript::Variable& returnValue, const MinitScript::SubStatement& subStatement) override {
472  string url;
473  if (arguments.size() <= 3 &&
474  MinitScript::getStringValue(arguments, 0, url, false) == true &&
475  (arguments.size() <= 1 || arguments[1].getType() == MinitScript::TYPE_MAP) &&
476  (arguments.size() <= 2 || arguments[2].getType() == MinitScript::TYPE_MAP)) {
477  // GET parameters
478  unordered_map<string, string> getParameters;
479  if (arguments.size() >= 2) {
480  auto getParameterMapPtr = arguments[1].getMapPointer();
481  if (getParameterMapPtr != nullptr) {
482  for (const auto& [key, value]: *getParameterMapPtr) {
483  getParameters[key] = value->getValueAsString();
484  }
485  }
486  }
487  // headers
488  unordered_map<string, string> headers;
489  if (arguments.size() >= 3) {
490  auto headersMapPtr = arguments[2].getMapPointer();
491  if (headersMapPtr != nullptr) {
492  for (const auto& [key, value]: *headersMapPtr) {
493  headers[key] = value->getValueAsString();
494  }
495  }
496  }
497  //
498  try {
499  _HTTPClient httpClient;
501  httpClient.setGETParameters(getParameters);
502  httpClient.setHeaders(headers);
503  httpClient.setURL(url);
504  httpClient.execute();
505  //
506  returnValue.setType(MinitScript::TYPE_MAP);
507  returnValue.setMapEntry("status", static_cast<int64_t>(httpClient.getStatusCode()));
508  //
509  MinitScript::Variable responseHeaders;
510  responseHeaders.setType(MinitScript::TYPE_MAP);
511  for (const auto& [headerName, headerValue]: httpClient.getResponseHeaders()) {
512  responseHeaders.setMapEntry(headerName, headerValue);
513  }
514  returnValue.setMapEntry("headers", responseHeaders);
515  } catch (_Exception& exception) {
516  _Console::printLine("An error occurred: " + string(exception.what()));
517  }
518  } else {
519  MINITSCRIPT_METHODUSAGE_COMPLAIN(getMethodName());
520  }
521  }
522  };
523  minitScript->registerMethod(new MethodNetworkHTTPClientDELETE(minitScript));
524  }
525 }
minitscript::utilities::Exception _Exception
#define MINITSCRIPT_METHODUSAGE_COMPLAIN(methodName)
Definition: MinitScript.h:59
void setMapEntry(const string &key, const Variable &value, bool _private=false)
Set entry in map with given key.
Definition: MinitScript.h:1993
void setType(VariableType newType)
Set type.
Definition: MinitScript.h:1253
void registerMethod(Method *method)
Register method.
static void setConstant(Variable &variable)
Set variable recursively to be a constant.
Definition: MinitScript.h:4255
MinitScript script network methods.
void setBody(const string &contentType, const string &body)
Set body.
Definition: HTTPClient.h:264
static MINITSCRIPT_STATIC_DLL_IMPEXT const string HTTP_METHOD_GET
Definition: HTTPClient.h:63
static MINITSCRIPT_STATIC_DLL_IMPEXT const string HTTP_METHOD_DELETE
Definition: HTTPClient.h:67
void setPOSTParameters(const unordered_map< string, string > &parameters)
Set POST parameter.
Definition: HTTPClient.h:247
const unordered_map< string, string > & getResponseHeaders()
Definition: HTTPClient.h:315
static MINITSCRIPT_STATIC_DLL_IMPEXT const string HTTP_METHOD_POST
Definition: HTTPClient.h:65
static MINITSCRIPT_STATIC_DLL_IMPEXT const string HTTP_METHOD_PUT
Definition: HTTPClient.h:66
void setGETParameters(const unordered_map< string, string > &parameters)
Set GET parameter.
Definition: HTTPClient.h:231
static MINITSCRIPT_STATIC_DLL_IMPEXT const string HTTP_METHOD_HEAD
Definition: HTTPClient.h:64
void setMethod(const string &method)
Set method.
Definition: HTTPClient.h:167
void setHeaders(const unordered_map< string, string > &headers)
Set request headers.
Definition: HTTPClient.h:215
void setURL(const string &url)
Set URL.
Definition: HTTPClient.h:151
static void printLine()
Print newline to console.
Definition: Console.cpp:66
std::exception Exception
Exception base class.
Definition: Exception.h:18