C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
rest_api.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "../json_compat.hpp"
26#include "../meta/literal_string.hpp"
27#include "common_types.hpp"
28
29#include <concepts>
30#include <functional>
31#include <string_view>
32#include <type_traits>
33
34namespace essence::net {
35 enum class http_method {
36 put,
37 post,
38 get,
39 };
40
41 template <typename T>
43
44 template <typename T>
45 concept rest_message = requires {
46 { rest_message_traits<T>::method } -> std::convertible_to<http_method>;
47 { rest_message_traits<T>::relative_uri } -> std::convertible_to<std::string_view>;
48 requires std::default_initializable<T>;
49 requires json_serializable<T>;
50 requires std::derived_from<T, dummy_body_tag>;
51 typename rest_message_traits<T>::response_type;
52 };
53
54 template <typename T>
55 concept has_rest_message_transformer_only_name = requires { rest_message_traits<T>::message_transformer; };
56
57 template <typename T>
59 std::is_invocable_r_v<json, decltype(rest_message_traits<T>::message_transformer), const T&>;
60
61 template <typename T>
62 concept has_rest_response_transformer_only_name = requires { rest_message_traits<T>::response_transformer; };
63
64 template <typename T>
65 concept has_rest_response_transformer = requires(const typename rest_message_traits<T>::response_type& response) {
66 { std::invoke(rest_message_traits<T>::response_transformer, response) } -> json_serializable;
67 };
68
69 template <has_rest_message_transformer T>
71 static constexpr auto&& value = rest_message_traits<T>::message_transformer;
72 };
73
74 template <has_rest_message_transformer T>
75 inline constexpr auto&& rest_message_transformer_v = rest_message_transformer<T>::value;
76
77 template <has_rest_response_transformer T>
79 static constexpr auto&& value = rest_message_traits<T>::response_transformer;
80 };
81
82 template <has_rest_response_transformer T>
83 inline constexpr auto&& rest_response_transformer_v = rest_response_transformer<T>::value;
84
85 template <http_method Method, meta::literal_string RelativeUri, typename ResponseType>
87 using response_type = ResponseType;
88
89 static constexpr http_method method{Method};
90 static constexpr std::string_view relative_uri{RelativeUri};
91 };
92
93 constexpr std::string_view to_string(http_method method) noexcept {
94 switch (method) {
95 case http_method::put:
96 return U8("PUT");
97 case http_method::post:
98 return U8("POST");
99 case http_method::get:
100 return U8("GET");
101 }
102
103 return U8("POST");
104 }
105} // namespace essence::net
namespace for Niels Lohmann
Definition json.hpp:19415
Definition json_compat.hpp:43
Definition rest_api.hpp:45
Definition rest_api.hpp:86
Definition rest_api.hpp:42
Definition rest_api.hpp:70
Definition rest_api.hpp:78