mirror of
https://bitbucket.org/seefoe/src.git
synced 2026-08-02 03:15:57 -04:00
has linker error! but this should be a good start on breaking out our changes and web api into a standalone library/namespace
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
set(SHARED_SOURCES
|
||||
webAPI.cpp
|
||||
webAPI.h
|
||||
json.hpp
|
||||
)
|
||||
|
||||
include_directories(
|
||||
${CURL_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
#target_link_libraries(
|
||||
# ${CURL_LIBRARIES}
|
||||
#)
|
||||
Vendored
+8897
File diff suppressed because it is too large
Load Diff
+86
@@ -0,0 +1,86 @@
|
||||
|
||||
#include "webAPI.h"
|
||||
|
||||
// if status == success, returns "success", or slotName's contents if specified...
|
||||
// otherwise returns the "message" if no success
|
||||
std::string simplePost(std::string endpoint, std::string data, std::string slotName)
|
||||
{
|
||||
nlohmann::json response = request(endpoint, data, 1);
|
||||
std::string output;
|
||||
|
||||
if (response.count("status") && response["status"].get<std::string>() == "success")
|
||||
{
|
||||
if (slotName && response.count(slotName))
|
||||
{
|
||||
output = response[slotName].get<std::string>();
|
||||
}
|
||||
else
|
||||
{
|
||||
output = "success";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (j.count("message"))
|
||||
{
|
||||
output = j["message"].get<std::string>();
|
||||
}
|
||||
else
|
||||
{
|
||||
output = "Message not provided by authentication service.";
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
nlohmann::json request(std::string endpoint, std::string data, int reqType, ...)
|
||||
{
|
||||
nlohmann::json response;
|
||||
|
||||
if (!endpoint.empty()) //data is allowed to be an empty string if we're doing a normal GET
|
||||
{
|
||||
CURL *curl = curl_easy_init();
|
||||
|
||||
if (curl)
|
||||
{
|
||||
std::string readBuffer;
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, endpoint.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
||||
|
||||
if (reqType == 1)
|
||||
{
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
|
||||
}
|
||||
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
|
||||
if (res == CURLE_OK && !(readBuffer.empty()))
|
||||
{
|
||||
response = json::parse(readBuffer);
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
else
|
||||
{
|
||||
response["message"] = "Failed to initialize cURL.";
|
||||
response["status"] = "failure";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
response["message"] = "Invalid endpoint URL.";
|
||||
response["status"] = "failure";
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
// This is used by curl to grab the response and put it into a var
|
||||
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
||||
{
|
||||
((std::string*)userp)->append((char*)contents, size * nmemb);
|
||||
return size * nmemb;
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
#ifndef webAPI_H
|
||||
#define webAPI_H
|
||||
|
||||
#include "json.hpp"
|
||||
#include <curl/curl.h>
|
||||
|
||||
namespace webAPI
|
||||
{
|
||||
std::string simplePost(std::string endpoint, std::string data, std::string slotName);
|
||||
//std::string simpleGet(char* endpoint, char* data);
|
||||
//nlohmann::json post(char* endpoint, char* data);
|
||||
//nlohmann::json get(char* endpoint, char* data);
|
||||
|
||||
nlohmann::json request(std::string endpoint, std::string data, int reqType); // 1 for post, 0 for get
|
||||
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user