xref: /openbmc/bmcweb/include/async_resp.hpp (revision 8d1b46d7f8d39db2ba048f9e9007106ca3a28c9b)
1e3cb5a31SEd Tanous #pragma once
2e3cb5a31SEd Tanous 
3*8d1b46d7Szhanghch05 #include "http_response.hpp"
4*8d1b46d7Szhanghch05 
5c0a1c8a0SIwona Klimaszewska #include <functional>
6c0a1c8a0SIwona Klimaszewska 
7e3cb5a31SEd Tanous namespace bmcweb
8e3cb5a31SEd Tanous {
9e3cb5a31SEd Tanous 
10e3cb5a31SEd Tanous /**
11e3cb5a31SEd Tanous  * AsyncResp
12e3cb5a31SEd Tanous  * Gathers data needed for response processing after async calls are done
13e3cb5a31SEd Tanous  */
14c0a1c8a0SIwona Klimaszewska 
15e3cb5a31SEd Tanous class AsyncResp
16e3cb5a31SEd Tanous {
17e3cb5a31SEd Tanous   public:
18e3cb5a31SEd Tanous     AsyncResp(crow::Response& response) : res(response)
191214b7e7SGunnar Mills     {}
20e3cb5a31SEd Tanous 
21c0a1c8a0SIwona Klimaszewska     AsyncResp(crow::Response& response, std::function<void()>&& function) :
22c0a1c8a0SIwona Klimaszewska         res(response), func(std::move(function))
231214b7e7SGunnar Mills     {}
24c0a1c8a0SIwona Klimaszewska 
25dab0604aSKrzysztof Grobelny     AsyncResp(const AsyncResp&) = delete;
26dab0604aSKrzysztof Grobelny     AsyncResp(AsyncResp&&) = delete;
27dab0604aSKrzysztof Grobelny 
28e3cb5a31SEd Tanous     ~AsyncResp()
29e3cb5a31SEd Tanous     {
30c0a1c8a0SIwona Klimaszewska         if (func && res.result() == boost::beast::http::status::ok)
31c0a1c8a0SIwona Klimaszewska         {
32c0a1c8a0SIwona Klimaszewska             func();
33c0a1c8a0SIwona Klimaszewska         }
34c0a1c8a0SIwona Klimaszewska 
35e3cb5a31SEd Tanous         res.end();
36e3cb5a31SEd Tanous     }
37e3cb5a31SEd Tanous 
38e3cb5a31SEd Tanous     crow::Response& res;
3923a21a1cSEd Tanous     std::function<void()> func;
40e3cb5a31SEd Tanous };
41e3cb5a31SEd Tanous 
42e3cb5a31SEd Tanous } // namespace bmcweb
43