20 #ifndef _ENABLE_QT_EXPERIMENTAL_
21 #error You should define _ENABLE_QT_EXPERIMENTAL_ in order to use this experimental header file.
24 #include <unity/util/DefinesPtrs.h>
25 #include <unity/util/NonCopyable.h>
26 #include <unity/UnityExceptions.h>
28 #include <unity/scopes/ScopeExceptions.h>
30 #include <core/net/http/client.h>
31 #include <core/net/http/request.h>
32 #include <core/net/http/response.h>
63 template <
typename PARSER>
64 using FactoryFunc = std::function<std::shared_ptr<PARSER>(
const std::string&, std::string&)>;
66 template <
typename TYPE,
typename PARSER>
68 std::function<bool(PARSER& root, const std::string&, std::deque<std::shared_ptr<TYPE>>&, std::string&)>;
70 template <
typename TYPE>
71 using ResultsList = std::deque<std::shared_ptr<TYPE>>;
73 template <
typename TYPE>
74 using ResultsFuture = std::future<ResultsList<TYPE>>;
76 template <
typename PARSER>
77 using ParserFuture = std::future<std::shared_ptr<PARSER>>;
108 template <
typename BASE,
typename TYPE,
typename PARSER>
109 ResultsFuture<BASE>
async_get(std::string
const& uri,
110 std::string
const& object_name,
111 FactoryFunc<PARSER>
const& create,
112 ParseFunc<BASE, PARSER>
const& parse)
const;
132 template <
typename TYPE,
typename PARSER>
133 ResultsFuture<TYPE>
async_get(std::string
const& uri,
134 std::string
const& object_name,
135 FactoryFunc<PARSER>
const& create,
136 ParseFunc<TYPE, PARSER>
const& parse)
const;
148 template <
typename PARSER>
149 ParserFuture<PARSER>
async_get_parser(std::string
const& uri, FactoryFunc<PARSER>
const& create)
const;
161 template <
typename T>
162 static T
get_or_throw(std::future<T>& f, int64_t seconds = 10);
168 std::string
get_uri(std::string
const& host,
169 std::vector<std::pair<std::string, std::string>>
const& parameters)
const;
173 core::net::http::Request::Progress::Next progress_report(core::net::http::Request::Progress
const& progress)
const;
175 void async_execute(core::net::http::Request::Handler
const& handler, std::string
const& uri)
const;
178 std::shared_ptr<Priv> p_;
182 template <
typename BASE,
typename TYPE,
typename PARSER>
184 std::string
const& object_name,
185 FactoryFunc<PARSER>
const& create,
186 ParseFunc<BASE, PARSER>
const& parse)
const
188 static_assert(std::is_base_of<BASE, TYPE>::value,
189 "Second template parameter type must be a valid base class of the first one.");
191 auto prom = std::make_shared<std::promise<std::deque<std::shared_ptr<BASE>>>>();
192 core::net::http::Request::Handler handler;
194 handler.on_progress(bind(&HttpAsyncReader::progress_report,
this, std::placeholders::_1));
196 handler.on_error([prom, uri](core::net::Error
const& e)
198 unity::LogicException logic_exception(
"AsyncReader::async_get: " + std::string(e.what()) +
199 "( uri = " + uri +
" )");
200 prom->set_exception(logic_exception.self());
204 [prom, object_name, parse, create, uri](core::net::http::Response
const& response)
206 if (response.status != core::net::http::Status::ok)
208 unity::LogicException e(
"AsyncReader::async_get: " + response.body +
"( uri = " + uri +
" )");
209 prom->set_exception(e.self());
213 std::string error_string;
214 std::shared_ptr<PARSER> reader = create(response.body, error_string);
217 unity::LogicException e(
"AsyncReader::async_get: error obtaining parser: " + error_string);
218 prom->set_exception(e.self());
222 std::deque<std::shared_ptr<BASE>> results;
223 std::string error_message;
224 if (!parse(*reader, object_name, results, error_message))
226 unity::LogicException e(
"AsyncReader::async_get: error parsing data: " + error_message);
227 prom->set_exception(e.self());
231 prom->set_value(results);
237 async_execute(handler, uri);
239 return prom->get_future();
242 template <
typename TYPE,
typename PARSER>
243 HttpAsyncReader::ResultsFuture<TYPE> HttpAsyncReader::async_get(std::string
const& uri,
244 std::string
const& object_name,
245 FactoryFunc<PARSER>
const& create,
246 ParseFunc<TYPE, PARSER>
const& parse)
const
248 return async_get<TYPE, TYPE, PARSER>(uri, object_name, create, parse);
251 template <
typename PARSER>
252 HttpAsyncReader::ParserFuture<PARSER> HttpAsyncReader::async_get_parser(std::string
const& uri,
253 FactoryFunc<PARSER>
const& create)
const
255 auto prom = std::make_shared<std::promise<std::shared_ptr<PARSER>>>();
256 core::net::http::Request::Handler handler;
257 handler.on_progress(bind(&HttpAsyncReader::progress_report,
this, std::placeholders::_1));
259 handler.on_error([prom, uri](core::net::Error
const& e)
261 unity::LogicException logic_exception(
"AsyncReader::async_get: " + std::string(e.what()) +
262 "( uri = " + uri +
" )");
263 prom->set_exception(logic_exception.self());
267 [
this, prom, create, uri](core::net::http::Response
const& response)
269 if (response.status != core::net::http::Status::ok)
271 unity::LogicException e(
"AsyncReader::async_get_parser: " + response.body +
"( uri = " + uri +
" )");
272 prom->set_exception(e.self());
276 std::string error_string;
277 std::shared_ptr<PARSER> reader = create(response.body, error_string);
280 unity::LogicException e(
"AsyncReader::async_get: error obtaining parser: " + error_string);
281 prom->set_exception(e.self());
285 prom->set_value(reader);
290 async_execute(handler, uri);
292 return prom->get_future();
295 template <
typename T>
296 T HttpAsyncReader::get_or_throw(std::future<T>& f, int64_t seconds)
298 if (f.wait_for(std::chrono::seconds(seconds)) != std::future_status::ready)
301 std::to_string(seconds) +
" seconds");
Definition: HttpAsyncReader.h:45
ResultsFuture< BASE > async_get(std::string const &uri, std::string const &object_name, FactoryFunc< PARSER > const &create, ParseFunc< BASE, PARSER > const &parse) const
Downloads a HTTP remote file asynchronously and returns a future to a list of results This method dow...
Definition: HttpAsyncReader.h:183
ParserFuture< PARSER > async_get_parser(std::string const &uri, FactoryFunc< PARSER > const &create) const
Downloads a HTTP remote file asynchronously and returns a future to a valid parser containing the dat...
Definition: HttpAsyncReader.h:252
Top-level namespace for all things Unity-related.
Definition: Version.h:49
static T get_or_throw(std::future< T > &f, int64_t seconds=10)
Gets the data of the given future in the gived timeout. If the time given expires and the data in the...
Definition: HttpAsyncReader.h:296
Class that downloads http files asynchronously.
Definition: HttpAsyncReader.h:55
std::string get_uri(std::string const &host, std::vector< std::pair< std::string, std::string >> const ¶meters) const
Constructs a URI with the given host and parameters. This is a convenience method that constructs a u...
Definition: HttpAsyncReader.cpp:87
Exception to indicate that a twoway request timed out.
Definition: ScopeExceptions.h:108