edited api class and made it to work
This commit is contained in:
87
src/API.cpp
Normal file
87
src/API.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
//
|
||||
// Created by lukas on 06.04.19.
|
||||
//
|
||||
|
||||
#include "API.h"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
|
||||
#include "Hashmap.h"
|
||||
|
||||
std::string API::request(std::string myurl) {
|
||||
Hashmap<std::string,std::string> map;
|
||||
std::vector<std::string> str;
|
||||
|
||||
return request(myurl,false,map,str);
|
||||
}
|
||||
|
||||
std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::string> &map, std::vector<std::string> headers) {
|
||||
CURL *curl;
|
||||
CURLcode res;
|
||||
|
||||
|
||||
curl = curl_easy_init();
|
||||
|
||||
struct curl_slist *list = NULL;
|
||||
for (int j = 0; j < headers.size(); ++j) {
|
||||
list = curl_slist_append(list, headers.at(j).c_str());
|
||||
}
|
||||
|
||||
std::string readString;
|
||||
if (curl) {
|
||||
curl_easy_setopt(curl, CURLOPT_URL, myurl.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
|
||||
|
||||
|
||||
//ssl verification
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
|
||||
if(post){
|
||||
std::cout << "add post data\n";
|
||||
std::stringstream poststring;
|
||||
poststring << "{";
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
poststring << "\""+map.getKey(i) << "\": \""+map.getValue(i) << "\"";
|
||||
if(i < map.size()-1){
|
||||
poststring << ", ";
|
||||
}
|
||||
}
|
||||
poststring << "}";
|
||||
|
||||
std::cout << "post string: " << poststring.str() << "\n";
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, poststring.str().c_str());
|
||||
}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+="&";
|
||||
}
|
||||
}
|
||||
|
||||
myurl+="?"+getstring;
|
||||
}
|
||||
|
||||
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readString);
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
return readString;
|
||||
}
|
||||
|
||||
size_t API::write_data(void *contents, size_t size, size_t nmemb, FILE *stream) {
|
||||
((std::string*)stream)->append((char*)contents, size * nmemb);
|
||||
return size * nmemb;
|
||||
}
|
22
src/API.h
Normal file
22
src/API.h
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by lukas on 06.04.19.
|
||||
//
|
||||
|
||||
#ifndef IPREFRESHER_API_H
|
||||
#define IPREFRESHER_API_H
|
||||
|
||||
|
||||
#include <string>
|
||||
#include "Hashmap.h"
|
||||
|
||||
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);
|
||||
|
||||
private:
|
||||
static size_t write_data(void *buffer, size_t size, size_t buffersize, FILE *stream);
|
||||
};
|
||||
|
||||
|
||||
#endif //IPREFRESHER_API_H
|
47
src/Hashmap.h
Normal file
47
src/Hashmap.h
Normal file
@ -0,0 +1,47 @@
|
||||
//
|
||||
// Created by lukas on 07.04.19.
|
||||
//
|
||||
|
||||
#ifndef QT5PROJECT_HASHMAP_H
|
||||
#define QT5PROJECT_HASHMAP_H
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
template <class keytype,class valuetype> class Hashmap {
|
||||
public:
|
||||
void add(keytype key,keytype value);
|
||||
keytype getKey(int position);
|
||||
valuetype getValue(int position);
|
||||
|
||||
int size();
|
||||
|
||||
private:
|
||||
std::vector<keytype> keys;
|
||||
std::vector<valuetype> values;
|
||||
};
|
||||
|
||||
template<class keytype, class valuetype>
|
||||
void Hashmap<keytype, valuetype>::add(keytype key, keytype value) {
|
||||
keys.push_back(key);
|
||||
values.push_back(value);
|
||||
}
|
||||
|
||||
template<class keytype, class valuetype>
|
||||
keytype Hashmap<keytype, valuetype>::getKey(int position) {
|
||||
return keys.at(position);
|
||||
}
|
||||
|
||||
template<class keytype, class valuetype>
|
||||
valuetype Hashmap<keytype, valuetype>::getValue(int position) {
|
||||
return values.at(position);
|
||||
}
|
||||
|
||||
template<class keytype, class valuetype>
|
||||
int Hashmap<keytype, valuetype>::size() {
|
||||
return keys.size();
|
||||
}
|
||||
|
||||
|
||||
#endif //QT5PROJECT_HASHMAP_H
|
29
src/main.cpp
Normal file
29
src/main.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
#include "API.h"
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
|
||||
API api;
|
||||
std::string ip = api.request("https://api.ipify.org");
|
||||
std::cout << ip <<std::endl;
|
||||
|
||||
//api key: 88vNpMfDhMM2YYDNfWR1DNYfRX9W6fYg
|
||||
|
||||
Hashmap<std::string, std::string> args;
|
||||
|
||||
args.add("name","luki.dynu.net");
|
||||
args.add("ipv4Address",ip);
|
||||
|
||||
std::vector<std::string> headers;
|
||||
headers.push_back("accept: application/json");
|
||||
headers.push_back("User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)");
|
||||
headers.push_back("API-Key: 88vNpMfDhMM2YYDNfWR1DNYfRX9W6fYg");
|
||||
|
||||
std::string dynurepl = api.request("https://api.dynu.com/v2/dns/8506047",true, args,headers);
|
||||
|
||||
std::cout << "---" << dynurepl << "\n";
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user