|
|
@@ -42,43 +42,53 @@ WebAPI::~WebAPI()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
-std::string WebAPI::ProcessQuery(DataStorage::DataStorageClient* pDataStorageClient, DataInterface::GraphClient* pGraphClient, const std::string& uri, const std::vector<Http::HttpPostData>& /*postData*/)
|
|
|
+Http::HttpServer::HttpReply WebAPI::ProcessQuery(DataStorage::DataStorageClient* pDataStorageClient, DataInterface::GraphClient* pGraphClient, const std::string& uri, const std::vector<Http::HttpPostData>& /*postData*/)
|
|
|
{
|
|
|
- std::string output;
|
|
|
+ if (!StringAlgorithm::iequals(uri, "/api") && !StringAlgorithm::istarts_with(uri, "/api/"))
|
|
|
+ {
|
|
|
+ Http::HttpServer::HttpReply reply;
|
|
|
+ reply.status = Http::HttpServer::HttpReply::Status::Unauthorized;
|
|
|
+ return reply;
|
|
|
+ }
|
|
|
+
|
|
|
+ std::string apiURI = uri.substr(4);
|
|
|
|
|
|
std::stringstream ss;
|
|
|
- ss << "API Query: " << uri << std::endl;
|
|
|
+ ss << "API Query: " << apiURI << std::endl;
|
|
|
Logging::Log(Logging::Severity::Debug, ss.str());
|
|
|
|
|
|
- if (StringAlgorithm::istarts_with(uri, "/log/"))
|
|
|
- output = ProcessLogQuery(pDataStorageClient, uri);
|
|
|
- else if (StringAlgorithm::istarts_with(uri, "/graphheader/"))
|
|
|
- output = ProcessGraphHeaderQuery(pGraphClient, uri);
|
|
|
- else if (StringAlgorithm::istarts_with(uri, "/graph/"))
|
|
|
- output = ProcessGraphQuery(pGraphClient, uri);
|
|
|
- else if (StringAlgorithm::istarts_with(uri, "/settings/"))
|
|
|
- output = ProcessSettingsQuery(uri);
|
|
|
- else
|
|
|
- output = "{\"result\": \"error\"}\r\n";
|
|
|
+ if (StringAlgorithm::istarts_with(apiURI, "/log/"))
|
|
|
+ return ProcessLogQuery(pDataStorageClient, apiURI);
|
|
|
+ else if (StringAlgorithm::istarts_with(apiURI, "/graphheader/"))
|
|
|
+ return ProcessGraphHeaderQuery(pGraphClient, apiURI);
|
|
|
+ else if (StringAlgorithm::istarts_with(apiURI, "/graph/"))
|
|
|
+ return ProcessGraphQuery(pGraphClient, apiURI);
|
|
|
+ else if (StringAlgorithm::istarts_with(apiURI, "/settings/"))
|
|
|
+ return ProcessSettingsQuery(apiURI);
|
|
|
|
|
|
- return output;
|
|
|
+
|
|
|
+ Http::HttpServer::HttpReply reply;
|
|
|
+ reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
|
|
|
+ reply.content = "{\"result\": \"error\"}\r\n";
|
|
|
+ return reply;
|
|
|
}
|
|
|
|
|
|
-std::string WebAPI::ProcessLogQuery(DataStorage::DataStorageClient* pDataStorageClient, const std::string& uri)
|
|
|
+Http::HttpServer::HttpReply WebAPI::ProcessLogQuery(DataStorage::DataStorageClient* pDataStorageClient, const std::string& uri)
|
|
|
{
|
|
|
// /log/<timestamp>/<device_id>/<data_id>/<value>
|
|
|
|
|
|
- std::string output;
|
|
|
- std::string error = "{\"result\": \"error\"}\r\n";
|
|
|
+ Http::HttpServer::HttpReply reply;
|
|
|
+ reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
|
|
|
+ reply.content = "{\"result\": \"error\"}\r\n";
|
|
|
|
|
|
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
|
|
if (uriTokens.size() < 6)
|
|
|
- return error;
|
|
|
+ return reply;
|
|
|
|
|
|
if (uriTokens[2].empty() ||
|
|
|
uriTokens[3].empty() ||
|
|
|
uriTokens[4].empty())
|
|
|
- return error;
|
|
|
+ return reply;
|
|
|
|
|
|
int timestamp = std::stoi(uriTokens[2]);
|
|
|
int deviceId = std::stoi(uriTokens[3]);
|
|
|
@@ -95,26 +105,28 @@ std::string WebAPI::ProcessLogQuery(DataStorage::DataStorageClient* pDataStorage
|
|
|
|
|
|
// TODO: Actually Log...
|
|
|
|
|
|
- output = "{\"result\": \"success\"}\r\n";
|
|
|
+ reply.status = Http::HttpServer::HttpReply::Status::Ok;
|
|
|
+ reply.content = "{\"result\": \"success\"}\r\n";
|
|
|
|
|
|
- return output;
|
|
|
+ return reply;
|
|
|
}
|
|
|
|
|
|
-std::string WebAPI::ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri)
|
|
|
+Http::HttpServer::HttpReply WebAPI::ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri)
|
|
|
{
|
|
|
// /graphheader/<device_id>/<data_ids>/<timespan>
|
|
|
|
|
|
- std::string output;
|
|
|
- std::string error = "{\"result\": \"error\"}\r\n";
|
|
|
+ Http::HttpServer::HttpReply reply;
|
|
|
+ reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
|
|
|
+ reply.content = "{\"result\": \"error\"}\r\n";
|
|
|
|
|
|
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
|
|
|
|
|
if (uriTokens.size() != 5)
|
|
|
- return error;
|
|
|
+ return reply;
|
|
|
|
|
|
if (uriTokens[2].empty() ||
|
|
|
uriTokens[3].empty())
|
|
|
- return error;
|
|
|
+ return reply;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
@@ -129,32 +141,35 @@ std::string WebAPI::ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphCl
|
|
|
period = "day";
|
|
|
|
|
|
nlohmann::json json = GetGraphHeader(pGraphClient, dataIds, period);
|
|
|
- output = json.dump();
|
|
|
+
|
|
|
+ reply.content = json.dump();
|
|
|
+ reply.status = Http::HttpServer::HttpReply::Status::Ok;
|
|
|
}
|
|
|
catch (const std::exception& e)
|
|
|
{
|
|
|
std::cout << "ProcessGraphHeaderQuery() - Error: " << e.what() << std::endl;
|
|
|
- return error;
|
|
|
+ return reply;
|
|
|
}
|
|
|
|
|
|
- return output;
|
|
|
+ return reply;
|
|
|
}
|
|
|
|
|
|
-std::string WebAPI::ProcessGraphQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri)
|
|
|
+Http::HttpServer::HttpReply WebAPI::ProcessGraphQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri)
|
|
|
{
|
|
|
// /graph/<device_id>/<data_ids>/<timespan>
|
|
|
|
|
|
- std::string output;
|
|
|
- std::string error = "{\"result\": \"error\"}\r\n";
|
|
|
+ Http::HttpServer::HttpReply reply;
|
|
|
+ reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
|
|
|
+ reply.content = "{\"result\": \"error\"}\r\n";
|
|
|
|
|
|
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
|
|
|
|
|
if (uriTokens.size() != 5)
|
|
|
- return error;
|
|
|
+ return reply;
|
|
|
|
|
|
if (uriTokens[2].empty() ||
|
|
|
uriTokens[3].empty())
|
|
|
- return error;
|
|
|
+ return reply;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
@@ -169,28 +184,31 @@ std::string WebAPI::ProcessGraphQuery(DataInterface::GraphClient* pGraphClient,
|
|
|
period = "day";
|
|
|
|
|
|
nlohmann::json json = GetGraphData(pGraphClient, deviceId, dataIds, period);
|
|
|
- output = json.dump();
|
|
|
+
|
|
|
+ reply.content = json.dump();
|
|
|
+ reply.status = Http::HttpServer::HttpReply::Status::Ok;
|
|
|
}
|
|
|
catch (const std::exception& e)
|
|
|
{
|
|
|
std::cout << "ProcessGraphQuery() - Error: " << e.what() << std::endl;
|
|
|
- return error;
|
|
|
+ return reply;
|
|
|
}
|
|
|
|
|
|
- return output;
|
|
|
+ return reply;
|
|
|
}
|
|
|
|
|
|
-std::string WebAPI::ProcessSettingsQuery(const std::string& uri)
|
|
|
+Http::HttpServer::HttpReply WebAPI::ProcessSettingsQuery(const std::string& uri)
|
|
|
{
|
|
|
// /settings/<setting>/<value>
|
|
|
|
|
|
- std::string success = "{\"result\": \"success\"}\r\n";
|
|
|
- std::string error = "{\"result\": \"error\"}\r\n";
|
|
|
+ Http::HttpServer::HttpReply reply;
|
|
|
+ reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
|
|
|
+ reply.content = "{\"result\": \"error\"}\r\n";
|
|
|
|
|
|
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
|
|
|
|
|
if (uriTokens.size() < 4)
|
|
|
- return std::string();
|
|
|
+ return reply;
|
|
|
|
|
|
std::string setting = uriTokens[2];
|
|
|
std::string value = uriTokens[3];
|
|
|
@@ -203,14 +221,17 @@ std::string WebAPI::ProcessSettingsQuery(const std::string& uri)
|
|
|
else if (StringAlgorithm::iequals(value, "off"))
|
|
|
Logging::SetLogMask(Logging::Severity::Info);
|
|
|
else
|
|
|
- return error;
|
|
|
+ return reply;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- return error;
|
|
|
+ return reply;
|
|
|
}
|
|
|
|
|
|
- return success;
|
|
|
+ reply.status = Http::HttpServer::HttpReply::Status::Ok;
|
|
|
+ reply.content = "{\"result\": \"success\"}\r\n";
|
|
|
+
|
|
|
+ return reply;
|
|
|
}
|
|
|
|
|
|
nlohmann::json WebAPI::GetGraphHeader(DataInterface::GraphClient* pGraphClient, const std::vector<int>& dataIds, const std::string& timespan)
|