cleanups and clarity

This commit is contained in:
DarthArgus
2016-08-06 02:35:55 +00:00
parent 00fef810e7
commit 207449547f
4 changed files with 37 additions and 23 deletions
@@ -2872,7 +2872,7 @@ void CentralServer::sendMetricsToWebAPI(std::string updateURL)
postBuf << "totalPlayerCount=" << m_totalPlayerCount << "&totalGameServers=" << m_gameServers.size() - 1 << "&totalPlanetServers=" << m_planetServers.size() << "&isPublic=" << getIsClusterPublic() << "&isLocked=" << getIsClusterLocked() << "&isSecret=" << getIsClusterSecret() << "&preloadFinished=" << getClusterStartupTime() << "&databasebacklogged=" << isDatabaseBacklogged() << "&totalTutorialSceneCount=" << m_totalTutorialSceneCount << "&totalFalconSceneCount=" << m_totalFalconSceneCount;
webAPI::statusMessage response = webAPI::simplePost(updateURL, std::string(postBuf.str()));
WARNING(response.status, ("Error sending stats: %s", response.message.c_str()));
WARNING(response.status, ("Error sending stats: %s", response.retVal.c_str()));
}
//-----------------------------------------------------------------------
@@ -172,33 +172,33 @@ void ClientConnection::onReceive(const Archive::ByteStream & message)
// originally was used to validate station API credentials, now uses our custom api
void ClientConnection::validateClient(const std::string & id, const std::string & key)
{
int authOK = 0;
StationId suid = atoi(id.c_str());
std::string uname;
bool authOK = false;
StationId suid = atoi(id.c_str());
std::string authURL(ConfigLoginServer::getExternalAuthUrl());
std::string uname;
if (!authURL.empty())
{
std::ostringstream postBuf;
postBuf << "user_name=" << id << "&user_password=" << key << "&ip=" << getRemoteAddress();
const webAPI::statusMessage response = webAPI::simplePost(authURL, std::string(postBuf.str()), "username");
if (response.status && !response.message.empty())
if (response.status && !response.retVal.empty())
{
authOK = 1;
uname = response.message;
authOK = true;
uname = response.retVal;
}
else
{
ErrorMessage err("Login Failed", response.message);
ErrorMessage err("Login Failed", response.retVal);
this->send(err, true);
}
}
else
{
// test mode
authOK = 1;
authOK = true;
uname = id;
}
+23 -9
View File
@@ -27,7 +27,7 @@ statusVal: the expected value in the case of success
webAPI::statusMessage webAPI::simplePost(const string &endpoint, const string &data, const string &slotName, const string &messageSlot, const string &statusSlot, const string &statusVal)
{
// declare our output and go ahead and attempt to get data from remote
nlohmann::json response = request(endpoint, data, 1);
nlohmann::json response = request(endpoint, data);
// if we got data back...
if (response.count(statusSlot) && response[statusSlot].get<std::string>() == statusVal && response.count(slotName))
@@ -61,29 +61,43 @@ nlohmann::json webAPI::request(const string &endpoint, const string &data, const
string readBuffer; // container for the remote response
long http_code = 0; // we get this after performing the get or post
curl_easy_setopt(curl, CURLOPT_URL, endpoint.c_str()); // endpoint is always specified by caller
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); // place the data into readBuffer using writeCallback
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); // specify readBuffer as the container for data
if (reqType == 1)
switch (reqType)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
} //todo - get request? etc
case 1:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_URL, endpoint.c_str());
break;
case 2:
std::string getURI = endpoint + "?" + data;
curl_easy_setopt(curl, CURLOPT_URL, getURI.c_str());
break;
// want to do a put, or whatever other type? feel free to add here
}
CURLcode res = curl_easy_perform(curl); // make the request!
char *contentType;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); //get status code
curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &contentType);
if (res == CURLE_OK && http_code == 200 && !(readBuffer.empty())) // check it all out and parse
{
try {
// if we ever use anything other than json we can break the stuff below output
// into a separate function...
try
{
response = nlohmann::json::parse(readBuffer);
}
catch (string &e) {
catch (string &e)
{
response["message"] = e;
response["status"] = "failure";
}
catch (...) {
catch (...)
{
response["message"] = "JSON parse error for endpoint.";
response["status"] = "failure";
}
+2 -2
View File
@@ -30,13 +30,13 @@ namespace webAPI
struct statusMessage
{
bool status;
std::string message;
std::string retVal;
};
using namespace std;
statusMessage simplePost(const string &endpoint, const string &data, const string &slotName = "success", const string &messageSlot ="message", const string &statusSlot = "status", const string &statusVal = "success");
nlohmann::json request(const string &endpoint, const string &data, const int &reqType); // 1 for post, 0 for get
nlohmann::json request(const string &endpoint, const string &data, const int &reqType = 1); // 1 for post, 0 for get
size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp);
};