userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
response.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/clients/http/response.hpp
4
/// @brief @copybrief clients::http::Response
5
6
#
include
<
string
>
7
8
#
include
<
userver
/
clients
/
http
/
error
.
hpp
>
9
#
include
<
userver
/
clients
/
http
/
local_stats
.
hpp
>
10
#
include
<
userver
/
http
/
header_map
.
hpp
>
11
#
include
<
userver
/
http
/
status_code
.
hpp
>
12
#
include
<
userver
/
server
/
http
/
http_response_cookie
.
hpp
>
13
#
include
<
userver
/
utils
/
str_icase
.
hpp
>
14
15
USERVER_NAMESPACE_BEGIN
16
17
namespace
clients::http {
18
19
using
Status = USERVER_NAMESPACE::http::
StatusCode
;
20
21
/// Headers container type
22
using
Headers
= USERVER_NAMESPACE::http::
headers
::HeaderMap;
23
24
/// Class that will be returned for successful request
25
class
Response
final
{
26
public
:
27
using
CookiesMap = server::
http
::Cookie::CookiesMap;
28
29
Response() =
default
;
30
31
/// response string
32
std::string&
sink_string
() {
return
response_; }
33
34
/// body as string
35
std::string
body
()
const
& {
return
response_; }
36
std::string&& body() && {
return
std::move(response_); }
37
38
/// body as string_view
39
std::string_view
body_view
()
const
{
return
response_; }
40
41
/// return reference to headers
42
const
Headers
&
headers
()
const
{
return
headers_; }
43
Headers
& headers() {
return
headers_; }
44
const
CookiesMap& cookies()
const
{
return
cookies_; }
45
CookiesMap& cookies() {
return
cookies_; }
46
47
/// status_code
48
Status
status_code
()
const
;
49
/// check status code
50
bool
IsOk
()
const
{
return
status_code
(
)
== Status::kOk; }
51
bool
IsError()
const
{
return
static_cast
<uint16_t>(
status_code
(
)
) >= 400; }
52
53
static
void
RaiseForStatus(
int
code,
const
LocalStats& stats);
54
static
void
RaiseForStatus(
int
code,
const
LocalStats& stats, std::string_view message);
55
56
/// @brief Configuration whether to include the response body in the exception in @ref raise_for_status.
57
enum
class
RaiseIncludeBody
:
std
::
uint8_t
{ kNo = 0, kYes = 1 };
58
59
/// @brief Raise an exception depending on the response status.
60
/// The body of the response may be included in the exception depending on the \c include_body.
61
///
62
/// @throws HttpClientException for statuses [400; 500)
63
/// @throws HttpServerException for statuses [500; 600)
64
void
raise_for_status
(
RaiseIncludeBody
include_body =
RaiseIncludeBody
::kNo)
const
;
65
66
/// returns statistics on request execution like count of opened sockets, connect time...
67
LocalStats
GetStats
()
const
;
68
69
void
SetStats(
const
LocalStats& stats) { stats_ = stats; }
70
void
SetStatusCode(Status status_code) { status_code_ = status_code; }
71
72
private
:
73
Headers
headers_;
74
CookiesMap cookies_;
75
std::string response_;
76
Status status_code_{Status::kInvalid};
77
LocalStats stats_;
78
};
79
80
}
// namespace clients::http
81
82
USERVER_NAMESPACE_END
userver
clients
http
response.hpp
Generated on
for userver by
Doxygen
1.17.0