From e73d648e1b2034257e1cd7a9f1ffbb7c14aef852 Mon Sep 17 00:00:00 2001 From: Silvan Calarco Date: Sun, 2 Jun 2024 10:39:14 +0200 Subject: [PATCH] DistroqueryAPI: provide initial package source details response --- src/DistroqueryAPI.cpp | 61 +++++++++++++++++++++++++++++++----------- src/DistroqueryAPI.hpp | 3 ++- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/DistroqueryAPI.cpp b/src/DistroqueryAPI.cpp index 2c42715..f816598 100644 --- a/src/DistroqueryAPI.cpp +++ b/src/DistroqueryAPI.cpp @@ -56,25 +56,19 @@ void DistroqueryAPI::sendErrorResponse(string message) { exit(0); } -json DistroqueryAPI::getPackageSourceDetailsById(string repository, long id) { +json DistroqueryAPI::getPackageSourceDetailsById(configTag* ct, long id) { json j; string sql; sqlite3_stmt *stmt; - struct configTag* ct = findRepositoryByTag(repository.c_str()); - if (ct == NULL) { - j["error"] = "repository with tag '" + repository + "' does not exist"; - return j; - } - - auto dbs = openRepositoryDatabase(ct); - if (!dbs) { - j["error"] = "error opening sources database for repository " + repository; + auto db = openRepositoryDatabase(ct); + if (!db) { + j["error"] = "error opening sources database for repository " + string(ct->tag); return j; } sql = "SELECT * FROM sources,packagers WHERE sources.id=" + to_string(id) + " AND sources.id_packager=packagers.id"; - if (!sqlite3_prepare_v2(dbs, sql.c_str(), sql.length(), &stmt, NULL) == SQLITE_OK) { + if (!sqlite3_prepare_v2(db, sql.c_str(), sql.length(), &stmt, NULL) == SQLITE_OK) { j["error"] = "error preparing query '" + sql + "'"; return j; } @@ -140,6 +134,38 @@ json DistroqueryAPI::getRepositoryPackages(string repository) { return j; } +json DistroqueryAPI::getPackageSourceDetails(string repository, string package) { + json j; + string sql; + sqlite3_stmt *stmt; + + struct configTag* ct = findRepositoryByTag(repository.c_str()); + if (ct == NULL) { + j["error"] = "repository with tag '" + repository + "' does not exist"; + return j; + } + + auto db = openRepositoryDatabase(ct); + if (!db) { + j["error"] = "error opening source database for repository " + repository; + return j; + } + + sql = "SELECT * FROM sources WHERE name = '" + package + "'"; + + if (sqlite3_prepare_v2(db, sql.c_str(), sql.length(), &stmt, NULL) == SQLITE_OK) { + long id; + if (sqlite3_step(stmt) == SQLITE_ROW) { + id = sqlite3_column_int(stmt,sqlite3_find_column_id(stmt, NULL, "id")); + return getPackageSourceDetailsById(ct, id); + } + } else { + j["error"] = "error preparing query '" + sql + "'"; + return j; + } + return j; +} + json DistroqueryAPI::getPackageDetails(string repository, string package, string arch) { json j; string sql; @@ -184,7 +210,7 @@ json DistroqueryAPI::getPackageDetails(string repository, string package, string // Source info long id_source = sqlite3_column_int(stmt,sqlite3_find_column_id(stmt, NULL, "id_source")); - j["source"] = getPackageSourceDetailsById(repository, id_source); + j["source"] = getPackageSourceDetailsById(ct, id_source); // Download URL j["download_url"] = string(ct->download_prefix) + ct->download_dir + "/RPMS." + string(j["arch"]) + "/" + @@ -294,9 +320,14 @@ void DistroqueryAPI::getApiResponse(string path_info) { cout << packages.dump(); } else if (path_split[0] == "package") { // API service: package - if (path_split.size() != 4) - sendErrorResponse("expected exactly 4 arguments for " + path_split[0]); - auto details = getPackageDetails(path_split[1], path_split[2], path_split[3]); + if (path_split.size() < 3 || path_split.size() > 4) + sendErrorResponse("expected 3 or 4 arguments for " + path_split[0]); + json details; + if (path_split.size() == 3) { + details = getPackageSourceDetails(path_split[1], path_split[2]); + } else { + details = getPackageDetails(path_split[1], path_split[2], path_split[3]); + } cout << details.dump(); } else { sendErrorResponse("invalid request for service '" + path_split[0]); diff --git a/src/DistroqueryAPI.hpp b/src/DistroqueryAPI.hpp index 5992f61..e7efae7 100644 --- a/src/DistroqueryAPI.hpp +++ b/src/DistroqueryAPI.hpp @@ -42,8 +42,9 @@ class DistroqueryAPI { json configToJsonRepositories(); void sendErrorResponse(string message); json getRepositoryPackages(string repository); + json getPackageSourceDetailsById(configTag* ct, long id); + json getPackageSourceDetails(string repository, string package); json getPackageDetails(string repository, string package, string arch); - json getPackageSourceDetailsById(string repository, long id); }; #endif // __DISTROQUERY_API_H