DistroqueryAPI: implementation of /get_repositories
This commit is contained in:
parent
e6616f603b
commit
116eadbd6f
94
src/DistroqueryAPI.cpp
Normal file
94
src/DistroqueryAPI.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* distroquery_api - API for querying data generated by distromatic
|
||||
*
|
||||
* Copyright (C) 2024 by Silvan Calarco <silvan.calarco@mambasoft.it>
|
||||
*
|
||||
* 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<string> split(string str, string token) {
|
||||
vector<string>result;
|
||||
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<std::string> path_split;
|
||||
|
||||
std::vector<std::string> 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);
|
||||
}
|
45
src/DistroqueryAPI.hpp
Normal file
45
src/DistroqueryAPI.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* distroquery_api - API for querying data generated by distromatic
|
||||
*
|
||||
* Copyright (C) 2024 by Silvan Calarco <silvan.calarco@mambasoft.it>
|
||||
*
|
||||
* 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 <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#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
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user