1 | #include "cpr/curl_container.h" |
---|---|
2 | #include <algorithm> |
3 | #include <iterator> |
4 | |
5 | |
6 | namespace cpr { |
7 | template <class T> |
8 | CurlContainer<T>::CurlContainer(const std::initializer_list<T>& containerList) : containerList_(containerList) {} |
9 | |
10 | template <class T> |
11 | void CurlContainer<T>::Add(const std::initializer_list<T>& containerList) { |
12 | std::transform(containerList.begin(), containerList.end(), std::back_inserter(containerList_), [](const T& elem) { return std::move(elem); }); |
13 | } |
14 | |
15 | template <class T> |
16 | void CurlContainer<T>::Add(const T& element) { |
17 | containerList_.push_back(std::move(element)); |
18 | } |
19 | |
20 | template <> |
21 | const std::string CurlContainer<Parameter>::GetContent(const CurlHolder& holder) const { |
22 | std::string content{}; |
23 | for (const Parameter& parameter : containerList_) { |
24 | if (!content.empty()) { |
25 | content += "&"; |
26 | } |
27 | |
28 | const std::string escapedKey = encode ? holder.urlEncode(s: parameter.key) : parameter.key; |
29 | if (parameter.value.empty()) { |
30 | content += escapedKey; |
31 | } else { |
32 | const std::string escapedValue = encode ? holder.urlEncode(s: parameter.value) : parameter.value; |
33 | content += escapedKey + "="; |
34 | content += escapedValue; |
35 | } |
36 | }; |
37 | |
38 | return content; |
39 | } |
40 | |
41 | template <> |
42 | const std::string CurlContainer<Pair>::GetContent(const CurlHolder& holder) const { |
43 | std::string content{}; |
44 | for (const cpr::Pair& element : containerList_) { |
45 | if (!content.empty()) { |
46 | content += "&"; |
47 | } |
48 | const std::string escaped = encode ? holder.urlEncode(s: element.value) : element.value; |
49 | content += element.key + "="+ escaped; |
50 | } |
51 | |
52 | return content; |
53 | } |
54 | |
55 | template class CurlContainer<Pair>; |
56 | template class CurlContainer<Parameter>; |
57 | |
58 | } // namespace cpr |
59 |