未验证 提交 c3f2765f 编写于 作者: J Jin Hai 提交者: GitHub

Merge pull request #2035 from natoka/master

Move http query parser
......@@ -78,58 +78,6 @@ WebErrorMap(ErrorCode code) {
}
/////////////////////////////////// Private methods ///////////////////////////////////////
Status
WebRequestHandler::ParseQueryInteger(const OQueryParams& query_params, const std::string& key, int64_t& value,
bool nullable) {
auto query = query_params.get(key.c_str());
if (nullptr != query.get() && query->getSize() > 0) {
std::string value_str = query->std_str();
if (!ValidationUtil::ValidateStringIsNumber(value_str).ok()) {
return Status(ILLEGAL_QUERY_PARAM,
"Query param \'offset\' is illegal, only non-negative integer supported");
}
value = std::stol(value_str);
} else if (!nullable) {
return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required");
}
return Status::OK();
}
Status
WebRequestHandler::ParseQueryStr(const OQueryParams& query_params, const std::string& key, std::string& value,
bool nullable) {
auto query = query_params.get(key.c_str());
if (nullptr != query.get() && query->getSize() > 0) {
value = query->std_str();
} else if (!nullable) {
return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required");
}
return Status::OK();
}
Status
WebRequestHandler::ParseQueryBool(const OQueryParams& query_params, const std::string& key, bool& value,
bool nullable) {
auto query = query_params.get(key.c_str());
if (nullptr != query.get() && query->getSize() > 0) {
std::string value_str = query->std_str();
if (!ValidationUtil::ValidateStringIsBool(value_str).ok()) {
return Status(ILLEGAL_QUERY_PARAM, "Query param \'all_required\' must be a bool");
}
value = value_str == "True" || value_str == "true";
return Status::OK();
}
if (!nullable) {
return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required");
}
return Status::OK();
}
void
WebRequestHandler::AddStatusToJson(nlohmann::json& json, int64_t code, const std::string& msg) {
json["code"] = (int64_t)code;
......
......@@ -77,16 +77,6 @@ class WebRequestHandler {
return context_ptr;
}
private:
Status
ParseQueryInteger(const OQueryParams& query_params, const std::string& key, int64_t& value, bool nullable = true);
Status
ParseQueryStr(const OQueryParams& query_params, const std::string& key, std::string& value, bool nullable = true);
Status
ParseQueryBool(const OQueryParams& query_params, const std::string& key, bool& value, bool nullable = true);
private:
void
AddStatusToJson(nlohmann::json& json, int64_t code, const std::string& msg);
......
......@@ -11,6 +11,8 @@
#include "server/web_impl/utils/Util.h"
#include "utils/ValidationUtil.h"
namespace milvus {
namespace server {
namespace web {
......@@ -54,6 +56,55 @@ CopyBinRowRecords(const OList<OList<OInt64>::ObjectWrapper>::ObjectWrapper& reco
return Status::OK();
}
Status
ParseQueryInteger(const OQueryParams& query_params, const std::string& key, int64_t& value, bool nullable) {
auto query = query_params.get(key.c_str());
if (nullptr != query.get() && query->getSize() > 0) {
std::string value_str = query->std_str();
if (!ValidationUtil::ValidateStringIsNumber(value_str).ok()) {
return Status(ILLEGAL_QUERY_PARAM,
"Query param \'offset\' is illegal, only non-negative integer supported");
}
value = std::stol(value_str);
} else if (!nullable) {
return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required");
}
return Status::OK();
}
Status
ParseQueryStr(const OQueryParams& query_params, const std::string& key, std::string& value, bool nullable) {
auto query = query_params.get(key.c_str());
if (nullptr != query.get() && query->getSize() > 0) {
value = query->std_str();
} else if (!nullable) {
return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required");
}
return Status::OK();
}
Status
ParseQueryBool(const OQueryParams& query_params, const std::string& key, bool& value, bool nullable) {
auto query = query_params.get(key.c_str());
if (nullptr != query.get() && query->getSize() > 0) {
std::string value_str = query->std_str();
if (!ValidationUtil::ValidateStringIsBool(value_str).ok()) {
return Status(ILLEGAL_QUERY_PARAM, "Query param \'all_required\' must be a bool");
}
value = value_str == "True" || value_str == "true";
return Status::OK();
}
if (!nullable) {
return Status(QUERY_PARAM_LOSS, "Query param \"" + key + "\" is required");
}
return Status::OK();
}
} // namespace web
} // namespace server
} // namespace milvus
......@@ -28,6 +28,15 @@ CopyRowRecords(const OList<OList<OFloat32>::ObjectWrapper>::ObjectWrapper& recor
Status
CopyBinRowRecords(const OList<OList<OInt64>::ObjectWrapper>::ObjectWrapper& records, std::vector<uint8_t>& vectors);
Status
ParseQueryInteger(const OQueryParams& query_params, const std::string& key, int64_t& value, bool nullable = true);
Status
ParseQueryStr(const OQueryParams& query_params, const std::string& key, std::string& value, bool nullable = true);
Status
ParseQueryBool(const OQueryParams& query_params, const std::string& key, bool& value, bool nullable = true);
} // namespace web
} // namespace server
} // namespace milvus
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册