Update Http Library
This commit is contained in:
+65
-44
@@ -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;
|
std::stringstream ss;
|
||||||
ss << "API Query: " << uri << std::endl;
|
ss << "API Query: " << apiURI << std::endl;
|
||||||
Logging::Log(Logging::Severity::Debug, ss.str());
|
Logging::Log(Logging::Severity::Debug, ss.str());
|
||||||
|
|
||||||
if (StringAlgorithm::istarts_with(uri, "/log/"))
|
if (StringAlgorithm::istarts_with(apiURI, "/log/"))
|
||||||
output = ProcessLogQuery(pDataStorageClient, uri);
|
return ProcessLogQuery(pDataStorageClient, apiURI);
|
||||||
else if (StringAlgorithm::istarts_with(uri, "/graphheader/"))
|
else if (StringAlgorithm::istarts_with(apiURI, "/graphheader/"))
|
||||||
output = ProcessGraphHeaderQuery(pGraphClient, uri);
|
return ProcessGraphHeaderQuery(pGraphClient, apiURI);
|
||||||
else if (StringAlgorithm::istarts_with(uri, "/graph/"))
|
else if (StringAlgorithm::istarts_with(apiURI, "/graph/"))
|
||||||
output = ProcessGraphQuery(pGraphClient, uri);
|
return ProcessGraphQuery(pGraphClient, apiURI);
|
||||||
else if (StringAlgorithm::istarts_with(uri, "/settings/"))
|
else if (StringAlgorithm::istarts_with(apiURI, "/settings/"))
|
||||||
output = ProcessSettingsQuery(uri);
|
return ProcessSettingsQuery(apiURI);
|
||||||
else
|
|
||||||
output = "{\"result\": \"error\"}\r\n";
|
|
||||||
|
|
||||||
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>
|
// /log/<timestamp>/<device_id>/<data_id>/<value>
|
||||||
|
|
||||||
std::string output;
|
Http::HttpServer::HttpReply reply;
|
||||||
std::string error = "{\"result\": \"error\"}\r\n";
|
reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
|
||||||
|
reply.content = "{\"result\": \"error\"}\r\n";
|
||||||
|
|
||||||
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
||||||
if (uriTokens.size() < 6)
|
if (uriTokens.size() < 6)
|
||||||
return error;
|
return reply;
|
||||||
|
|
||||||
if (uriTokens[2].empty() ||
|
if (uriTokens[2].empty() ||
|
||||||
uriTokens[3].empty() ||
|
uriTokens[3].empty() ||
|
||||||
uriTokens[4].empty())
|
uriTokens[4].empty())
|
||||||
return error;
|
return reply;
|
||||||
|
|
||||||
int timestamp = std::stoi(uriTokens[2]);
|
int timestamp = std::stoi(uriTokens[2]);
|
||||||
int deviceId = std::stoi(uriTokens[3]);
|
int deviceId = std::stoi(uriTokens[3]);
|
||||||
@@ -95,26 +105,28 @@ std::string WebAPI::ProcessLogQuery(DataStorage::DataStorageClient* pDataStorage
|
|||||||
|
|
||||||
// TODO: Actually Log...
|
// 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>
|
// /graphheader/<device_id>/<data_ids>/<timespan>
|
||||||
|
|
||||||
std::string output;
|
Http::HttpServer::HttpReply reply;
|
||||||
std::string error = "{\"result\": \"error\"}\r\n";
|
reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
|
||||||
|
reply.content = "{\"result\": \"error\"}\r\n";
|
||||||
|
|
||||||
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
||||||
|
|
||||||
if (uriTokens.size() != 5)
|
if (uriTokens.size() != 5)
|
||||||
return error;
|
return reply;
|
||||||
|
|
||||||
if (uriTokens[2].empty() ||
|
if (uriTokens[2].empty() ||
|
||||||
uriTokens[3].empty())
|
uriTokens[3].empty())
|
||||||
return error;
|
return reply;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -129,32 +141,35 @@ std::string WebAPI::ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphCl
|
|||||||
period = "day";
|
period = "day";
|
||||||
|
|
||||||
nlohmann::json json = GetGraphHeader(pGraphClient, dataIds, period);
|
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)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
std::cout << "ProcessGraphHeaderQuery() - Error: " << e.what() << std::endl;
|
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>
|
// /graph/<device_id>/<data_ids>/<timespan>
|
||||||
|
|
||||||
std::string output;
|
Http::HttpServer::HttpReply reply;
|
||||||
std::string error = "{\"result\": \"error\"}\r\n";
|
reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
|
||||||
|
reply.content = "{\"result\": \"error\"}\r\n";
|
||||||
|
|
||||||
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
||||||
|
|
||||||
if (uriTokens.size() != 5)
|
if (uriTokens.size() != 5)
|
||||||
return error;
|
return reply;
|
||||||
|
|
||||||
if (uriTokens[2].empty() ||
|
if (uriTokens[2].empty() ||
|
||||||
uriTokens[3].empty())
|
uriTokens[3].empty())
|
||||||
return error;
|
return reply;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -169,28 +184,31 @@ std::string WebAPI::ProcessGraphQuery(DataInterface::GraphClient* pGraphClient,
|
|||||||
period = "day";
|
period = "day";
|
||||||
|
|
||||||
nlohmann::json json = GetGraphData(pGraphClient, deviceId, dataIds, period);
|
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)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
std::cout << "ProcessGraphQuery() - Error: " << e.what() << std::endl;
|
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>
|
// /settings/<setting>/<value>
|
||||||
|
|
||||||
std::string success = "{\"result\": \"success\"}\r\n";
|
Http::HttpServer::HttpReply reply;
|
||||||
std::string error = "{\"result\": \"error\"}\r\n";
|
reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
|
||||||
|
reply.content = "{\"result\": \"error\"}\r\n";
|
||||||
|
|
||||||
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
|
||||||
|
|
||||||
if (uriTokens.size() < 4)
|
if (uriTokens.size() < 4)
|
||||||
return std::string();
|
return reply;
|
||||||
|
|
||||||
std::string setting = uriTokens[2];
|
std::string setting = uriTokens[2];
|
||||||
std::string value = uriTokens[3];
|
std::string value = uriTokens[3];
|
||||||
@@ -203,14 +221,17 @@ std::string WebAPI::ProcessSettingsQuery(const std::string& uri)
|
|||||||
else if (StringAlgorithm::iequals(value, "off"))
|
else if (StringAlgorithm::iequals(value, "off"))
|
||||||
Logging::SetLogMask(Logging::Severity::Info);
|
Logging::SetLogMask(Logging::Severity::Info);
|
||||||
else
|
else
|
||||||
return error;
|
return reply;
|
||||||
}
|
}
|
||||||
else
|
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)
|
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
|
#define API_WEBAPI_H
|
||||||
|
|
||||||
#include <HttpPostData.h>
|
#include <HttpPostData.h>
|
||||||
|
#include <HttpServer.h>
|
||||||
#include <json.hpp>
|
#include <json.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -29,15 +30,15 @@ public:
|
|||||||
WebAPI();
|
WebAPI();
|
||||||
~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:
|
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 Http::HttpServer::HttpReply ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri);
|
||||||
static std::string ProcessGraphQuery(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 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);
|
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);
|
DataStorage::DataStorageClient dataStorageClient(pMySQLClient, table);
|
||||||
DataStorageInterface::DataInterface::GraphClient graphClient(pMySQLClient.get());
|
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);
|
Http::HttpServer server(port, callback);
|
||||||
Logging::Log(Logging::Severity::Info, "Startup Complete");
|
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)
|
LFLAGS += -L$(ROOTPATH)/Libraries/DataStorage/lib/$(ARCH)
|
||||||
CFLAGS += -I$(ROOTPATH)/Libraries/DataStorage/include
|
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 += -lHttp -lcrypto -lcurl -lssl -lz
|
||||||
LFLAGS += -L$(ROOTPATH)/Libraries/Http/lib/$(ARCH)
|
LFLAGS += -L$(ROOTPATH)/Libraries/Http/lib/$(ARCH)
|
||||||
CFLAGS += -I$(ROOTPATH)/Libraries/Http/include
|
CFLAGS += -I$(ROOTPATH)/Libraries/Http/include
|
||||||
|
|||||||
Reference in New Issue
Block a user