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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
};
};
|