refactor: add unified gatherArgument helper for request parsing

This commit is contained in:
2026-07-16 10:01:10 +03:00
parent 5df8da3a60
commit db11d7a4d9
3 changed files with 10 additions and 7 deletions
+7
View File
@@ -28,4 +28,11 @@ PIHTTP::MessageMutable noContent() {
return msg;
}
PIString gatherArgument(const PIHTTP::MessageConst & request, const PIString & key) {
PIJSON js = PIJSON::fromJSON(PIString::fromUTF8(request.body()));
if (js.contains(key)) return js[key].toString();
if (request.arguments().contains(key)) return request.arguments().value(key);
return {};
}
} // namespace MessageUtils
+2
View File
@@ -12,6 +12,8 @@ PIHTTP::MessageMutable errorReply(PIHTTP::Code code, const PIString & message);
PIHTTP::MessageMutable successReply(const PIJSON & json);
PIHTTP::MessageMutable noContent();
PIString gatherArgument(const PIHTTP::MessageConst & request, const PIString & key);
} // namespace MessageUtils
#endif
+1 -7
View File
@@ -183,13 +183,7 @@ PIHTTP::MessageMutable Server::deletePipeline(const PIHTTP::MessageConst & reque
PIHTTP::MessageMutable Server::startRun(const PIHTTP::MessageConst & request) {
piCout << "POST /api/runs";
PIString body = PIString::fromUTF8(request.body());
PIJSON j = PIJSON::fromJSON(body);
if (!j.isObject()) {
return MessageUtils::errorReply(PIHTTP::Code::BadRequest, "Invalid JSON");
}
PIString pipeline_id = j["pipeline_id"].toString();
PIString pipeline_id = MessageUtils::gatherArgument(request, "pipeline_id");
if (pipeline_id.isEmpty()) {
return MessageUtils::errorReply(PIHTTP::Code::BadRequest, "pipeline_id is required");
}