DistroqueryAPI: added /repository API service; added description in packages response

This commit is contained in:
Silvan Calarco 2024-06-01 18:50:04 +02:00
parent 457f455bf3
commit 9bb369ca1f
2 changed files with 43 additions and 0 deletions

View File

@ -85,6 +85,7 @@ json DistroqueryAPI::getPackageSourceDetailsById(string repository, long id) {
j["epoch"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "epoch")));
j["version"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "version")));
j["release"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "release")));
j["description"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "description")));
j["group"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "groupdescr")));
j["license"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "license")));
j["url"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "url")));
@ -105,6 +106,40 @@ json DistroqueryAPI::getPackageSourceDetailsById(string repository, long id) {
return j;
}
json DistroqueryAPI::getRepositoryPackages(string repository) {
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 database for repository " + repository;
return j;
}
sql = "SELECT * FROM sources";
if (sqlite3_prepare_v2(db, sql.c_str(), sql.length(), &stmt, NULL) == SQLITE_OK) {
j = json::array();
while (sqlite3_step(stmt) == SQLITE_ROW) {
json package;
package["name"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "name")));
package["summary"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "summary")));
j.push_back(package);
}
sqlite3_finalize(stmt);
} else {
j["error"] = "error preparing query '" + sql + "'";
return j;
}
return j;
}
json DistroqueryAPI::getPackageDetails(string repository, string package, string arch) {
json j;
string sql;
@ -133,6 +168,7 @@ json DistroqueryAPI::getPackageDetails(string repository, string package, string
j["epoch"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "epoch")));
j["version"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "version")));
j["release"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "release")));
j["description"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "description")));
j["group"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "groupdescr")));
string pkg_arch = arch;
if (sqlite3_find_column_id(stmt, NULL, "arch") > 0) {
@ -250,6 +286,12 @@ void DistroqueryAPI::getApiResponse(string path_info) {
if (path_split.size() != 1)
sendErrorResponse("expected exactly 1 argument for " + path_split[0]);
cout << configToJsonRepositories();
} else if (path_split[0] == "repository") {
// API service: repository
if (path_split.size() != 2)
sendErrorResponse("expected exactly 2 arguments for " + path_split[0]);
auto packages = getRepositoryPackages(path_split[1]);
cout << packages.dump();
} else if (path_split[0] == "package") {
// API service: package
if (path_split.size() != 4)

View File

@ -41,6 +41,7 @@ class DistroqueryAPI {
configTag *config;
json configToJsonRepositories();
void sendErrorResponse(string message);
json getRepositoryPackages(string repository);
json getPackageDetails(string repository, string package, string arch);
json getPackageSourceDetailsById(string repository, long id);
};