DistroqueryAPI: provide initial package source details response

This commit is contained in:
Silvan Calarco 2024-06-02 10:39:14 +02:00
parent 9bb369ca1f
commit e73d648e1b
2 changed files with 48 additions and 16 deletions

View File

@ -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]);

View File

@ -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