blob: 8e9584ccdf882873fbd1497f9a8d57897d0ce52f [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;
27
Ed Tanouse3cb5a32018-08-08 14:16:49 -070028 ~AsyncResp()
29 {
Gunnar Mills9062d472021-11-16 11:37:47 -060030 if (func && res.result() == boost::beast::http::status::ok)
31 {
32 func();
33 }
34
Ed Tanouse3cb5a32018-08-08 14:16:49 -070035 res.end();
36 }
37
Gunnar Mills9062d472021-11-16 11:37:47 -060038 crow::Response& res;
39 std::function<void()> func;
Ed Tanouse3cb5a32018-08-08 14:16:49 -070040};
41
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020042} // namespace bmcweb