diff options
author | randomdan <randomdan@localhost> | 2014-09-19 23:52:30 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2014-09-19 23:52:30 +0000 |
commit | 28691c29e93fbc4fdb5ae4866287282840fba021 (patch) | |
tree | 8b83ca80afc3f3a5d11b04a56c440d2aa38f4e16 /libtmdb/httpClient.h | |
parent | Support muxing directly in storage (diff) | |
download | p2pvr-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.h')
-rw-r--r-- | libtmdb/httpClient.h | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/libtmdb/httpClient.h b/libtmdb/httpClient.h new file mode 100644 index 0000000..1b50b7e --- /dev/null +++ b/libtmdb/httpClient.h @@ -0,0 +1,73 @@ +#include <boost/variant.hpp> +#include <jsonpp.h> +#include <slicer/slicer.h> +#include <json/serializer.h> +#include <boost/format.hpp> +#include <boost/lexical_cast.hpp> + +namespace TMDb { + class HttpClient { + public: + class ParameterValue { + public: + ParameterValue(const std::string & v) : + value(v) + { + } + + template <typename V> + ParameterValue(const V & v) : + value(boost::lexical_cast<std::string>(v)) + { + } + + template <typename V> + ParameterValue(const IceUtil::Optional<V> & v) : + value(v ? boost::lexical_cast<std::string>(*v) : IceUtil::Optional<std::string>()) + { + } + + IceUtil::Optional<std::string> value; + }; + typedef std::map<std::string, ParameterValue> Parameters; + + protected: + HttpClient(const std::string & baseUrl, const std::string & apikey); + + template <typename ... Params> + std::string + GetUrl(const std::string & pathFormat, const Params & ... params, const Parameters & parameters) const + { + boost::format path(pathFormat); + packParams(path, params...); + std::string pathStr(path.str()); + appendQueryParameters(pathStr, parameters); + return BaseURL + pathStr; + } + + template <typename ReturnModel, typename ... Params> + IceInternal::Handle<ReturnModel> + GetData(const std::string & pathFormat, const Params & ... params, const Parameters & parameters) const + { + json::Value data = FetchJson(GetUrl<Params...>(pathFormat, params..., parameters)); + return Slicer::Deserialize<Slicer::JsonValue, ReturnModel>(data); + } + + static void packParams(boost::format &); + + template <typename T, typename ... Ts> + static void packParams(boost::format & fmt, const T & param, const Ts & ... params) + { + fmt % param; + packParams(fmt, params...); + } + + void appendQueryParameters(std::string & path, const Parameters & parameters) const; + void appendQueryParameters(std::string & path, const Parameters::value_type & nvp) const; + json::Value FetchJson(const std::string & path) const; + + const std::string BaseURL; + const std::string ApiKey; + }; +}; + |