Skip to content

Commit

Permalink
complete api
Browse files Browse the repository at this point in the history
  • Loading branch information
noahrav committed Dec 30, 2023
1 parent 2e39075 commit 621c496
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 17 deletions.
4 changes: 2 additions & 2 deletions PROTOCOL.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ API Protocol
| / | GET | | string | Check if server is up |
| /chgen | POST | JSON | JSON | Scan builds for changes, returns changelog and exports it into a text file |
| /transfer | POST | JSON | JSON | Scan builds for transfer changelog, returns changelog |
| /transfer | GET | | string | Executes transfer based on last scanned transfer changelog |
| /transfer/confirm | POST | JSON | string | Executes transfer based on last scanned transfer changelog |
| /transfer/changelog | GET | | JSON | Returns last scanned transfer changelog |
| /submit | POST | JSON | JSON | Scan builds for submit changelog, returns changelog |
| /submit | GET | | string | Executes submit based on last scanned submission changelog |
| /submit/confirm | GET | JSON | string | Executes submit based on last scanned submission changelog |
| /submit/changelog | GET | | JSON | Returns last scanned submit changelog |
115 changes: 100 additions & 15 deletions src/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ namespace CUSubmitterService {
Routes::Get(router, "/", Routes::bind(&Service::ready, this));
Routes::Post(router, "/chgen", Routes::bind(&Service::generateChangelog, this));
Routes::Post(router, "/transfer", Routes::bind(&Service::generateTransferChangelog, this));
Routes::Get(router, "/transfer", Routes::bind(&Service::transfer, this));
Routes::Post(router, "/transfer/confirm", Routes::bind(&Service::transfer, this));
Routes::Get(router, "/transfer/changelog", Routes::bind(&Service::lastTransferChangelog, this));
Routes::Post(router, "/submit", Routes::bind(&Service::generateSubmissionChangelog, this));
Routes::Get(router, "/submit", Routes::bind(&Service::submit, this));
Routes::Post(router, "/submit/confirm", Routes::bind(&Service::submit, this));
Routes::Get(router, "/submit/changelog", Routes::bind(&Service::lastSubmissionChangelog, this));
}

Expand All @@ -61,13 +61,14 @@ namespace CUSubmitterService {
try {
logRequest(request);

const std::string body = request.body();
const std::string& body = request.body();
log("Raw content :\n" + body);

rapidjson::Document document;
document.Parse(body.c_str());

if (!(document.HasMember("base_path") && document.HasMember("modified_path"))) {
error("Incorrect arguments");
response.send(Pistache::Http::Code::Bad_Request);
return;
}
Expand Down Expand Up @@ -106,26 +107,37 @@ namespace CUSubmitterService {
try {
logRequest(request);

const std::string body = request.body();
const std::string& body = request.body();
log("Raw content :\n" + body);

rapidjson::Document document;
document.Parse(body.c_str());

if (!(document.HasMember("unmodified_copy_path") && document.HasMember("unmodified_copy_path") && document.HasMember("destination_path"))) {
if (!(document.HasMember("unmodified_copy_path") && document.HasMember("modified_copy_path"))) {
error("Incorrect arguments");
response.send(Pistache::Http::Code::Bad_Request);
return;
}

const std::string unmodified_copy_path = document["unmodified_copy_path"].GetString();
const std::string modified_copy_path = document["modified_copy_path"].GetString();
const std::string destination_path = document["destination_path"].GetString();

log("Parameter unmodified_copy_path : " + unmodified_copy_path);
log("Parameter modified_copy_path : " + modified_copy_path);
log("Parameter destination_path : " + destination_path);

response.send(Pistache::Http::Code::Ok);
const auto changelog = transfer::DevbuildTransferer::getTransferChangelog(unmodified_copy_path, modified_copy_path);
if (changelog == nullptr) {
response.send(Pistache::Http::Code::Bad_Request, "Could not generate changelog", MIME(Text, Plain));
return;
}

rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);

changelog->Serialize(writer);

const std::string string_changelog = sb.GetString();
response.send(Pistache::Http::Code::Ok, string_changelog, MIME(Application, Json));
} catch (const std::runtime_error &e) {
log("Error: " + std::string(e.what()));
response.send(Pistache::Http::Code::Not_Found, e.what(), MIME(Text, Plain));
Expand All @@ -139,7 +151,26 @@ namespace CUSubmitterService {
try {
logRequest(request);

response.send(Pistache::Http::Code::Ok, "CU Submitter is up and running\n", MIME(Text, Plain));
const std::string& body = request.body();
log("Raw content :\n" + body);

rapidjson::Document document;
document.Parse(body.c_str());

if (!document.HasMember("destination_path")) {
error("Incorrect arguments");
response.send(Pistache::Http::Code::Bad_Request);
return;
}

const std::string destination_path = document["destination_path"].GetString();

log("Parameter destination_path : " + destination_path);

transfer::DevbuildTransferer::transfer(destination_path);
transfer::DevbuildTransferer::exportChangelog();

response.send(Pistache::Http::Code::Ok);
} catch (const std::runtime_error &e) {
log("Error: " + std::string(e.what()));
response.send(Pistache::Http::Code::Not_Found, e.what(), MIME(Text, Plain));
Expand All @@ -153,7 +184,19 @@ namespace CUSubmitterService {
try {
logRequest(request);

response.send(Pistache::Http::Code::Ok, "CU Submitter is up and running\n", MIME(Text, Plain));
const auto changelog = transfer::DevbuildTransferer::getTransferChangelog();
if (changelog == nullptr) {
response.send(Pistache::Http::Code::Bad_Request, "Could not generate changelog", MIME(Text, Plain));
return;
}

rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);

changelog->Serialize(writer);

const std::string string_changelog = sb.GetString();
response.send(Pistache::Http::Code::Ok, string_changelog, MIME(Application, Json));
} catch (const std::runtime_error &e) {
log("Error: " + std::string(e.what()));
response.send(Pistache::Http::Code::Not_Found, e.what(), MIME(Text, Plain));
Expand All @@ -167,13 +210,14 @@ namespace CUSubmitterService {
try {
logRequest(request);

const std::string body = request.body();
const std::string& body = request.body();
log("Raw content :\n" + body);

rapidjson::Document document;
document.Parse(body.c_str());

if (!(document.HasMember("unmodified_copy_path") && document.HasMember("unmodified_copy_path"))) {
if (!(document.HasMember("unmodified_copy_path") && document.HasMember("modified_copy_path"))) {
error("Incorrect arguments");
response.send(Pistache::Http::Code::Bad_Request);
return;
}
Expand All @@ -190,7 +234,19 @@ namespace CUSubmitterService {
log("Parameter archive_path : " + archive_path);
}

response.send(Pistache::Http::Code::Ok);
const auto changelog = submit::SubmissionBuilder::getSubmissionChangelog(unmodified_copy_path, modified_copy_path);
if (changelog == nullptr) {
response.send(Pistache::Http::Code::Bad_Request, "Could not generate changelog", MIME(Text, Plain));
return;
}

rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);

changelog->Serialize(writer);

const std::string string_changelog = sb.GetString();
response.send(Pistache::Http::Code::Ok, string_changelog, MIME(Application, Json));
} catch (const std::runtime_error &e) {
log("Error: " + std::string(e.what()));
response.send(Pistache::Http::Code::Not_Found, e.what(), MIME(Text, Plain));
Expand All @@ -204,7 +260,24 @@ namespace CUSubmitterService {
try {
logRequest(request);

response.send(Pistache::Http::Code::Ok, "CU Submitter is up and running\n", MIME(Text, Plain));
const std::string& body = request.body();
log("Raw content :\n" + body);

rapidjson::Document document;
document.Parse(body.c_str());

std::string archive_path;

if(document.HasMember("archive_path")) {
archive_path = document["archive_path"].GetString();
log("Parameter archive_path : " + archive_path);

submit::SubmissionBuilder::submit(archive_path);
} else {
submit::SubmissionBuilder::submit();
}

response.send(Pistache::Http::Code::Ok);
} catch (const std::runtime_error &e) {
log("Error: " + std::string(e.what()));
response.send(Pistache::Http::Code::Not_Found, e.what(), MIME(Text, Plain));
Expand All @@ -218,7 +291,19 @@ namespace CUSubmitterService {
try {
logRequest(request);

response.send(Pistache::Http::Code::Ok, "CU Submitter is up and running\n", MIME(Text, Plain));
const auto changelog = submit::SubmissionBuilder::getSubmissionChangelog();
if (changelog == nullptr) {
response.send(Pistache::Http::Code::Bad_Request, "Could not generate changelog", MIME(Text, Plain));
return;
}

rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);

changelog->Serialize(writer);

const std::string string_changelog = sb.GetString();
response.send(Pistache::Http::Code::Ok, string_changelog, MIME(Application, Json));
} catch (const std::runtime_error &e) {
log("Error: " + std::string(e.what()));
response.send(Pistache::Http::Code::Not_Found, e.what(), MIME(Text, Plain));
Expand Down

0 comments on commit 621c496

Please sign in to comment.