session.setopt(CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
}
- std::optional<int> perform_get_request(const std::string &url, const libcurl::EasySession::ReceiveHandler &receiver, const std::optional<std::string> &user_agent,
- const http::Client::GetRequestProgressHandler &progress_handler)
- {
- libcurl::EasySession session;
- if (!session.is_valid()) {
- return std::nullopt;
+ class GetRequest {
+ public:
+ GetRequest(const std::string &url, const std::optional<std::string> &user_agent = {})
+ : url(url)
+ , user_agent(user_agent)
+ {
}
- session.common_setup(url, HTTP_CONNECTION_TIMEOUT);
- setup_http_option(session, user_agent);
-
- session.receiver_setup(receiver);
- if (progress_handler) {
- auto handler = [progress_handler](size_t dltotal, size_t dlnow, size_t, size_t) {
- return progress_handler({ dltotal, dlnow });
- };
- session.progress_setup(handler);
+ std::optional<int> perform()
+ {
+ libcurl::EasySession session;
+ if (!session.is_valid()) {
+ return std::nullopt;
+ }
+
+ session.common_setup(this->url, HTTP_CONNECTION_TIMEOUT);
+ setup_http_option(session, this->user_agent);
+
+ session.receiver_setup(this->receiver);
+ if (this->progress_handler) {
+ auto handler = [handler = this->progress_handler](size_t dltotal, size_t dlnow, size_t, size_t) {
+ return handler({ dltotal, dlnow });
+ };
+ session.progress_setup(handler);
+ }
+
+ if (!session.perform()) {
+ return std::nullopt;
+ }
+
+ long status;
+ session.getinfo(CURLINFO_RESPONSE_CODE, &status);
+ return static_cast<int>(status);
}
- if (!session.perform()) {
- return std::nullopt;
- }
+ libcurl::EasySession::ReceiveHandler receiver;
+ http::Client::GetRequestProgressHandler progress_handler;
- long status;
- session.getinfo(CURLINFO_RESPONSE_CODE, &status);
- return static_cast<int>(status);
- }
+ private:
+ std::string url;
+ std::optional<std::string> user_agent;
+ };
}
/*!
std::optional<Response> Client::get(const std::string &url, GetRequestProgressHandler progress_handler)
{
Response response{};
- auto receiver = [&response](char *buf, size_t n) {
+
+ GetRequest request(url, this->user_agent);
+ request.receiver = [&response](char *buf, size_t n) {
response.body.append(buf, n);
return n;
};
+ request.progress_handler = progress_handler;
- const auto status_opt = perform_get_request(url, receiver, this->user_agent, progress_handler);
+ const auto status_opt = request.perform();
if (!status_opt) {
return std::nullopt;
}
std::optional<Response> Client::get(const std::string &url, const std::filesystem::path &path, GetRequestProgressHandler progress_handler)
{
std::ofstream ofs(path, std::ios::binary);
- auto receiver = [&ofs](char *buf, size_t n) {
+
+ GetRequest request(url, this->user_agent);
+ request.receiver = [&ofs](char *buf, size_t n) {
ofs.write(buf, n);
return ofs ? n : 0;
};
+ request.progress_handler = progress_handler;
- const auto status_opt = perform_get_request(url, receiver, this->user_agent, progress_handler);
+ const auto status_opt = request.perform();
if (!status_opt) {
return std::nullopt;
}