summaryrefslogtreecommitdiff
path: root/libtmdb/httpClient.cpp
blob: 45377cff49fdae6bd5e4f2a6a0c6e1f1e2a9f04c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "httpClient.h"
#include <boost/lexical_cast.hpp>
#include <curl/curl.h>
#include <tmdb-api.h>
#include <curlStream.h>

namespace TMDb {
	HttpClient::HttpClient(const std::string & bu, const std::string & k) :
		BaseURL(bu),
		ApiKey(k)
	{
	}

	void
	HttpClient::packParams(boost::format &)
	{
	}

	void
	HttpClient::appendQueryParameters(std::string & path, const Parameters::value_type & nvp) const
	{
		path += nvp.first;
		path += "=";
		auto ev = curl_easy_escape(NULL, nvp.second.value->c_str(), nvp.second.value->size());
		path += ev;
		curl_free(ev);
	}

	void
	HttpClient::appendQueryParameters(std::string & path, const Parameters & parameters) const
	{
		path += "?";
		appendQueryParameters(path, { "api_key", ApiKey });
		for (const auto & nvp : parameters) {
			if (nvp.second.value) {
				path += "&";
				appendQueryParameters(path, nvp);
			}
		}
	}

	json::Value
	HttpClient::FetchJson(const std::string & path) const
	{
		AdHoc::Net::CurlStreamSource css(path);
		css.appendHeader("Accept: application/json");
		css.setopt(CURLOPT_USERAGENT, "libtmdb/1.0");

		AdHoc::Net::CurlStream curlstrm(css);
		return json::parseValue(curlstrm);
	}
}