From 116eadbd6f7a06803f8bd522947d8e4fc382e6f3 Mon Sep 17 00:00:00 2001 From: Silvan Calarco Date: Fri, 31 May 2024 14:08:15 +0200 Subject: [PATCH] DistroqueryAPI: implementation of /get_repositories --- src/DistroqueryAPI.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++ src/DistroqueryAPI.hpp | 45 ++++++++++++++++++++ src/distroquery.cpp | 4 +- 3 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 src/DistroqueryAPI.cpp create mode 100644 src/DistroqueryAPI.hpp diff --git a/src/DistroqueryAPI.cpp b/src/DistroqueryAPI.cpp new file mode 100644 index 0000000..a8af246 --- /dev/null +++ b/src/DistroqueryAPI.cpp @@ -0,0 +1,94 @@ +/* + * distroquery_api - API for querying data generated by distromatic + * + * Copyright (C) 2024 by Silvan Calarco + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of version 2 of the GNU General Public License as published by the + * Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY, to the extent permitted by law; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "DistroqueryAPI.hpp" + +vector split(string str, string token) { + vectorresult; + while(str.size()){ + auto index = str.find(token); + if(index!=string::npos){ + result.push_back(str.substr(0,index)); + str = str.substr(index+token.size()); + if(str.size()==0)result.push_back(str); + }else{ + result.push_back(str); + str = ""; + } + } + return result; +} + +DistroqueryAPI::DistroqueryAPI(configTag* conf): config(conf) { +} + +json DistroqueryAPI::configToJsonRepositories() { + json j; + j["repositories"] = {}; + struct configTag *ct = config; + while (ct) { + json archs = {}; + for (auto arch: ct->arch) { + if (arch == NULL) break; + archs.push_back(arch); + } + json parents = json::array(); + for (auto parent: ct->repository) { + if (parent == NULL) break; + if (parent->tag != ct->tag) parents.push_back(parent->tag); + } + json repository = { + { "tag", ct->tag }, + { "archs", archs }, + { "description", ct->description }, + { "parents", parents } + }; + j["repositories"].push_back(repository); + ct = ct->next; + } + return j; +} + +void DistroqueryAPI::getApiResponse(string path_info) { + cout << "Content-Type: application/json;charset=utf-8\n\n" << endl; + + string path, query_string; + std::vector path_split; + + std::vector v = split(path_info, "?"); + + if (v.size() > 0) { + path = v[0]; + path_split = split(path, "/"); + } + if (v.size() > 1) query_string = v[1]; + + if (path_split.size() > 0 ) { + if (path_split[0] == "get_repositories") { + // API service: get_repository + cout << configToJsonRepositories(); + } else { + cout << setw(4) << json::meta() << endl; + } + } else { + cout << "{\"path_info\":\"" << path << "\",\"query_string\":\"" << query_string << "\"}"; + } + + exit(0); +} diff --git a/src/DistroqueryAPI.hpp b/src/DistroqueryAPI.hpp new file mode 100644 index 0000000..8c2c738 --- /dev/null +++ b/src/DistroqueryAPI.hpp @@ -0,0 +1,45 @@ +/* + * distroquery_api - API for querying data generated by distromatic + * + * Copyright (C) 2024 by Silvan Calarco + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of version 2 of the GNU General Public License as published by the + * Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY, to the extent permitted by law; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __DISTROQUERY_API_H +#define __DISTROQUERY_API_H + +#define API_PREFIX "/api/v1/" + +#include +#include +#include +#include + +#include "config.h" + +using namespace std; +using json = nlohmann::json; + +class DistroqueryAPI { + public: + DistroqueryAPI(configTag* conf); + void getApiResponse(string path_info); + + private: + configTag *config; + json configToJsonRepositories(); +}; + +#endif // __DISTROQUERY_API_H diff --git a/src/distroquery.cpp b/src/distroquery.cpp index 1371496..b574973 100644 --- a/src/distroquery.cpp +++ b/src/distroquery.cpp @@ -1628,8 +1628,8 @@ main(int argc, char *argv[]) query_string = getenv("QUERY_STRING"); if (strncmp(path_info, "/api/v1/", strlen(API_PREFIX)) == 0) { - DistroqueryAPI api; - api.getApiResponse(&path_info[strlen(API_PREFIX)]); + DistroqueryAPI *api = new DistroqueryAPI(firstconfigtag); + api->getApiResponse(&path_info[strlen(API_PREFIX)]); } else if (strlen(path_info) > 0) { cout << "Content-Type: text/html;charset=utf-8" << endl; cout << "Status: 400 Bad request" << endl << endl;