oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
net_http.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#include <ove/net_http.h>
17#include <ove/types.hpp>
18
19#ifdef CONFIG_OVE_NET_HTTP
20
21namespace ove {
22
31namespace http {
32
43class Response {
44public:
48 Response() = default;
49
53 ~Response() { ove_http_response_free(&raw_); }
54
55 Response(const Response &) = delete;
56 Response &operator=(const Response &) = delete;
57
62 Response(Response &&other) noexcept : raw_(other.raw_) {
63 other.raw_ = {};
64 }
65
71 Response &operator=(Response &&other) noexcept {
72 if (this != &other) {
73 ove_http_response_free(&raw_);
74 raw_ = other.raw_;
75 other.raw_ = {};
76 }
77 return *this;
78 }
79
81 int status() const { return raw_.status; }
82
84 const char *body() const { return raw_.body; }
85
87 size_t body_len() const { return raw_.body_len; }
88
90 const char *headers() const { return raw_.headers; }
91
93 size_t headers_len() const { return raw_.headers_len; }
94
99 ove_http_response_t *raw() { return &raw_; }
100
101 ove_http_response_t raw_{};
102};
103
115class Client {
116public:
123 Client() {
124#ifdef CONFIG_OVE_ZERO_HEAP
125 int err = ove_http_client_init(&handle_, &storage_);
126#else
127 int err = ove_http_client_create(&handle_);
128#endif
129 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
130 }
131
137 ~Client() {
138 if (!handle_) return;
139#ifdef CONFIG_OVE_ZERO_HEAP
140 ove_http_client_deinit(handle_);
141#else
142 ove_http_client_destroy(handle_);
143#endif
144 }
145
146 Client(const Client &) = delete;
147 Client &operator=(const Client &) = delete;
148
149#ifdef CONFIG_OVE_ZERO_HEAP
150 Client(Client &&) = delete;
151 Client &operator=(Client &&) = delete;
152#else
157 Client(Client &&other) noexcept : handle_(other.handle_) {
158 other.handle_ = nullptr;
159 }
160
166 Client &operator=(Client &&other) noexcept {
167 if (this != &other) {
168 if (handle_) ove_http_client_destroy(handle_);
169 handle_ = other.handle_;
170 other.handle_ = nullptr;
171 }
172 return *this;
173 }
174#endif
175
182 [[nodiscard]] int get(const char *url, Response &resp) {
183 return ove_http_get(handle_, url, &resp.raw_);
184 }
185
195 [[nodiscard]] int post(const char *url, const char *content_type,
196 const void *body, size_t body_len,
197 Response &resp) {
198 return ove_http_post(handle_, url, content_type,
199 body, body_len, &resp.raw_);
200 }
201
212 [[nodiscard]] int request(ove_http_method_t method, const char *url,
213 const char *content_type,
214 const void *body, size_t body_len,
215 Response &resp) {
216 return ove_http_request(handle_, method, url, content_type,
217 body, body_len, &resp.raw_);
218 }
219
232 [[nodiscard]] int request(ove_http_method_t method, const char *url,
233 const char *content_type,
234 const void *body, size_t body_len,
235 const ove_http_header_t *headers,
236 size_t header_count,
237 Response &resp) {
238 return ove_http_request_ex(handle_, method, url, content_type,
239 body, body_len, headers,
240 header_count, &resp.raw_);
241 }
242
247 bool valid() const { return handle_ != nullptr; }
248
253 ove_http_client_t handle() const { return handle_; }
254
255private:
256 ove_http_client_t handle_ = nullptr;
257#ifdef CONFIG_OVE_ZERO_HEAP
258 ove_http_client_storage_t storage_ = {};
259#endif
260};
261
262} /* namespace http */
263
264} // namespace ove
265
266#endif /* CONFIG_OVE_NET_HTTP */
int get(unsigned int port, unsigned int pin)
Reads the current logic level of a GPIO pin.
Definition gpio.hpp:62
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.