Refactor client code

This commit is contained in:
Martchus 2021-03-27 19:55:24 +01:00
parent 163ff91bff
commit bfbde3f957
3 changed files with 26 additions and 18 deletions

View File

@ -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<std::list<LibPkg::PackageSearchResult>>(jsonData.data(), jsonData.size());
tabulate::Table t;
@ -167,7 +167,7 @@ template <typename List> inline std::string formatList(const List &list)
return joinStrings(list, ", ");
}
std::string formatDependencies(const std::vector<LibPkg::Dependency> &deps)
static std::string formatDependencies(const std::vector<LibPkg::Dependency> &deps)
{
auto asStrings = std::vector<std::string>();
asStrings.reserve(deps.size());
@ -177,7 +177,7 @@ std::string formatDependencies(const std::vector<LibPkg::Dependency> &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<std::list<LibPkg::Package>>(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<std::list<LibRepoMgr::BuildAction>>(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<LibRepoMgr::WebClient::Response>(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;

View File

@ -232,7 +232,7 @@ void SslSession::closed(boost::beast::error_code ec)
template <typename SessionType, typename... ArgType>
std::variant<string, std::shared_ptr<Session>, std::shared_ptr<SslSession>> runSession(const std::string &host, const std::string &port,
const std::string &target, std::function<void(SessionData, const HttpClientError &)> &&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<SessionType>(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<string, std::shared_ptr<Session>, std::shared_ptr<SslSession>> runS
"Basic " + encodeBase64(reinterpret_cast<const std::uint8_t *>(authInfo.data()), static_cast<std::uint32_t>(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<string, std::shared_ptr<Session>, std::shared_ptr<SslSession>>(std::move(session));
}
std::variant<string, std::shared_ptr<Session>, std::shared_ptr<SslSession>> runSessionFromUrl(boost::asio::io_context &ioContext,
boost::asio::ssl::context &sslContext, std::string_view url, std::function<void(SessionData, const HttpClientError &)> &&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<string, std::shared_ptr<Session>, std::shared_ptr<SslSession>> runS
}
if (ssl) {
return runSession<SslSession>(host, port, target, move(handler), move(destinationPath), userName, password, ioContext, sslContext);
return runSession<SslSession>(host, port, target, move(handler), move(destinationPath), userName, password, verb, ioContext, sslContext);
} else {
return runSession<Session>(host, port, target, move(handler), move(destinationPath), userName, password, ioContext);
return runSession<Session>(host, port, target, move(handler), move(destinationPath), userName, password, verb, ioContext);
}
}

View File

@ -139,7 +139,8 @@ inline SslSession::SslSession(boost::asio::io_context &ioContext, boost::asio::s
LIBREPOMGR_EXPORT std::variant<std::string, std::shared_ptr<Session>, std::shared_ptr<SslSession>> runSessionFromUrl(
boost::asio::io_context &ioContext, boost::asio::ssl::context &sslContext, std::string_view url,
std::function<void(SessionData data, const HttpClientError &error)> &&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