Slimline fork of https://github.com/SpectrumIM/spectrum2, with lots of things cleaned up and improved
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

74 lines
1.5 KiB

#pragma once
#include <json/json.h>
#include <string.h>
#include <boost/signals2.hpp>
#include <iostream>
#include <sstream>
#include "curl/curl.h"
#include "transport/Logging.h"
#include "transport/ThreadPool.h"
namespace Transport {
class HTTPRequest : public Thread {
public:
typedef enum { Get } Type;
typedef boost::function<void(HTTPRequest *, bool, Json::Value &json, const std::string &data)> Callback;
HTTPRequest(ThreadPool *tp, Type type, const std::string &url, Callback callback);
HTTPRequest(Type type, const std::string &url);
virtual ~HTTPRequest();
void setProxy(std::string, std::string, std::string, std::string);
bool execute();
bool execute(Json::Value &json);
std::string getError() {
return std::string(curl_errorbuffer);
}
const std::string &getRawData() {
return m_data;
}
void run();
void finalize();
const std::string &getURL() {
return m_url;
}
boost::signals2::signal<void()> onRequestFinished;
static void globalInit() {
curl_global_init(CURL_GLOBAL_ALL);
}
static void globalCleanup() {
curl_global_cleanup();
}
private:
bool init();
bool GET(std::string url, std::string &output);
bool GET(std::string url, Json::Value &json);
CURL *curlhandle;
char curl_errorbuffer[1024];
std::string error;
std::string callbackdata;
ThreadPool *m_tp;
std::string m_url;
bool m_ok;
Json::Value m_json;
std::string m_data;
Callback m_callback;
Type m_type;
static int curlCallBack(char *data, size_t size, size_t nmemb, HTTPRequest *obj);
};
} // namespace Transport