diff options
Diffstat (limited to 'libtmdb/httpClient.cpp')
-rw-r--r-- | libtmdb/httpClient.cpp | 34 |
1 files changed, 6 insertions, 28 deletions
diff --git a/libtmdb/httpClient.cpp b/libtmdb/httpClient.cpp index a4b4eb7..4331c07 100644 --- a/libtmdb/httpClient.cpp +++ b/libtmdb/httpClient.cpp @@ -2,6 +2,7 @@ #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) : @@ -38,38 +39,15 @@ namespace TMDb { } } - static size_t - appendString(void * contents, size_t size, size_t nmemb, void * userp) - { - auto data = static_cast<std::stringstream *>(userp); - data->write(static_cast<const char *>(contents), size * nmemb); - return size * nmemb; - } - json::Value HttpClient::FetchJson(const std::string & path) const { - std::stringstream 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); - } + AdHoc::Net::CurlStreamSource css(path); + css.appendHeader("Accept: application/json"); + css.setopt(CURLOPT_USERAGENT, "libtmdb/1.0"); - return json::parseValue(jsonData); + AdHoc::Net::CurlStream curlstrm(css); + return json::parseValue(curlstrm); } } |