summaryrefslogtreecommitdiff
path: root/libtmdb/httpClient.cpp
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2014-09-19 23:52:30 +0000
committerrandomdan <randomdan@localhost>2014-09-19 23:52:30 +0000
commit28691c29e93fbc4fdb5ae4866287282840fba021 (patch)
tree8b83ca80afc3f3a5d11b04a56c440d2aa38f4e16 /libtmdb/httpClient.cpp
parentSupport muxing directly in storage (diff)
downloadp2pvr-28691c29e93fbc4fdb5ae4866287282840fba021.tar.bz2
p2pvr-28691c29e93fbc4fdb5ae4866287282840fba021.tar.xz
p2pvr-28691c29e93fbc4fdb5ae4866287282840fba021.zip
First bash at a slicer ice proxy for TMDb and an untested instantiation in p2pvr's core adapter
Diffstat (limited to 'libtmdb/httpClient.cpp')
-rw-r--r--libtmdb/httpClient.cpp77
1 files changed, 77 insertions, 0 deletions
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 <boost/foreach.hpp>
+#include <boost/lexical_cast.hpp>
+#include <curl/curl.h>
+#include <tmdb-api.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, { "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<Glib::ustring *>(userp);
+ data->append(static_cast<const char *>(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);
+ }
+}
+