-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
100 lines (83 loc) · 2.74 KB
/
main.cpp
File metadata and controls
100 lines (83 loc) · 2.74 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <cpprest/http_client.h>
#include <boost/program_options.hpp>
enum class FormatType {
json
};
FormatType formatFromString(const std::string& typeStr)
{
if (typeStr == "json")
return FormatType::json;
return FormatType::json;
}
std::ostream& operator << (std::ostream& ostr, const FormatType& type )
{
switch (type) {
case FormatType::json:
return ostr << "json";
default:
return ostr;
}
}
std::istream& operator >> (std::istream& istr, FormatType& type)
{
std::string typeStr;
istr >> typeStr;
type = formatFromString(typeStr);
return istr;
}
pplx::task<std::string> RequestJsonAsync(std::string serverAddress, FormatType)
{
web::http::client::http_client client(serverAddress);
return client.request(web::http::methods::GET)
.then ([] (web::http::http_response response) -> pplx::task<web::json::value>
{
if (response.status_code() == web::http::status_codes::OK)
return response.extract_json();
throw std::runtime_error("Unable to get response from server");
})
.then([] (pplx::task<web::json::value> jsonTask)
{
return jsonTask.get().at(U("time_zone")).as_string();
});
}
using namespace std;
namespace ProgramOption = boost::program_options;
template<typename Type, typename Value>
bool exist(const Type& type, const Value& value)
{
return type.find(value) != type.end();
}
std::string extractAddress(ProgramOption::variables_map& variableMap)
{
return exist(variableMap, "address") ? variableMap["address"].as<std::string>()
: std::string("http://freegeoip.net/json/");
}
FormatType extractFormatType(ProgramOption::variables_map& variableMap)
{
return exist(variableMap, "type") ? variableMap["type"].as<FormatType>()
: FormatType::json;
}
boost::program_options::variables_map initializeParserArgument(int argc, char *argv[])
{
ProgramOption::options_description optionDescription("Allowed option");
optionDescription.add_options()
("help", "produce help message")
("type", ProgramOption::value<FormatType>(), "format type: for now can be only json [json]")
("address", ProgramOption::value<std::string>(), "server address [http://freegeoip.net/json/]")
;
ProgramOption::variables_map variableMap;
ProgramOption::store(ProgramOption::parse_command_line(argc, argv, optionDescription), variableMap);
ProgramOption::notify(variableMap);
return variableMap;
}
int main(int argc, char *argv[])
{
ProgramOption::variables_map variableMap = initializeParserArgument(argc, argv);
auto serverAddress = extractAddress(variableMap);
auto formatType = extractFormatType(variableMap);
std::cout << "address is " << serverAddress << '\n'
<< "type is " << formatType << endl;
ucout << "time zone is: " << RequestJsonAsync(serverAddress, formatType).get() << std::endl;
return 0;
}