diff --git a/cli/main.cpp b/cli/main.cpp index 320d84f..8bc4984 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -102,7 +102,7 @@ void ClientConfig::parse(const Argument &configFileArg, const Argument &instance } } -void configureColumnWidths(tabulate::Table &table) +static void configureColumnWidths(tabulate::Table &table) { const auto terminalSize = determineTerminalSize(); if (!terminalSize.columns) { @@ -145,7 +145,7 @@ void configureColumnWidths(tabulate::Table &table) } } -void printPackageSearchResults(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData) +static void printPackageSearchResults(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData) { const auto packages = ReflectiveRapidJSON::JsonReflector::fromJson>(jsonData.data(), jsonData.size()); tabulate::Table t; @@ -167,7 +167,7 @@ template inline std::string formatList(const List &list) return joinStrings(list, ", "); } -std::string formatDependencies(const std::vector &deps) +static std::string formatDependencies(const std::vector &deps) { auto asStrings = std::vector(); asStrings.reserve(deps.size()); @@ -177,7 +177,7 @@ std::string formatDependencies(const std::vector &deps) return formatList(asStrings); } -void printPackageDetails(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData) +static void printPackageDetails(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData) { const auto packages = ReflectiveRapidJSON::JsonReflector::fromJson>(jsonData.data(), jsonData.size()); for (const auto &package : packages) { @@ -226,7 +226,7 @@ void printPackageDetails(const LibRepoMgr::WebClient::Response::body_type::value std::cout.flush(); } -void printListOfBuildActions(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData) +static void printListOfBuildActions(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData) { const auto actions = ReflectiveRapidJSON::JsonReflector::fromJson>(jsonData.data(), jsonData.size()); const auto meta = LibRepoMgr::BuildActionMetaInfo(); @@ -253,28 +253,34 @@ void printListOfBuildActions(const LibRepoMgr::WebClient::Response::body_type::v std::cout << t << std::endl; } -void printRawData(const LibRepoMgr::WebClient::Response::body_type::value_type &rawData) +static void printRawDataForErrorHandling(const LibRepoMgr::WebClient::Response::body_type::value_type &rawData) { if (!rawData.empty()) { std::cerr << Phrases::InfoMessage << "Server replied:" << Phrases::End << rawData << '\n'; } } -void handleResponse(const std::string &url, const LibRepoMgr::WebClient::SessionData &data, const LibRepoMgr::WebClient::HttpClientError &error, - void (*printer)(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData), int &returnCode) +static void printRawData(const LibRepoMgr::WebClient::Response::body_type::value_type &rawData) +{ + std::cout << rawData << '\n'; +} + +static void handleResponse(const std::string &url, const LibRepoMgr::WebClient::SessionData &data, + const LibRepoMgr::WebClient::HttpClientError &error, void (*printer)(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData), + int &returnCode) { const auto &response = std::get(data.response); const auto &body = response.body(); if (error.errorCode != boost::beast::errc::success && error.errorCode != boost::asio::ssl::error::stream_truncated) { std::cerr << Phrases::ErrorMessage << "Unable to connect: " << error.what() << Phrases::End; std::cerr << Phrases::InfoMessage << "URL was: " << url << Phrases::End; - printRawData(body); + printRawDataForErrorHandling(body); return; } if (response.result() != boost::beast::http::status::ok) { std::cerr << Phrases::ErrorMessage << "HTTP request not successful: " << error.what() << Phrases::End; std::cerr << Phrases::InfoMessage << "URL was: " << url << Phrases::End; - printRawData(body); + printRawDataForErrorHandling(body); return; } try { @@ -293,7 +299,8 @@ void handleResponse(const std::string &url, const LibRepoMgr::WebClient::Session int main(int argc, const char *argv[]) { // define command-specific parameters - std::string path; + auto verb = boost::beast::http::verb::get; + auto path = std::string(); void (*printer)(const LibRepoMgr::WebClient::Response::body_type::value_type &jsonData) = nullptr; // read CLI args @@ -359,7 +366,7 @@ int main(int argc, const char *argv[]) sslContext.set_default_verify_paths(); LibRepoMgr::WebClient::runSessionFromUrl(ioContext, sslContext, url, std::bind(&handleResponse, std::ref(url), std::placeholders::_1, std::placeholders::_2, printer, std::ref(returnCode)), std::string(), - config.userName, config.password); + config.userName, config.password, verb); ioContext.run(); return 0; diff --git a/librepomgr/webclient/session.cpp b/librepomgr/webclient/session.cpp index 73e9d17..a44aba6 100644 --- a/librepomgr/webclient/session.cpp +++ b/librepomgr/webclient/session.cpp @@ -232,7 +232,7 @@ void SslSession::closed(boost::beast::error_code ec) template std::variant, std::shared_ptr> runSession(const std::string &host, const std::string &port, const std::string &target, std::function &&handler, std::string &&destinationPath, - std::string_view userName, std::string_view password, ArgType &&...args) + std::string_view userName, std::string_view password, boost::beast::http::verb verb, ArgType &&...args) { auto session = make_shared(args..., [handler{ move(handler) }](auto &session2, const HttpClientError &error) mutable { handler(SessionData{ session2.shared_from_this(), session2.request, session2.response, session2.destinationFilePath }, error); @@ -243,13 +243,13 @@ std::variant, std::shared_ptr> runS "Basic " + encodeBase64(reinterpret_cast(authInfo.data()), static_cast(authInfo.size()))); } session->destinationFilePath = move(destinationPath); - session->run(host.data(), port.data(), http::verb::get, target.data()); + session->run(host.data(), port.data(), verb, target.data()); return std::variant, std::shared_ptr>(std::move(session)); } std::variant, std::shared_ptr> runSessionFromUrl(boost::asio::io_context &ioContext, boost::asio::ssl::context &sslContext, std::string_view url, std::function &&handler, - std::string &&destinationPath, std::string_view userName, std::string_view password) + std::string &&destinationPath, std::string_view userName, std::string_view password, boost::beast::http::verb verb) { string host, port, target; auto ssl = false; @@ -286,9 +286,9 @@ std::variant, std::shared_ptr> runS } if (ssl) { - return runSession(host, port, target, move(handler), move(destinationPath), userName, password, ioContext, sslContext); + return runSession(host, port, target, move(handler), move(destinationPath), userName, password, verb, ioContext, sslContext); } else { - return runSession(host, port, target, move(handler), move(destinationPath), userName, password, ioContext); + return runSession(host, port, target, move(handler), move(destinationPath), userName, password, verb, ioContext); } } diff --git a/librepomgr/webclient/session.h b/librepomgr/webclient/session.h index af8d63a..835cde4 100644 --- a/librepomgr/webclient/session.h +++ b/librepomgr/webclient/session.h @@ -139,7 +139,8 @@ inline SslSession::SslSession(boost::asio::io_context &ioContext, boost::asio::s LIBREPOMGR_EXPORT std::variant, std::shared_ptr> runSessionFromUrl( boost::asio::io_context &ioContext, boost::asio::ssl::context &sslContext, std::string_view url, std::function &&handler, std::string &&destinationPath = std::string(), - std::string_view userName = std::string_view(), std::string_view password = std::string_view()); + std::string_view userName = std::string_view(), std::string_view password = std::string_view(), + boost::beast::http::verb verb = boost::beast::http::verb::get); } // namespace WebClient } // namespace LibRepoMgr