Skip to content

Commit

Permalink
add after execute call handler to perform actions on http response
Browse files Browse the repository at this point in the history
before used handling of response
  • Loading branch information
virgiliofornazin committed Oct 29, 2024
1 parent 76a01d5 commit 90f31d9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 10 additions & 6 deletions include/cpp_http/http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace cpp_http

protected:
template <typename http_stream_type, typename duration_type, typename callback_type>
void do_execute_http_request(http_stream_type& http_stream, duration_type const& timeout_interval, std::shared_ptr<cpp_http::atomic_flag>& callback_called, callback_type& callback)
void do_execute_http_request(http_stream_type& http_stream, duration_type const& timeout_interval, std::shared_ptr<cpp_http::atomic_flag>& callback_called, http_request::shared_ptr request_ptr, callback_type& callback)
{
if (!_connected)
{
Expand All @@ -73,7 +73,7 @@ namespace cpp_http
auto self = shared_from_this();

_strand.dispatch(
[this, self, &http_stream, callback_called, callback]
[this, self, &http_stream, callback_called, request_ptr, callback]
()
{
if (!_connected)
Expand All @@ -82,7 +82,7 @@ namespace cpp_http
}

boost::beast::http::async_write(http_stream, _http_request, cpp_http_asio::bind_executor(_strand,
[this, self, &http_stream, callback_called, callback]
[this, self, &http_stream, callback_called, request_ptr, callback]
(boost::beast::error_code ec, size_t bytes_transferred) mutable
{
cpp_hpp_diagnostic_trace([&]() { std::stringstream ss; ss << "beast::async_write(http), ec: " << ec; return ss.str(); });
Expand All @@ -109,7 +109,7 @@ namespace cpp_http
}

boost::beast::http::async_read(http_stream, _flat_buffer, _http_response, cpp_http_asio::bind_executor(_strand,
[this, self, callback_called, callback]
[this, self, callback_called, request_ptr, callback]
(boost::beast::error_code ec, size_t bytes_transferred) mutable
{
cpp_hpp_diagnostic_trace([&]() { std::stringstream ss; ss << "beast::async_read(http), ec: " << ec; return ss.str(); });
Expand All @@ -134,6 +134,8 @@ namespace cpp_http

impl::from_beast_http_response(*response_ptr.get(), _http_response);

after_execute(*response_ptr.get(), *request_ptr.get(), _http_target_string);

debug_info([&]() { return cpp_http_format::format("http{} async response received:\n{}", (_uri_protocol_is_secure ? "s" : ""), response_ptr->to_string()); });

callback(response_ptr, {});
Expand Down Expand Up @@ -277,6 +279,8 @@ namespace cpp_http

impl::from_beast_http_response(response, _http_response);

after_execute(response, request, _http_target_string);

debug_info([&]() { return cpp_http_format::format("http sync response received:\n{}", response.to_string()); });

return response;
Expand Down Expand Up @@ -377,11 +381,11 @@ namespace cpp_http

if (_uri_protocol_is_secure)
{
do_execute_http_request(_https_stream, timeout_interval, callback_called, callback);
do_execute_http_request(_https_stream, timeout_interval, callback_called, request_ptr, callback);
}
else
{
do_execute_http_request(_http_stream, timeout_interval, callback_called, callback);
do_execute_http_request(_http_stream, timeout_interval, callback_called, request_ptr, callback);
}
}));
}
Expand Down
6 changes: 5 additions & 1 deletion include/cpp_http/impl/http_client_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ namespace cpp_http
}

protected:
virtual void before_execute(http_request& request /* info */, std::string const& /* final_http_target_string */)
virtual void before_execute(http_request& /* request */, std::string const& /* final_http_target_string */)
{
}

virtual void after_execute(http_response& /* response */, http_request const& /* request */, std::string const& /* final_http_target_string */)
{
}

Expand Down

0 comments on commit 90f31d9

Please # to comment.