refactored curl code into new helper class

This commit is contained in:
Bob Polis 2020-09-02 09:53:22 +02:00
parent efaf6124e5
commit d38251fe66
3 changed files with 71 additions and 31 deletions

View File

@ -13,8 +13,8 @@
#include <vector>
#include <cstring>
#include <getopt.h>
#include <curl/curl.h>
#include <libsclogging.hpp>
#include "requester.hpp"
sc::logger logger {"curly", sc::loglevel::info};
@ -28,16 +28,6 @@ void print_version() {
std::cout << "curly version 1.0\n";
}
size_t write_data(void* buf, size_t sz, size_t nmemb, void* userp) {
size_t realsize = sz * nmemb;
SCInfo(logger, "received ", realsize, " bytes");
std::vector<char>* dest {reinterpret_cast<std::vector<char>*>(userp)};
size_t oldsize = dest->size();
dest->resize(oldsize + realsize);
std::memcpy(dest->data() + oldsize, buf, realsize);
return realsize;
}
int main(int argc, const char * argv[]) {
try {
int opt_char, opt_val;
@ -76,27 +66,10 @@ int main(int argc, const char * argv[]) {
}
}
std::cout << "hello, curly\n";
requester req;
std::cout << req.get("https://www.swiftcoder.nl/") << '\n';
std::cout << req.get("https://www.swiftcoder.nl/moneydance/") << '\n';
curl_global_init(CURL_GLOBAL_ALL);
auto easy_handle = curl_easy_init();
curl_easy_setopt(easy_handle, CURLOPT_URL, "https://www.swiftcoder.nl/");
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);
std::vector<char> buf;
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &buf);
auto success = curl_easy_perform(easy_handle);
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
std::string text {buf.data(), buf.size()};
std::cout << text << '\n';
curl_easy_setopt(easy_handle, CURLOPT_URL, "https://www.swiftcoder.nl/moneydance/");
buf.clear();
success = curl_easy_perform(easy_handle);
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
std::string page2 {buf.data(), buf.size()};
std::cout << page2 << '\n';
} catch (const std::exception& ex) {
std::cerr << "curly: " << ex.what() << '\n';
return EXIT_FAILURE;

40
requester.cpp Normal file
View File

@ -0,0 +1,40 @@
//
// 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>
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");
std::vector<char>* dest {reinterpret_cast<std::vector<char>*>(user_data)};
size_t oldsize = dest->size();
dest->resize(oldsize + realsize);
std::memcpy(dest->data() + oldsize, buf, realsize);
return realsize;
}
requester::requester() {
curl_global_init(CURL_GLOBAL_ALL);
_h = curl_easy_init();
curl_easy_setopt(_h, CURLOPT_WRITEFUNCTION, write_data);
}
std::string requester::get(const std::string &url) {
curl_easy_setopt(_h, CURLOPT_URL, url.c_str());
std::vector<char> buf;
curl_easy_setopt(_h, CURLOPT_WRITEDATA, &buf);
auto success = curl_easy_perform(_h);
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
std::string text {buf.data(), buf.size()};
return text;
}

27
requester.hpp Normal file
View File

@ -0,0 +1,27 @@
//
// requester.hpp
// curly
//
// Created by Bob Polis at 2020-09-01
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#ifndef _requester_H_
#define _requester_H_
#include <string>
#include <curl/curl.h>
class requester {
public:
static size_t write_data(char* buf, size_t sz, size_t nmemb, void* user_data);
requester();
std::string get(const std::string& url);
private:
CURL* _h;
};
#endif // _requester_H_