55#define TRANSFORM_RESULT (result ) std::make_unique<ActualResult>((result))
66
77
8- namespace oopetris ::http::implementation {
8+ oopetris::http::implementation::ActualResult::ActualResult (httplib::Result&& result) : m_result{ std::move (result) } { }
99
10+ oopetris::http::implementation::ActualResult::~ActualResult () = default ;
1011
11- ActualResult::ActualResult (httplib::Result&& result) : m_result{ std::move (result) } { }
12-
13- ActualResult::~ActualResult () = default ;
14-
15- [[nodiscard]] std::optional<std::string> ActualResult::get_header (const std::string& key) const {
16- if (m_result->has_header (key)) {
17- return std::nullopt ;
18- }
19-
20- return m_result->get_header_value (key);
12+ [[nodiscard]] std::optional<std::string> oopetris::http::implementation::ActualResult::get_header (const std::string& key
13+ ) const {
14+ if (m_result->has_header (key)) {
15+ return std::nullopt ;
2116 }
2217
23- [[nodiscard]] std::string ActualResult::body () const {
24- return m_result->body ;
25- }
18+ return m_result->get_header_value (key);
19+ }
2620
27- [[nodiscard]] int ActualResult::status () const {
28- return m_result->status ;
29- }
21+ [[nodiscard]] std::string oopetris::http::implementation:: ActualResult::body () const {
22+ return m_result->body ;
23+ }
3024
31- [[nodiscard]] std::optional<std::string> ActualResult::get_error () const {
25+ [[nodiscard]] int oopetris::http::implementation::ActualResult::status () const {
26+ return m_result->status ;
27+ }
3228
33- if (not m_result) {
34- return httplib::to_string (m_result.error ());
35- }
29+ [[nodiscard]] std::optional<std::string> oopetris::http::implementation::ActualResult::get_error () const {
3630
37- return std::nullopt ;
31+ if (not m_result) {
32+ return httplib::to_string (m_result.error ());
3833 }
3934
35+ return std::nullopt ;
36+ }
37+
4038
41- ActualClient::ActualClient (ActualClient&& other) noexcept : m_client{ std::move (other.m_client ) } { }
39+ oopetris::http::implementation::ActualClient::ActualClient (ActualClient&& other) noexcept
40+ : m_client{ std::move (other.m_client ) } { }
4241
43- ActualClient::~ActualClient () = default ;
42+ oopetris::http::implementation:: ActualClient::~ActualClient () = default ;
4443
45- ActualClient::ActualClient (const std::string& api_url) : m_client{ api_url } {
46- // clang-format off
44+ oopetris::http::implementation:: ActualClient::ActualClient (const std::string& api_url) : m_client{ api_url } {
45+ // clang-format off
4746 m_client.set_default_headers ({
4847#if defined(CPPHTTPLIB_ZLIB_SUPPORT) || defined(CPPHTTPLIB_BROTLI_SUPPORT)
4948 { " Accept-Encoding" ,
@@ -59,49 +58,52 @@ namespace oopetris::http::implementation {
5958#endif
6059 },
6160#endif
62- // clang-format on
63- { " Accept" , ::http::constants::json_content_type } });
61+ // clang-format on
62+ { " Accept" , ::http::constants::json_content_type } });
6463
6564#if defined(CPPHTTPLIB_ZLIB_SUPPORT) || defined(CPPHTTPLIB_BROTLI_SUPPORT)
66- m_client.set_compress (true );
67- m_client.set_decompress (true );
65+ m_client.set_compress (true );
66+ m_client.set_decompress (true );
6867#endif
69- }
70-
71- [[nodiscard]] std::unique_ptr<Result> ActualClient::Get (const std::string& url) {
72- return TRANSFORM_RESULT (m_client.Get (url));
73- }
74-
75- [[nodiscard]] std::unique_ptr<Result> ActualClient::Delete (const std::string& url) {
76- return TRANSFORM_RESULT (m_client.Delete (url));
77- }
68+ }
7869
79- [[nodiscard]] std::unique_ptr<Result>
80- ActualClient::Post (const std::string& url, const std::optional<std::pair<std::string, std::string>>& payload) {
70+ [[nodiscard]] std::unique_ptr<oopetris::http::Result> oopetris::http::implementation::ActualClient::Get (const std::string& url) {
71+ return TRANSFORM_RESULT (m_client.Get (url));
72+ }
8173
82- if ( not payload. has_value () ) {
83- return TRANSFORM_RESULT (m_client.Post (url));
84- }
74+ [[nodiscard]] std::unique_ptr<oopetris::http::Result> oopetris::http::implementation::ActualClient::Delete ( const std::string& url ) {
75+ return TRANSFORM_RESULT (m_client.Delete (url));
76+ }
8577
86- auto [content, content_type] = payload.value ();
78+ [[nodiscard]] std::unique_ptr<oopetris::http::Result> oopetris::http::implementation::ActualClient::Post (
79+ const std::string& url,
80+ const std::optional<std::pair<std::string, std::string>>& payload
81+ ) {
8782
88- return TRANSFORM_RESULT (m_client.Post (url, content, content_type));
83+ if (not payload.has_value ()) {
84+ return TRANSFORM_RESULT (m_client.Post (url));
8985 }
9086
91- [[nodiscard]] std::unique_ptr<Result>
92- ActualClient::Put (const std::string& url, const std::optional<std::pair<std::string, std::string>>& payload) {
87+ auto [content, content_type] = payload.value ();
9388
94- if (not payload.has_value ()) {
95- return TRANSFORM_RESULT (m_client.Put (url));
96- }
89+ return TRANSFORM_RESULT (m_client.Post (url, content, content_type));
90+ }
9791
98- auto [content, content_type] = payload.value ();
92+ [[nodiscard]] std::unique_ptr<oopetris::http::Result> oopetris::http::implementation::ActualClient::Put (
93+ const std::string& url,
94+ const std::optional<std::pair<std::string, std::string>>& payload
95+ ) {
9996
100- return TRANSFORM_RESULT (m_client.Put (url, content, content_type));
97+ if (not payload.has_value ()) {
98+ return TRANSFORM_RESULT (m_client.Put (url));
10199 }
102100
103- void ActualClient::SetBearerAuth ( const std::string& token) {
101+ auto [content, content_type] = payload. value ();
104102
105- m_client.set_bearer_token_auth (token);
106- }
107- } // namespace oopetris::http::implementation
103+ return TRANSFORM_RESULT (m_client.Put (url, content, content_type));
104+ }
105+
106+ void oopetris::http::implementation::ActualClient::SetBearerAuth (const std::string& token) {
107+
108+ m_client.set_bearer_token_auth (token);
109+ }
0 commit comments