Skip to content

Commit 8d61375

Browse files
committed
feat clients: add http-method convert from string
add clients::http::HttpMethod convert commit_hash:7e42f12587c8a788d143a1141e1f84f81026c95c
1 parent c0f5c83 commit 8d61375

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

.mapping.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@
10971097
"core/src/clients/http/error.cpp":"taxi/uservices/userver/core/src/clients/http/error.cpp",
10981098
"core/src/clients/http/form.cpp":"taxi/uservices/userver/core/src/clients/http/form.cpp",
10991099
"core/src/clients/http/form_test.cpp":"taxi/uservices/userver/core/src/clients/http/form_test.cpp",
1100+
"core/src/clients/http/http_method_test.cpp":"taxi/uservices/userver/core/src/clients/http/http_method_test.cpp",
11001101
"core/src/clients/http/plugin.cpp":"taxi/uservices/userver/core/src/clients/http/plugin.cpp",
11011102
"core/src/clients/http/plugins/headers_propagator/component.cpp":"taxi/uservices/userver/core/src/clients/http/plugins/headers_propagator/component.cpp",
11021103
"core/src/clients/http/plugins/headers_propagator/plugin.cpp":"taxi/uservices/userver/core/src/clients/http/plugins/headers_propagator/plugin.cpp",

core/include/userver/clients/http/request.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ namespace impl {
4040
class EasyWrapper;
4141
} // namespace impl
4242

43-
/// HTTP request method
43+
/// @brief HTTP request method
4444
enum class HttpMethod { kGet, kPost, kHead, kPut, kDelete, kPatch, kOptions };
4545

46+
/// @brief Convert HTTP method enum value to string
4647
std::string_view ToStringView(HttpMethod method);
4748

49+
/// @brief Convert HTTP method string to enum value
50+
HttpMethod HttpMethodFromString(std::string_view method_str);
51+
4852
using USERVER_NAMESPACE::http::HttpVersion;
4953

5054
enum class HttpAuthType {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <userver/utest/using_namespace_userver.hpp>
2+
3+
#include <userver/clients/http/request.hpp>
4+
#include <userver/utest/utest.hpp>
5+
6+
USERVER_NAMESPACE_BEGIN
7+
8+
namespace {
9+
namespace ch = clients::http;
10+
}
11+
12+
UTEST(ClientHttpMethodTest, Convert) {
13+
EXPECT_EQ(ch::ToStringView(ch::HttpMethod::kGet), "GET");
14+
EXPECT_EQ(ch::ToStringView(ch::HttpMethod::kHead), "HEAD");
15+
EXPECT_EQ(ch::ToStringView(ch::HttpMethod::kPost), "POST");
16+
EXPECT_EQ(ch::ToStringView(ch::HttpMethod::kPatch), "PATCH");
17+
EXPECT_EQ(ch::ToStringView(ch::HttpMethod::kPut), "PUT");
18+
EXPECT_EQ(ch::ToStringView(ch::HttpMethod::kDelete), "DELETE");
19+
EXPECT_EQ(ch::ToStringView(ch::HttpMethod::kOptions), "OPTIONS");
20+
21+
EXPECT_EQ(ch::HttpMethodFromString("GET"), ch::HttpMethod::kGet);
22+
EXPECT_EQ(ch::HttpMethodFromString("HEAD"), ch::HttpMethod::kHead);
23+
EXPECT_EQ(ch::HttpMethodFromString("POST"), ch::HttpMethod::kPost);
24+
EXPECT_EQ(ch::HttpMethodFromString("PATCH"), ch::HttpMethod::kPatch);
25+
EXPECT_EQ(ch::HttpMethodFromString("PUT"), ch::HttpMethod::kPut);
26+
EXPECT_EQ(ch::HttpMethodFromString("DELETE"), ch::HttpMethod::kDelete);
27+
EXPECT_EQ(ch::HttpMethodFromString("OPTIONS"), ch::HttpMethod::kOptions);
28+
29+
UEXPECT_THROW(ch::HttpMethodFromString("123"), std::runtime_error);
30+
}
31+
32+
USERVER_NAMESPACE_END

core/src/clients/http/request.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <userver/clients/http/request.hpp>
22

3-
#include <chrono>
43
#include <cstdlib>
5-
#include <map>
64
#include <string>
75
#include <string_view>
86
#include <system_error>
@@ -38,6 +36,17 @@ namespace {
3836

3937
constexpr std::string_view kHeaderExpect = "Expect";
4038

39+
static constexpr utils::TrivialBiMap kHttpMethodMap([](auto selector) {
40+
return selector()
41+
.Case(HttpMethod::kGet, "GET")
42+
.Case(HttpMethod::kHead, "HEAD")
43+
.Case(HttpMethod::kPost, "POST")
44+
.Case(HttpMethod::kPut, "PUT")
45+
.Case(HttpMethod::kPatch, "PATCH")
46+
.Case(HttpMethod::kDelete, "DELETE")
47+
.Case(HttpMethod::kOptions, "OPTIONS");
48+
});
49+
4150
std::string ToString(HttpMethod method) { return std::string{ToStringView(method)}; }
4251

4352
curl::easy::http_version_t ToNative(HttpVersion version) {
@@ -171,19 +180,16 @@ bool IsAllowedSchemaInUrl(std::string_view url) {
171180

172181
} // namespace
173182

174-
std::string_view ToStringView(HttpMethod method) {
175-
static constexpr utils::TrivialBiMap kMap([](auto selector) {
176-
return selector()
177-
.Case(HttpMethod::kDelete, "DELETE")
178-
.Case(HttpMethod::kGet, "GET")
179-
.Case(HttpMethod::kHead, "HEAD")
180-
.Case(HttpMethod::kPost, "POST")
181-
.Case(HttpMethod::kPut, "PUT")
182-
.Case(HttpMethod::kPatch, "PATCH")
183-
.Case(HttpMethod::kOptions, "OPTIONS");
184-
});
185-
186-
return utils::impl::EnumToStringView(method, kMap);
183+
std::string_view ToStringView(HttpMethod method) { return utils::impl::EnumToStringView(method, kHttpMethodMap); }
184+
185+
HttpMethod HttpMethodFromString(std::string_view method_str) {
186+
if (auto method = kHttpMethodMap.TryFindBySecond(method_str)) {
187+
return *method;
188+
}
189+
190+
throw std::runtime_error(
191+
fmt::format("Unsupported http method '{}' (must be one of {})", method_str, kHttpMethodMap.DescribeSecond())
192+
);
187193
}
188194

189195
ProxyAuthType ProxyAuthTypeFromString(const std::string& auth_name) {

0 commit comments

Comments
 (0)