blob: f596dcdd20279558ddefa4a9cbc3186c4e5ec340 [file] [log] [blame]
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001#pragma once
2
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +02003#include <functional>
4
Ed Tanouse3cb5a32018-08-08 14:16:49 -07005namespace bmcweb
6{
7
8/**
9 * AsyncResp
10 * Gathers data needed for response processing after async calls are done
11 */
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020012
Ed Tanouse3cb5a32018-08-08 14:16:49 -070013class AsyncResp
14{
15 public:
16 AsyncResp(crow::Response& response) : res(response)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050017 {}
Ed Tanouse3cb5a32018-08-08 14:16:49 -070018
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020019 AsyncResp(crow::Response& response, std::function<void()>&& function) :
20 res(response), func(std::move(function))
Gunnar Mills1214b7e2020-06-04 10:11:30 -050021 {}
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020022
Ed Tanouse3cb5a32018-08-08 14:16:49 -070023 ~AsyncResp()
24 {
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020025 if (func && res.result() == boost::beast::http::status::ok)
26 {
27 func();
28 }
29
Ed Tanouse3cb5a32018-08-08 14:16:49 -070030 res.end();
31 }
32
33 crow::Response& res;
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020034 std::function<void()> func = 0;
Ed Tanouse3cb5a32018-08-08 14:16:49 -070035};
36
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020037} // namespace bmcweb