From d38251fe66d14bf01301dd0f29cea3346fe333a2 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Wed, 2 Sep 2020 09:53:22 +0200 Subject: [PATCH] refactored curl code into new helper class --- main.cpp | 35 ++++------------------------------- requester.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ requester.hpp | 27 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 31 deletions(-) create mode 100644 requester.cpp create mode 100644 requester.hpp diff --git a/main.cpp b/main.cpp index 28e1fc3..93226cb 100644 --- a/main.cpp +++ b/main.cpp @@ -13,8 +13,8 @@ #include #include #include -#include #include +#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* dest {reinterpret_cast*>(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 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; diff --git a/requester.cpp b/requester.cpp new file mode 100644 index 0000000..7cc85a3 --- /dev/null +++ b/requester.cpp @@ -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 +#include +#include + +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* dest {reinterpret_cast*>(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 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; +} diff --git a/requester.hpp b/requester.hpp new file mode 100644 index 0000000..fbf6deb --- /dev/null +++ b/requester.hpp @@ -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 +#include + +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_