refactor: add unified gatherArgument helper for request parsing
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user