Better compliance with API reference

Send User-Agent header, move token auth to Authorization header.
https://discordapp.com/developers/docs/reference
This commit is contained in:
Jack Bond-Preston 2016-07-20 22:57:47 +01:00
parent 1f6d1a774d
commit db3ab59daf
3 changed files with 13 additions and 8 deletions

View File

@ -4,15 +4,13 @@
#include "APIHelper.hpp" #include "APIHelper.hpp"
extern std::string bot_token;
APIHelper::APIHelper() : BASE_URL("https://discordapp.com/api"), CHANNELS_URL(BASE_URL + "/channels"), APIHelper::APIHelper() : BASE_URL("https://discordapp.com/api"), CHANNELS_URL(BASE_URL + "/channels"),
TOKEN_PARAM("token=" + bot_token), JSON_CTYPE("application/json") { JSON_CTYPE("application/json") {
http = new HTTPHelper(); http = new HTTPHelper();
} }
void APIHelper::send_message(std::string channel_id, std::string message) { void APIHelper::send_message(std::string channel_id, std::string message) {
const std::string url = CHANNELS_URL + "/" + channel_id + "/messages?" + TOKEN_PARAM; const std::string url = CHANNELS_URL + "/" + channel_id + "/messages";
json data = { json data = {
{ "content", message } { "content", message }
}; };

View File

@ -18,8 +18,6 @@ public:
private: private:
const std::string BASE_URL; const std::string BASE_URL;
const std::string CHANNELS_URL; const std::string CHANNELS_URL;
const std::string TOKEN;
const std::string TOKEN_PARAM;
const std::string JSON_CTYPE; const std::string JSON_CTYPE;
HTTPHelper *http; HTTPHelper *http;

View File

@ -1,5 +1,7 @@
#include "HTTPHelper.hpp" #include "HTTPHelper.hpp"
extern std::string bot_token;
/* /*
* Warning: (Awful) C Code * Warning: (Awful) C Code
*/ */
@ -17,8 +19,15 @@ std::string HTTPHelper::post_request(std::string url, std::string content_type,
// Now with real HTTPS! // Now with real HTTPS!
curl_easy_setopt(curl, CURLOPT_CAINFO, "bot/http/DiscordCA.crt"); curl_easy_setopt(curl, CURLOPT_CAINFO, "bot/http/DiscordCA.crt");
std::string content_header = "Content-Type: " + content_type; std::string header_arr[3];
headers = curl_slist_append(headers, content_header.c_str()); header_arr[0] = "Content-Type: " + content_type;
header_arr[1] = "Authorization: Bot " + bot_token;
header_arr[2] = "User-Agent: DiscordBot(http://github.com/jackb-p/triviadiscord, 1.0)";
for (std::string h : header_arr) {
headers = curl_slist_append(headers, h.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());