blob: 607d168e9e8782652586f3b9c73f5938e3cc4505 [file] [log] [blame]
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001#pragma once
2
zhanghch058d1b46d2021-04-01 11:18:24 +08003#include "http_response.hpp"
4
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +02005#include <functional>
6
Ed Tanouse3cb5a32018-08-08 14:16:49 -07007namespace bmcweb
8{
9
10/**
11 * AsyncResp
12 * Gathers data needed for response processing after async calls are done
13 */
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020014
Ed Tanouse3cb5a32018-08-08 14:16:49 -070015class AsyncResp
16{
17 public:
Gunnar Mills9062d472021-11-16 11:37:47 -060018 AsyncResp(crow::Response& response) : res(response)
19 {}
20
21 AsyncResp(crow::Response& response, std::function<void()>&& function) :
22 res(response), func(std::move(function))
23 {}
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020024
Krzysztof Grobelnydab06042021-04-02 13:28:14 +000025 AsyncResp(const AsyncResp&) = delete;
26 AsyncResp(AsyncResp&&) = delete;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080027 AsyncResp& operator=(const AsyncResp&) = delete;
28 AsyncResp& operator=(AsyncResp&&) = delete;
Krzysztof Grobelnydab06042021-04-02 13:28:14 +000029
Ed Tanouse3cb5a32018-08-08 14:16:49 -070030 ~AsyncResp()
31 {
Gunnar Mills9062d472021-11-16 11:37:47 -060032 if (func && res.result() == boost::beast::http::status::ok)
33 {
34 func();
35 }
36
Ed Tanouse3cb5a32018-08-08 14:16:49 -070037 res.end();
38 }
39
Gunnar Mills9062d472021-11-16 11:37:47 -060040 crow::Response& res;
41 std::function<void()> func;
Ed Tanouse3cb5a32018-08-08 14:16:49 -070042};
43
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020044} // namespace bmcweb