Created
July 11, 2012 11:55
-
-
Save joernroeder/3089922 to your computer and use it in GitHub Desktop.
RequestHandler.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* RequestHandler.cpp | |
* appConnector | |
* | |
* Created by Jonathan Pirnay & Jörn Röder. | |
* Copyright 2012 jj. All rights reserved. | |
* | |
*/ | |
#include "RequestHandler.h" | |
void RequestHandler::init(string appKey) { | |
this->appKey = appKey; | |
protocol = "http://"; | |
host = "appconnector.joernroeder.de"; | |
port = 80; | |
postUrl = "/app/publish"; | |
descUrl = "/app/desc"; | |
getUrl = "/app/sync"; | |
setupUrl = "/app/setup"; | |
httpUtils.start(); | |
} | |
void RequestHandler::exit() { | |
httpUtils.stop(); | |
} | |
/** | |
* returns setup stuff. at the moment starttime and interval | |
*/ | |
json_t* RequestHandler::setup() { | |
json_t *obj = json_loads(getRequest(setupUrl).c_str(), JSON_DECODE_ANY, NULL); | |
return obj; | |
} | |
void RequestHandler::setDebugResponse(bool val) { | |
_debugResponse = val; | |
} | |
bool RequestHandler::getDebugResponse() { | |
return _debugResponse; | |
} | |
/** | |
* pushes values to the server | |
*/ | |
void RequestHandler::post(json_t* values) { | |
values = addAppKey(values); | |
postRequest(values, postUrl); | |
} | |
/** | |
* get synced variables | |
*/ | |
string RequestHandler::get(json_t* obj) { | |
return postRequest(obj, getUrl); | |
} | |
/** | |
* returns the current interval from the server | |
*/ | |
int RequestHandler::getInterval() { | |
return ofToInt(getRequest(setupUrl + "/interval")); | |
} | |
void RequestHandler::setDescription(json_t* desc) { | |
desc = addAppKey(desc); | |
postRequest(desc, descUrl); | |
} | |
/* | |
** internal methods | |
*/ | |
string RequestHandler::getRequest(string url) { | |
return httpUtils.getUrl(protocol + host + url).responseBody.getText(); | |
} | |
string RequestHandler::postRequest(json_t* obj, string url) { | |
ofxHttpForm form; | |
const char* appName; | |
json_t* appList; | |
form.action = protocol + host + url; | |
form.method = OFX_HTTP_POST; | |
json_object_foreach(obj, appName, appList) { | |
const char* name; | |
json_t* value; | |
const char* val; | |
bool freeIt = false; | |
if (json_is_string(appList)) { | |
val = json_string_value(appList); | |
} | |
else { | |
val = json_dumps(appList, JSON_ENCODE_ANY); | |
freeIt = true; | |
} | |
form.addFormField(appName, val); | |
if (freeIt) { free((void*)val); } | |
} | |
json_decref(obj); | |
ofxHttpResponse response = httpUtils.submitForm(form); | |
return response.responseBody.getText(); | |
} | |
json_t* RequestHandler::addAppKey(json_t* values) { | |
json_t* key = json_string(appKey.c_str()); | |
json_object_set(values, "appKey", key); | |
json_decref(key); | |
return values; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment