1.10
http.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_NETWORK_HTTP_H
7#define EGT_NETWORK_HTTP_H
8
14#include <egt/app.h>
15#include <egt/detail/meta.h>
16#include <memory>
17#include <string>
18#include <vector>
19
20namespace egt
21{
22inline namespace v1
23{
24
25namespace detail
26{
27struct HttpClientRequestData;
28class HttpClientRequestManager;
29}
30
31namespace experimental
32{
33
47class EGT_API HttpClientRequest
48{
49public:
50
52 using ReadCallback = std::function < void(const unsigned char* data, size_t len, bool done) >;
53
57 explicit HttpClientRequest() noexcept;
58
60 HttpClientRequest& operator=(const HttpClientRequest&) = delete;
62 HttpClientRequest& operator=(HttpClientRequest&&) noexcept;
63
67 virtual void start_async(const std::string& url, ReadCallback callback);
68
70 inline detail::HttpClientRequestData* impl()
71 {
72 return m_impl.get();
73 }
74
75 virtual ~HttpClientRequest() noexcept;
76
77protected:
78
80 void finish();
81
83 void cleanup();
84
86 std::unique_ptr<detail::HttpClientRequestData> m_impl;
87
88 friend class detail::HttpClientRequestManager;
89};
90
97template<class T>
98T load_file_from_network(const std::string& url)
99{
100 T buffer;
101 bool finished = false;
102 HttpClientRequest request;
103 request.start_async(url, [&buffer, url, &finished](const unsigned char* data, size_t len, bool done)
104 {
105 if (data && len)
106 buffer.insert(buffer.end(), data, data + len);
107
108 if (done)
109 finished = true;
110 });
111
112 // TODO: super nasty way to block here
113
114 while (!finished)
115 Application::instance().event().step();
116
117 return buffer;
118}
119
120}
121}
122}
123
124#endif
An HTTP client request.
Definition http.h:48
std::function< void(const unsigned char *data, size_t len, bool done) > ReadCallback
Type used for callback on read.
Definition http.h:52
HttpClientRequest() noexcept
Create a request for the specified URL.
virtual void start_async(const std::string &url, ReadCallback callback)
Start the download.
EGT framework namespace.
Definition animation.h:24