userver: userver/clients/http/response.hpp Source File
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
15USERVER_NAMESPACE_BEGIN
16
17namespace clients::http {
18
19using Status = USERVER_NAMESPACE::http::StatusCode;
20
21/// Headers container type
22using Headers = USERVER_NAMESPACE::http::headers::HeaderMap;
23
24/// Class that will be returned for successful request
25class Response final {
26public:
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::OK; }
51 bool IsError() const { return static_cast<uint16_t>(status_code()) >= 400; }
52
53 static void RaiseForStatus(int code, const LocalStats& stats);
54
55 void raise_for_status() const;
56
57 /// returns statistics on request execution like count of opened sockets,
58 /// connect time...
59 LocalStats GetStats() const;
60
61 void SetStats(const LocalStats& stats) { stats_ = stats; }
62 void SetStatusCode(Status status_code) { status_code_ = status_code; }
63
64private:
65 Headers headers_;
66 CookiesMap cookies_;
67 std::string response_;
68 Status status_code_{Status::Invalid};
69 LocalStats stats_;
70};
71
72} // namespace clients::http
73
74USERVER_NAMESPACE_END