mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 01:54:43 +00:00
Formatted src/web_client
This commit is contained in:
@@ -26,47 +26,47 @@ const std::regex kUrlRegex(
|
|||||||
R"(^(https?://[^/?#]+)(/[^?#]*(?:\?[^#]*)?(?:#.*)?)?)");
|
R"(^(https?://[^/?#]+)(/[^?#]*(?:\?[^#]*)?(?:#.*)?)?)");
|
||||||
|
|
||||||
std::pair<std::string, std::string> SplitUrl(const std::string& url) {
|
std::pair<std::string, std::string> SplitUrl(const std::string& url) {
|
||||||
std::smatch match;
|
std::smatch match;
|
||||||
if (!std::regex_match(url, match, kUrlRegex)) {
|
if (!std::regex_match(url, match, kUrlRegex)) {
|
||||||
throw std::invalid_argument("[HttpWebClient] Malformed URL: " + url);
|
throw std::invalid_argument("[HttpWebClient] Malformed URL: " + url);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {match[1].str(), match[2].matched ? match[2].str() : "/"};
|
return {match[1].str(), match[2].matched ? match[2].str() : "/"};
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
std::string HttpWebClient::Get(const std::string& url) {
|
std::string HttpWebClient::Get(const std::string& url) {
|
||||||
const auto [origin, path] = SplitUrl(url);
|
const auto [origin, path] = SplitUrl(url);
|
||||||
|
|
||||||
httplib::Client client(origin);
|
httplib::Client client(origin);
|
||||||
client.set_follow_location(true);
|
client.set_follow_location(true);
|
||||||
client.set_connection_timeout(kConnectionTimeoutSeconds);
|
client.set_connection_timeout(kConnectionTimeoutSeconds);
|
||||||
client.set_read_timeout(kReadTimeoutSeconds);
|
client.set_read_timeout(kReadTimeoutSeconds);
|
||||||
client.set_default_headers({{"Accept", "application/json"},
|
client.set_default_headers({{"Accept", "application/json"},
|
||||||
{"User-Agent", "biergarten-pipeline/1.0"}});
|
{"User-Agent", "biergarten-pipeline/1.0"}});
|
||||||
|
|
||||||
const httplib::Result result = client.Get(path);
|
const httplib::Result result = client.Get(path);
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
std::format("[HttpWebClient] Request failed for URL: {} — {}", url,
|
std::format("[HttpWebClient] Request failed for URL: {} — {}", url,
|
||||||
httplib::to_string(result.error())));
|
httplib::to_string(result.error())));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result->status < kSuccessMin || result->status >= kSuccessMax) {
|
if (result->status < kSuccessMin || result->status >= kSuccessMax) {
|
||||||
if (logger_) {
|
if (logger_) {
|
||||||
logger_->Log({.level = LogLevel::Error,
|
logger_->Log({.level = LogLevel::Error,
|
||||||
.phase = PipelinePhase::Enrichment,
|
.phase = PipelinePhase::Enrichment,
|
||||||
.message = std::format(
|
.message = std::format(
|
||||||
"[HttpWebClient] Request failed for URL: {}", url)});
|
"[HttpWebClient] Request failed for URL: {}", url)});
|
||||||
}
|
}
|
||||||
throw std::runtime_error(std::format("[HttpWebClient] HTTP {} for URL: {}",
|
throw std::runtime_error(std::format(
|
||||||
result->status, url));
|
"[HttpWebClient] HTTP {} for URL: {}", result->status, url));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result->body;
|
return result->body;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string HttpWebClient::EncodeURL(const std::string& value) {
|
std::string HttpWebClient::EncodeURL(const std::string& value) {
|
||||||
return httplib::encode_uri_component(value);
|
return httplib::encode_uri_component(value);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user