From 28691c29e93fbc4fdb5ae4866287282840fba021 Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 19 Sep 2014 23:52:30 +0000 Subject: First bash at a slicer ice proxy for TMDb and an untested instantiation in p2pvr's core adapter --- libtmdb/httpClient.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 libtmdb/httpClient.cpp (limited to 'libtmdb/httpClient.cpp') diff --git a/libtmdb/httpClient.cpp b/libtmdb/httpClient.cpp new file mode 100644 index 0000000..93fdd76 --- /dev/null +++ b/libtmdb/httpClient.cpp @@ -0,0 +1,77 @@ +#include "httpClient.h" +#include +#include +#include +#include + +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, { "apikey", ApiKey }); + BOOST_FOREACH(const auto & nvp, parameters) { + if (nvp.second.value) { + path += "&"; + appendQueryParameters(path, nvp); + } + } + } + + static size_t + appendString(void * contents, size_t size, size_t nmemb, void * userp) + { + auto data = static_cast(userp); + data->append(static_cast(contents), size * nmemb); + return size * nmemb; + } + + json::Value + HttpClient::FetchJson(const std::string & path) const + { + Glib::ustring jsonData; + + struct curl_slist *headers = NULL; + curl_slist_append(headers, "Accept: application/json"); + + CURL * curl_handle = curl_easy_init(); + curl_easy_setopt(curl_handle, CURLOPT_URL, path.c_str()); + curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, appendString); + curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&jsonData); + curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); + curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers); + CURLcode res = curl_easy_perform(curl_handle); + + long http_code = 0; + curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code); + curl_easy_cleanup(curl_handle); + if (res != CURLE_OK) { + throw TMDb::HttpException(http_code); + } + + Glib::ustring::const_iterator itr = jsonData.begin(); + return json::parseValue(itr); + } +} + -- cgit v1.2.3