OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
http_client.hpp
1#pragma once
2#include <ossia/detail/config.hpp>
3
4#include <ossia/network/http/http_client_request.hpp>
5
6#include <memory>
7#include <string>
8#include <utility>
9
10namespace ossia::net
11{
17template <typename Fun, typename Err>
19{
20 struct answer
21 {
22 static constexpr int reserve_expect = Fun::reserve_expect;
23 Fun fun;
24 Err err;
25
26 void operator()(auto& req, int status, const auto& body)
27 {
28 // The legacy callback carries no status, so only a successful response
29 // can be handed to it as a body.
30 if(status >= 200 && status < 300)
31 {
32 fun(req, body);
33 }
34 else
35 {
36 ossia::logger().error("HTTP Error: status code {}", status);
37 err(req);
38 }
39 }
40 };
41
42 struct error
43 {
44 Err err;
45 void operator()(auto& req, std::string_view) { err(req); }
46 };
47
48 using implementation = http_client_request<answer, error>;
49
50public:
52 Fun f, Err err, boost::asio::io_context& ctx, const std::string& server,
53 const std::string& path, std::string_view verb = "GET")
54 : m_impl{std::make_shared<implementation>(
55 answer{std::move(f), err}, error{err}, ctx, verb, server, path)}
56 {
57 }
58
61 void resolve(const std::string& server, const std::string& port)
62 {
63 m_impl->resolve(server, port);
64 }
65
66 void close() { m_impl->close(); }
67
68private:
69 std::shared_ptr<implementation> m_impl;
70};
71}
Definition http_client.hpp:19
void resolve(const std::string &server, const std::string &port)
Definition http_client.hpp:61
spdlog::logger & logger() noexcept
Where the errors will be logged. Default is stderr.
Definition context.cpp:120