Skip to content

Instantly share code, notes, and snippets.

@sidoh
Created May 13, 2019 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidoh/75ca02d4e20b2f5a7ed35ffdc5db0a42 to your computer and use it in GitHub Desktop.
Save sidoh/75ca02d4e20b2f5a7ed35ffdc5db0a42 to your computer and use it in GitHub Desktop.
void HttpServer::handleUpdateSettings(RequestContext& request) {
JsonObject req = request.getJsonBody().as<JsonObject>();
if (req.isNull()) {
request.response.json["error"] = "Invalid JSON";
request.response.setCode(400);
return;
}
ConfigurationDictionary params;
for (JsonObject::iterator it = req.begin(); it != req.end(); ++it) {
params[it->key().c_str()] = it->value().as<String>();
}
settings.setFromDictionary(params);
Bleeper.storage.persist();
handleListSettings(request);
}
void HttpServer::handleListSettings(RequestContext& request) {
request.response.sendRaw(200, APPLICATION_JSON, getSettingsBody().c_str());
}
String HttpServer::getSettingsBody() {
DynamicJsonDocument json(2048);
ConfigurationDictionary params = settings.getAsDictionary(true);
for (std::map<String, String>::const_iterator it = params.begin(); it != params.end(); ++it) {
json[it->first] = it->second;
}
String strJson;
serializeJson(json, strJson);
return strJson;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment