1#include "cpr/curl_container.h"
2#include <algorithm>
3#include <iterator>
4
5
6namespace cpr {
7template <class T>
8CurlContainer<T>::CurlContainer(const std::initializer_list<T>& containerList) : containerList_(containerList) {}
9
10template <class T>
11void 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
15template <class T>
16void CurlContainer<T>::Add(const T& element) {
17 containerList_.push_back(std::move(element));
18}
19
20template <>
21const 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
41template <>
42const 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
55template class CurlContainer<Pair>;
56template class CurlContainer<Parameter>;
57
58} // namespace cpr
59