SOMHunter Core
http.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  * Vit Skrhak <v.skrhak@gmail.com>
7  *
8  * SOMHunter is free software: you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License as published by the Free
10  * Software Foundation, either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef HTTP_H_
23 #define HTTP_H_
24 
25 #include <memory>
26 #include <string>
27 #include <thread>
28 #include <vector>
29 // ---
30 #include <nlohmann/json.hpp>
31 // ---
32 #include "common.h"
33 
34 namespace sh {
35 using ReqCode = std::size_t;
36 enum class RequestType { GET, POST };
37 
38 class Http {
39  // *** METHODS ***
40 public:
41  ~Http() noexcept;
42  // ---
43  void do_POST_async(const std::string& URL, const nlohmann::json& body, const nlohmann::json& headers = {},
44  std::function<void(ReqCode, nlohmann::json)> cb_succ = {}, std::function<void()> cb_err = {});
45  void do_GET_async(const std::string& URL, const nlohmann::json& query, const nlohmann::json& headers = {},
46  std::function<void(ReqCode, nlohmann::json)> cb_succ = {}, std::function<void()> cb_err = {});
47 
48  std::pair<ReqCode, std::vector<uint8_t>> do_request_sync(const RequestType request_method, const std::string& URL,
49  const nlohmann::json& body,
50  const nlohmann::json& headers = {});
51  std::pair<ReqCode, nlohmann::json> do_POST_sync_json(const std::string& URL, const nlohmann::json& body,
52  const nlohmann::json& headers = {});
53  std::pair<ReqCode, nlohmann::json> do_GET_sync_json(const std::string& URL, const nlohmann::json& body,
54  const nlohmann::json& headers = {});
55  std::pair<ReqCode, std::vector<float>> do_GET_sync_floats(const std::string& URL, const nlohmann::json& body,
56  const nlohmann::json& headers);
57  std::tuple<ReqCode, std::vector<float>, std::vector<int>> do_GET_sync_distances(const std::string& URL,
58  const nlohmann::json& body,
59  const nlohmann::json& headers);
60 
61  void set_allow_insecure(bool val) { _allow_insecure = val; };
62  bool get_allow_insecure() const { return _allow_insecure; };
63 
64 private:
66 
67  void prune_threads() {
68  for (size_t i = 0; i < submit_threads.size();) {
69  if (*finish_flags[i]) {
70  submit_threads[i].join();
71  submit_threads.erase(submit_threads.begin() + i);
72  finish_flags.erase(finish_flags.begin() + i);
73  } else {
74  ++i;
75  }
76  }
77  }
78 
79  // *** MEMBER VARIABLES ***
80 private:
81  std::vector<std::thread> submit_threads;
82  std::vector<std::unique_ptr<bool>> finish_flags;
83 
85 };
86 
87 }; // namespace sh
88 
89 #endif // HTTP_H_
Definition: http.h:38
void do_POST_async(const std::string &URL, const nlohmann::json &body, const nlohmann::json &headers={}, std::function< void(ReqCode, nlohmann::json)> cb_succ={}, std::function< void()> cb_err={})
Definition: http.cpp:233
std::vector< std::unique_ptr< bool > > finish_flags
Definition: http.h:82
std::pair< ReqCode, std::vector< uint8_t > > do_request_sync(const RequestType request_method, const std::string &URL, const nlohmann::json &body, const nlohmann::json &headers={})
Definition: http.cpp:253
std::pair< ReqCode, nlohmann::json > do_POST_sync_json(const std::string &URL, const nlohmann::json &body, const nlohmann::json &headers={})
Definition: http.cpp:276
void set_allow_insecure(bool val)
Definition: http.h:61
void prune_threads()
Definition: http.h:67
std::vector< std::thread > submit_threads
Definition: http.h:81
bool get_allow_insecure() const
Definition: http.h:62
~Http() noexcept
Definition: http.cpp:229
bool _allow_insecure
Definition: http.h:84
void do_GET_async(const std::string &URL, const nlohmann::json &query, const nlohmann::json &headers={}, std::function< void(ReqCode, nlohmann::json)> cb_succ={}, std::function< void()> cb_err={})
Definition: http.cpp:243
std::tuple< ReqCode, std::vector< float >, std::vector< int > > do_GET_sync_distances(const std::string &URL, const nlohmann::json &body, const nlohmann::json &headers)
Definition: http.cpp:305
void common_finish()
Definition: http.h:65
std::pair< ReqCode, nlohmann::json > do_GET_sync_json(const std::string &URL, const nlohmann::json &body, const nlohmann::json &headers={})
Definition: http.cpp:285
std::pair< ReqCode, std::vector< float > > do_GET_sync_floats(const std::string &URL, const nlohmann::json &body, const nlohmann::json &headers)
Definition: http.cpp:294
Definition: common-types.h:33
RequestType
Definition: http.h:36
std::size_t ReqCode
Definition: http.h:35