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:
18 AsyncResp(crow::Response& response) : res(response)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050019 {}
Ed Tanouse3cb5a32018-08-08 14:16:49 -070020
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020021 AsyncResp(crow::Response& response, std::function<void()>&& function) :
22 res(response), func(std::move(function))
Gunnar Mills1214b7e2020-06-04 10:11:30 -050023 {}
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 {
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020030 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
38 crow::Response& res;
Ed Tanous23a21a12020-07-25 04:45:05 +000039 std::function<void()> func;
Ed Tanouse3cb5a32018-08-08 14:16:49 -070040};
41
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020042} // namespace bmcweb