Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1 | #pragma once |
2 | |||||
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 3 | #include "http_response.hpp" |
4 | |||||
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 5 | #include <functional> |
6 | |||||
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 7 | namespace bmcweb |
8 | { | ||||
9 | |||||
10 | /** | ||||
11 | * AsyncResp | ||||
12 | * Gathers data needed for response processing after async calls are done | ||||
13 | */ | ||||
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 14 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 15 | class AsyncResp |
16 | { | ||||
17 | public: | ||||
18 | AsyncResp(crow::Response& response) : res(response) | ||||
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 19 | {} |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 20 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 21 | AsyncResp(crow::Response& response, std::function<void()>&& function) : |
22 | res(response), func(std::move(function)) | ||||
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 23 | {} |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 24 | |
Krzysztof Grobelny | dab0604 | 2021-04-02 13:28:14 +0000 | [diff] [blame] | 25 | AsyncResp(const AsyncResp&) = delete; |
26 | AsyncResp(AsyncResp&&) = delete; | ||||
27 | |||||
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 28 | ~AsyncResp() |
29 | { | ||||
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 30 | if (func && res.result() == boost::beast::http::status::ok) |
31 | { | ||||
32 | func(); | ||||
33 | } | ||||
34 | |||||
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 35 | res.end(); |
36 | } | ||||
37 | |||||
38 | crow::Response& res; | ||||
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 39 | std::function<void()> func; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 40 | }; |
41 | |||||
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 42 | } // namespace bmcweb |