Update Http Library
This commit is contained in:
+70
-49
@@ -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;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "API Query: " << uri << 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";
|
||||
|
||||
return 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 WebAPI::ProcessLogQuery(DataStorage::DataStorageClient* pDataStorageClient, const std::string& uri)
|
||||
std::string apiURI = uri.substr(4);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "API Query: " << apiURI << std::endl;
|
||||
Logging::Log(Logging::Severity::Debug, ss.str());
|
||||
|
||||
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);
|
||||
|
||||
|
||||
Http::HttpServer::HttpReply reply;
|
||||
reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
|
||||
reply.content = "{\"result\": \"error\"}\r\n";
|
||||
return reply;
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
+6
-5
@@ -2,6 +2,7 @@
|
||||
#define API_WEBAPI_H
|
||||
|
||||
#include <HttpPostData.h>
|
||||
#include <HttpServer.h>
|
||||
#include <json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -29,15 +30,15 @@ public:
|
||||
WebAPI();
|
||||
~WebAPI();
|
||||
|
||||
static std::string ProcessQuery(DataStorage::DataStorageClient* pDataStorageClient, DataInterface::GraphClient* pGraphClient, const std::string& uri, const std::vector<Http::HttpPostData>& postData);
|
||||
static Http::HttpServer::HttpReply ProcessQuery(DataStorage::DataStorageClient* pDataStorageClient, DataInterface::GraphClient* pGraphClient, const std::string& uri, const std::vector<Http::HttpPostData>& postData);
|
||||
|
||||
private:
|
||||
static std::string ProcessLogQuery(DataStorage::DataStorageClient* pDataStorageClient, const std::string& uri);
|
||||
static Http::HttpServer::HttpReply ProcessLogQuery(DataStorage::DataStorageClient* pDataStorageClient, const std::string& uri);
|
||||
|
||||
static std::string ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri);
|
||||
static std::string ProcessGraphQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri);
|
||||
static Http::HttpServer::HttpReply ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri);
|
||||
static Http::HttpServer::HttpReply ProcessGraphQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri);
|
||||
|
||||
static std::string ProcessSettingsQuery(const std::string& uri);
|
||||
static Http::HttpServer::HttpReply ProcessSettingsQuery(const std::string& uri);
|
||||
|
||||
static nlohmann::json GetGraphHeader(DataInterface::GraphClient* pGraphClient, const std::vector<int>& dataIds, const std::string& timespan);
|
||||
static nlohmann::json GetGraphData(DataInterface::GraphClient* pGraphClient, int deviceId, const std::vector<int>& dataIds, const std::string& timespan);
|
||||
|
||||
@@ -52,7 +52,7 @@ int main(int argc, char** argv)
|
||||
|
||||
DataStorage::DataStorageClient dataStorageClient(pMySQLClient, table);
|
||||
DataStorageInterface::DataInterface::GraphClient graphClient(pMySQLClient.get());
|
||||
auto callback = std::bind(&DataStorageInterface::API::WebAPI::ProcessQuery, &dataStorageClient, &graphClient, std::placeholders::_1, std::placeholders::_2);
|
||||
Http::HttpServer::CallbackMethod callback = std::bind(&DataStorageInterface::API::WebAPI::ProcessQuery, &dataStorageClient, &graphClient, std::placeholders::_1, std::placeholders::_2);
|
||||
|
||||
Http::HttpServer server(port, callback);
|
||||
Logging::Log(Logging::Severity::Info, "Startup Complete");
|
||||
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../Libraries/Timer
|
||||
@@ -14,6 +14,10 @@ LFLAGS += -lDataStorage
|
||||
LFLAGS += -L$(ROOTPATH)/Libraries/DataStorage/lib/$(ARCH)
|
||||
CFLAGS += -I$(ROOTPATH)/Libraries/DataStorage/include
|
||||
|
||||
LFLAGS += -lTimer
|
||||
LFLAGS += -L$(ROOTPATH)/Libraries/Timer/lib/$(ARCH)
|
||||
CFLAGS += -I$(ROOTPATH)/Libraries/Timer/include
|
||||
|
||||
LFLAGS += -lHttp -lcrypto -lcurl -lssl -lz
|
||||
LFLAGS += -L$(ROOTPATH)/Libraries/Http/lib/$(ARCH)
|
||||
CFLAGS += -I$(ROOTPATH)/Libraries/Http/include
|
||||
|
||||
Reference in New Issue
Block a user