2020-09-02 09:53:22 +02:00
|
|
|
//
|
|
|
|
// requester.cpp
|
|
|
|
// curly
|
|
|
|
//
|
|
|
|
// Created by Bob Polis at 2020-09-01
|
|
|
|
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "requester.hpp"
|
|
|
|
#include <libsclogging.hpp>
|
|
|
|
#include <vector>
|
|
|
|
#include <cstring>
|
2020-09-02 10:02:50 +02:00
|
|
|
#include <stdexcept>
|
2020-09-02 09:53:22 +02:00
|
|
|
|
|
|
|
extern sc::logger logger;
|
|
|
|
|
|
|
|
size_t requester::write_data(char *buf, size_t sz, size_t nmemb, void *user_data) {
|
|
|
|
size_t realsize = sz * nmemb;
|
|
|
|
SCInfo(logger, "received ", realsize, " bytes");
|
2020-11-04 14:06:01 +01:00
|
|
|
std::string* text {reinterpret_cast<std::string*>(user_data)};
|
|
|
|
text->append(buf, realsize);
|
2020-09-02 09:53:22 +02:00
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
requester::requester() {
|
2020-09-02 09:57:45 +02:00
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
2020-09-12 19:34:32 +02:00
|
|
|
_h.reset(curl_easy_init());
|
|
|
|
curl_easy_setopt(_h.get(), CURLOPT_WRITEFUNCTION, write_data);
|
2020-09-02 10:59:31 +02:00
|
|
|
}
|
|
|
|
|
2020-10-30 11:11:30 +01:00
|
|
|
requester::~requester() {
|
|
|
|
curl_global_cleanup();
|
|
|
|
}
|
|
|
|
|
2020-09-02 09:53:22 +02:00
|
|
|
std::string requester::get(const std::string &url) {
|
2020-11-04 14:06:01 +01:00
|
|
|
std::string text;
|
2020-09-12 19:34:32 +02:00
|
|
|
curl_easy_setopt(_h.get(), CURLOPT_URL, url.c_str());
|
2020-11-04 14:06:01 +01:00
|
|
|
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &text);
|
2020-09-12 19:34:32 +02:00
|
|
|
auto success = curl_easy_perform(_h.get());
|
2020-09-02 09:53:22 +02:00
|
|
|
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
|
|
|
|
return text;
|
|
|
|
}
|