code reformat

This commit is contained in:
2019-05-08 19:31:49 +02:00
parent 31819abcaf
commit c738a01ce4
9 changed files with 35 additions and 28 deletions

View File

@ -12,13 +12,14 @@
std::string API::request(std::string myurl) {
Hashmap<std::string,std::string> map;
Hashmap<std::string, std::string> map;
std::vector<std::string> str;
return request(std::move(myurl),false,map,str);
return request(std::move(myurl), false, map, str);
}
std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::string> &map, std::vector<std::string> &headers) {
std::string
API::request(std::string myurl, bool post, Hashmap<std::string, std::string> &map, std::vector<std::string> &headers) {
CURL *curl;
CURLcode res;
@ -40,12 +41,12 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
if(post){
if (post) {
std::stringstream poststring;
poststring << "{";
poststring << "{";
for (int i = 0; i < map.size(); i++) {
poststring << "\""+map.getKey(i) << "\": \""+map.getValue(i) << "\"";
if(i < map.size()-1){
poststring << "\"" + map.getKey(i) << "\": \"" + map.getValue(i) << "\"";
if (i < map.size() - 1) {
poststring << ", ";
}
}
@ -53,20 +54,19 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, poststring.str().c_str());
}else{
} else {
std::string getstring;
for(int i =0; i< map.size();i++){
getstring+=map.getKey(i)+"="+map.getValue(i);
if(i < map.size()-1){
getstring+="&";
for (int i = 0; i < map.size(); i++) {
getstring += map.getKey(i) + "=" + map.getValue(i);
if (i < map.size() - 1) {
getstring += "&";
}
}
myurl+="?"+getstring;
myurl += "?" + getstring;
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readString);
@ -78,6 +78,6 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
}
size_t API::write_data(void *contents, size_t size, size_t nmemb, FILE *stream) {
((std::string*)stream)->append((char*)contents, size * nmemb);
((std::string *) stream)->append((char *) contents, size * nmemb);
return size * nmemb;
}

View File

@ -12,7 +12,8 @@
class API {
public:
std::string request(std::string myurl);
std::string request(std::string myurl, bool post, Hashmap<std::string,std::string> &map,std::vector<std::string> &headers);
std::string request(std::string myurl, bool post, Hashmap<std::string, std::string> &map, std::vector<std::string> &headers);
private:
static size_t write_data(void *buffer, size_t size, size_t buffersize, FILE *stream);

View File

@ -9,10 +9,13 @@
#include <iostream>
#include <vector>
template <class keytype,class valuetype> class Hashmap {
template<class keytype, class valuetype>
class Hashmap {
public:
void add(keytype key,keytype value);
void add(keytype key, keytype value);
keytype getKey(int position);
valuetype getValue(int position);
int size();

View File

@ -5,6 +5,6 @@
#include "TelegramAPI.h"
void TelegramAPI::sendMessage(std::string text) {
std::string reply = request("https://api.telegram.org/bot"+ apikey + "/sendmessage?chat_id="+chatid+"&text="+text);
std::cout << "[DEBUG] " <<reply << std::endl;
std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage?chat_id=" + chatid + "&text=" + text);
std::cout << "[DEBUG] " << reply << std::endl;
}

View File

@ -9,7 +9,7 @@
#include <string>
#include "API.h"
class TelegramAPI : API{
class TelegramAPI : API {
public:
void sendMessage(std::string text);