Requests: Merge global and profile persistent data requests

The code was largely shared, so it made more sense to just merge the
requests and create realms.
This commit is contained in:
tt2468 2021-08-26 23:06:23 -07:00
parent 8185580cff
commit 7c5c0f42da
3 changed files with 60 additions and 48 deletions

View File

@ -14,14 +14,14 @@ const std::map<std::string, RequestMethodHandler> RequestHandler::_handlerMap
{"Sleep", &RequestHandler::Sleep},
// Config
{"GetPersistentData", &RequestHandler::GetPersistentData},
{"SetPersistentData", &RequestHandler::SetPersistentData},
{"GetSceneCollectionList", &RequestHandler::GetSceneCollectionList},
{"SetCurrentSceneCollection", &RequestHandler::SetCurrentSceneCollection},
{"GetProfileList", &RequestHandler::GetProfileList},
{"SetCurrentProfile", &RequestHandler::SetCurrentProfile},
{"GetProfileParameter", &RequestHandler::GetProfileParameter},
{"SetProfileParameter", &RequestHandler::SetProfileParameter},
{"GetProfilePersistentData", &RequestHandler::GetProfilePersistentData},
{"SetProfilePersistentData", &RequestHandler::SetProfilePersistentData},
{"GetVideoSettings", &RequestHandler::GetVideoSettings},
{"SetVideoSettings", &RequestHandler::SetVideoSettings},
{"GetStreamServiceSettings", &RequestHandler::GetStreamServiceSettings},

View File

@ -29,14 +29,14 @@ class RequestHandler {
RequestResult Sleep(const Request&);
// Config
RequestResult GetPersistentData(const Request&);
RequestResult SetPersistentData(const Request&);
RequestResult GetSceneCollectionList(const Request&);
RequestResult SetCurrentSceneCollection(const Request&);
RequestResult GetProfileList(const Request&);
RequestResult SetCurrentProfile(const Request&);
RequestResult GetProfileParameter(const Request&);
RequestResult SetProfileParameter(const Request&);
RequestResult GetProfilePersistentData(const Request&);
RequestResult SetProfilePersistentData(const Request&);
RequestResult GetVideoSettings(const Request&);
RequestResult SetVideoSettings(const Request&);
RequestResult GetStreamServiceSettings(const Request&);

View File

@ -3,6 +3,62 @@
#include "RequestHandler.h"
#include "../plugin-macros.generated.h"
RequestResult RequestHandler::GetPersistentData(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!(request.ValidateString("realm", statusCode, comment) && request.ValidateString("slotName", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
std::string realm = request.RequestData["realm"];
std::string slotName = request.RequestData["slotName"];
std::string persistentDataPath = Utils::Obs::StringHelper::GetCurrentProfilePath();
if (realm == "OBS_WEBSOCKET_DATA_REALM_GLOBAL")
persistentDataPath += "../../../obsWebSocketPersistentData.json";
else if (realm == "OBS_WEBSOCKET_DATA_REALM_PROFILE")
persistentDataPath += "/obsWebSocketPersistentData.json";
else
return RequestResult::Error(RequestStatus::DataRealmNotFound, "You have specified an invalid persistent data realm.");
json responseData;
json persistentData;
if (!(Utils::Json::GetJsonFileContent(persistentDataPath, persistentData) && persistentData.contains(slotName)))
responseData["slotData"] = nullptr;
else
responseData["slotData"] = persistentData[slotName];
return RequestResult::Success(responseData);
}
RequestResult RequestHandler::SetPersistentData(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!(request.ValidateString("realm", statusCode, comment) && request.ValidateString("slotName", statusCode, comment) && request.ValidateBasic("slotName", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
std::string realm = request.RequestData["realm"];
std::string slotName = request.RequestData["slotName"];
json slotData = request.RequestData["slotData"];
std::string persistentDataPath = Utils::Obs::StringHelper::GetCurrentProfilePath();
if (realm == "OBS_WEBSOCKET_DATA_REALM_GLOBAL")
persistentDataPath += "../../../obsWebSocketPersistentData.json";
else if (realm == "OBS_WEBSOCKET_DATA_REALM_PROFILE")
persistentDataPath += "/obsWebSocketPersistentData.json";
else
return RequestResult::Error(RequestStatus::DataRealmNotFound, "You have specified an invalid persistent data realm.");
json persistentData = json::object();
Utils::Json::GetJsonFileContent(persistentDataPath, persistentData);
persistentData[slotName] = slotData;
if (!Utils::Json::SetJsonFileContent(persistentDataPath, persistentData))
return RequestResult::Error(RequestStatus::RequestProcessingFailed, "Unable to write persistent data. No permissions?");
return RequestResult::Success();
}
RequestResult RequestHandler::GetSceneCollectionList(const Request& request)
{
json responseData;
@ -124,50 +180,6 @@ RequestResult RequestHandler::SetProfileParameter(const Request& request)
return RequestResult::Success();
}
RequestResult RequestHandler::GetProfilePersistentData(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!request.ValidateString("slotName", statusCode, comment))
return RequestResult::Error(statusCode, comment);
std::string slotName = request.RequestData["slotName"];
std::string persistentDataPath = Utils::Obs::StringHelper::GetCurrentProfilePath();
persistentDataPath += "/obsWebSocketPersistentData.json";
json responseData;
json persistentData;
if (!(Utils::Json::GetJsonFileContent(persistentDataPath, persistentData) && persistentData.contains(slotName)))
responseData["slotData"] = nullptr;
else
responseData["slotData"] = persistentData[slotName];
return RequestResult::Success(responseData);
}
RequestResult RequestHandler::SetProfilePersistentData(const Request& request)
{
RequestStatus::RequestStatus statusCode;
std::string comment;
if (!(request.ValidateString("slotName", statusCode, comment) && request.ValidateBasic("slotName", statusCode, comment)))
return RequestResult::Error(statusCode, comment);
std::string slotName = request.RequestData["slotName"];
json slotData = request.RequestData["slotData"];
std::string persistentDataPath = Utils::Obs::StringHelper::GetCurrentProfilePath();
persistentDataPath += "/obsWebSocketPersistentData.json";
json persistentData = json::object();
Utils::Json::GetJsonFileContent(persistentDataPath, persistentData);
persistentData[slotName] = slotData;
if (!Utils::Json::SetJsonFileContent(persistentDataPath, persistentData))
return RequestResult::Error(RequestStatus::RequestProcessingFailed, "Unable to write persistent data. No permissions?");
return RequestResult::Success();
}
RequestResult RequestHandler::GetVideoSettings(const Request& request)
{
struct obs_video_info ovi;