Update TODO
[dueringa_WikiWalker.git] / src / CurlUrlCreator.cpp
blob75c2fa1f15afcddf0e9854f5b08a135de38af4bc
1 //! \file CurlUrlCreator.cpp
3 #include <utility>
5 #include "CurlUrlCreator.h"
7 namespace WikiWalker
9 CurlUrlCreator::CurlUrlCreator(std::string baseUrl)
10 : baseUrl_(std::move(baseUrl))
12 curl_global_init(CURL_GLOBAL_ALL);
13 handle_ = curl_easy_init();
16 CurlUrlCreator::~CurlUrlCreator()
18 curl_easy_cleanup(handle_);
19 curl_global_cleanup();
22 CurlUrlCreator& CurlUrlCreator::addParameter(const std::string& param,
23 const std::string& value)
25 // overwrite
26 char* eval = curl_easy_escape(handle_, value.c_str(), 0);
27 args_[param] = eval;
28 curl_free(eval);
29 return *this;
32 CurlUrlCreator& CurlUrlCreator::addParameter(
33 const std::map<const std::string, const std::string>& params)
35 /* we need to escape the values, so doing a direct ranged insert isn't
36 * possible */
37 for(auto& paramterPair : params) {
38 addParameter(paramterPair.first, paramterPair.second);
40 return *this;
43 std::string CurlUrlCreator::buildUrl() const
45 std::string ret = baseUrl_;
46 ret.append("?");
48 for(auto parpair : args_) {
49 ret.append(parpair.first).append("=").append(parpair.second).append("&");
52 ret.pop_back();
54 return ret;
57 void CurlUrlCreator::reset()
59 args_.clear();
61 } // namespace WikiWalker