1 #ifndef YATBCPP_TELEGRAM_METHODJSON_H 2 #define YATBCPP_TELEGRAM_METHODJSON_H 5 #include <jsoncpp/json/json.h> 6 #include "../exceptions/telegram_api_error.h" 7 #include "../exceptions/curl_error.h" 8 #include "../types/telegram_type.h" 9 #include "../exceptions/essential_key_missing.h" 10 #include "../bot/Token.h" 13 template <
typename T> Json::Value
toJson(T Obj);
27 static size_t WriteCallback(
void *contents,
size_t size,
size_t nmemb,
void *stringptr){
29 ((
string*)stringptr)->append((
char*)contents, size * nmemb);
37 string curl_payload( method_body.
toJson().toStyledString() );
42 struct curl_slist * curl_header_list =NULL;
43 curl_header_list = curl_slist_append( curl_header_list ,
"Content-Type: application/json" );
44 CURL* curl = curl_easy_init();
46 curl_easy_setopt(curl,CURLOPT_URL,api_url.c_str());
47 curl_easy_setopt(curl,CURLOPT_POSTFIELDS,curl_payload.c_str());
48 curl_easy_setopt(curl,CURLOPT_HTTPHEADER,curl_header_list);
50 curl_easy_setopt(curl,CURLOPT_WRITEDATA,&readBuffer);
52 CURLcode res = curl_easy_perform(curl);
53 reader.parse(readBuffer,Response);
57 if(Response[
"ok"].asBool()){
58 curl_slist_free_all(curl_header_list);
59 curl_easy_cleanup(curl);
60 return yatbcpp::fromJson<RETURNTYPE>(Response[
"result"]);
63 curl_easy_cleanup(curl);
64 curl_slist_free_all(curl_header_list);
65 throw telegram_api_error(Response[
"error_code"].asInt(),Response[
"description"].asString());
91 #endif //YATBCPP_TELEGRAM_METHODJSON_H
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *stringptr)
const std::string & getFunctionname() const
telegram_methodJSON(std::string functionname)
static RETURNTYPE perform_requestJSON(Token T, telegram_methodJSON< RETURNTYPE > &method_body)
virtual Json::Value toJson()
Json::Value toJson(T Obj)
const std::string getToken() const