Kurlyk
Loading...
Searching...
No Matches
type_utils.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_TYPES_TYPE_UTILS_HPP_INCLUDED
3#define _KURLYK_TYPES_TYPE_UTILS_HPP_INCLUDED
4
7
8namespace kurlyk {
9
11 inline const std::string& to_str(ProxyType value) noexcept {
12 static const std::vector<std::string> names = {
13 "PROXY_HTTP", "PROXY_HTTPS", "PROXY_HTTP_1_0",
14 "PROXY_SOCKS4", "PROXY_SOCKS4A", "PROXY_SOCKS5", "PROXY_SOCKS5_HOSTNAME"
15 };
16 return names[static_cast<size_t>(value)];
17 }
18
20 inline const std::string& to_str(RateLimitType value) noexcept {
21 static const std::vector<std::string> names = {
22 "RL_GENERAL", "RL_SPECIFIC"
23 };
24 return names[static_cast<size_t>(value)];
25 }
26
28 inline const std::string& to_str(WebSocketEventType value) noexcept {
29 static const std::vector<std::string> names = {
30 "WS_OPEN", "WS_MESSAGE", "WS_CLOSE", "WS_ERROR"
31 };
32 return names[static_cast<size_t>(value)];
33 }
34
36 template <typename T>
37 T to_enum(const std::string& str);
38
39 template <>
40 inline ProxyType to_enum<ProxyType>(const std::string& str) {
41 static const std::unordered_map<std::string, ProxyType> map = {
42 {"PROXY_HTTP", ProxyType::PROXY_HTTP},
43 {"PROXY_HTTPS", ProxyType::PROXY_HTTPS},
44 {"PROXY_HTTP_1_0", ProxyType::PROXY_HTTP_1_0},
45 {"PROXY_SOCKS4", ProxyType::PROXY_SOCKS4},
46 {"PROXY_SOCKS4A", ProxyType::PROXY_SOCKS4A},
47 {"PROXY_SOCKS5", ProxyType::PROXY_SOCKS5},
48 {"PROXY_SOCKS5_HOSTNAME", ProxyType::PROXY_SOCKS5_HOSTNAME}
49 };
50 auto it = map.find(utils::to_upper_case(str));
51 if (it != map.end()) return it->second;
52 throw std::invalid_argument("Invalid ProxyType: " + str);
53 }
54
55 template <>
56 inline RateLimitType to_enum<RateLimitType>(const std::string& str) {
57 static const std::unordered_map<std::string, RateLimitType> map = {
58 {"RL_GENERAL", RateLimitType::RL_GENERAL},
59 {"RL_SPECIFIC", RateLimitType::RL_SPECIFIC}
60 };
61 auto it = map.find(utils::to_upper_case(str));
62 if (it != map.end()) return it->second;
63 throw std::invalid_argument("Invalid RateLimitType: " + str);
64 }
65
66 template <>
67 inline WebSocketEventType to_enum<WebSocketEventType>(const std::string& str) {
68 static const std::unordered_map<std::string, WebSocketEventType> map = {
69 {"WS_OPEN", WebSocketEventType::WS_OPEN},
70 {"WS_MESSAGE", WebSocketEventType::WS_MESSAGE},
71 {"WS_CLOSE", WebSocketEventType::WS_CLOSE},
73 };
74 auto it = map.find(utils::to_upper_case(str));
75 if (it != map.end()) return it->second;
76 throw std::invalid_argument("Invalid WebSocketEventType: " + str);
77 }
78
79 inline std::ostream& operator<<(std::ostream& os, ProxyType type) {
80 return os << to_str(type);
81 }
82
83 inline std::ostream& operator<<(std::ostream& os, RateLimitType type) {
84 return os << to_str(type);
85 }
86
87 inline std::ostream& operator<<(std::ostream& os, WebSocketEventType type) {
88 return os << to_str(type);
89 }
90
91#ifdef KURLYK_USE_JSON
92
93 inline void to_json(nlohmann::json& j, const ProxyType& value) {
94 j = to_str(value);
95 }
96
97 inline void from_json(const nlohmann::json& j, ProxyType& value) {
98 value = to_enum<ProxyType>(j.get<std::string>());
99 }
100
101 inline void to_json(nlohmann::json& j, const RateLimitType& value) {
102 j = to_str(value);
103 }
104
105 inline void from_json(const nlohmann::json& j, RateLimitType& value) {
106 value = to_enum<RateLimitType>(j.get<std::string>());
107 }
108
109 inline void to_json(nlohmann::json& j, const WebSocketEventType& value) {
110 j = to_str(value);
111 }
112
113 inline void from_json(const nlohmann::json& j, WebSocketEventType& value) {
114 value = to_enum<WebSocketEventType>(j.get<std::string>());
115 }
116
117#endif // KURLYK_USE_JSON
118
119#if KURLYK_HTTP_SUPPORT
120
124 inline long to_curl_proxy_type(ProxyType type) {
125 static const long values[] = {
126 CURLPROXY_HTTP,
127 CURLPROXY_HTTPS,
128 CURLPROXY_HTTP_1_0,
129 CURLPROXY_SOCKS4,
130 CURLPROXY_SOCKS4A,
131 CURLPROXY_SOCKS5,
132 CURLPROXY_SOCKS5_HOSTNAME
133 };
134 return values[static_cast<size_t>(type)];
135 }
136
137#endif
138
139} // namespace kurlyk
140
141#endif // _KURLYK_TYPES_TYPE_UTILS_HPP_INCLUDED
void to_json(nlohmann::json &j, const ProxyConfig &config)
Serializes ProxyConfig to JSON.
void from_json(const nlohmann::json &j, ProxyConfig &config)
Deserializes ProxyConfig from JSON.
std::string to_upper_case(std::string str)
Converts a string to uppercase.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
WebSocketEventType to_enum< WebSocketEventType >(const std::string &str)
RateLimitType to_enum< RateLimitType >(const std::string &str)
T to_enum(const std::string &str)
Template specialization to convert string to enum value.
RateLimitType
Defines rate limit scope categories.
Definition enums.hpp:24
@ RL_GENERAL
Applies globally to all requests.
Definition enums.hpp:25
@ RL_SPECIFIC
Applies to specific client/request.
Definition enums.hpp:26
ProxyType
Enumeration of supported proxy types compatible with libcurl.
Definition enums.hpp:12
@ PROXY_SOCKS4A
SOCKS4A proxy.
Definition enums.hpp:17
@ PROXY_SOCKS5_HOSTNAME
SOCKS5 proxy with hostname resolution.
Definition enums.hpp:19
@ PROXY_HTTP
HTTP proxy.
Definition enums.hpp:13
@ PROXY_HTTPS
HTTPS proxy.
Definition enums.hpp:14
@ PROXY_HTTP_1_0
HTTP/1.0 proxy.
Definition enums.hpp:15
@ PROXY_SOCKS4
SOCKS4 proxy.
Definition enums.hpp:16
@ PROXY_SOCKS5
SOCKS5 proxy.
Definition enums.hpp:18
ProxyType to_enum< ProxyType >(const std::string &str)
WebSocketEventType
Types of WebSocket events.
Definition enums.hpp:31
@ WS_ERROR
Error occurred.
Definition enums.hpp:35
@ WS_MESSAGE
Message received.
Definition enums.hpp:33
@ WS_CLOSE
Connection closed.
Definition enums.hpp:34
@ WS_OPEN
Connection established.
Definition enums.hpp:32
const std::string & to_str(ProxyType value) noexcept
Converts a ProxyType enum value to its string representation.
std::ostream & operator<<(std::ostream &os, ProxyType type)