1
// Copyright (C) 2016 Canonical Ltd.
3
// This library is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU Lesser General Public License as published
5
// by the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
8
// This program is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
// GNU General Public License for more details.
13
// You should have received a copy of the GNU Lesser General Public License
14
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
#include <ichnaea/client.h>
16
#include <ichnaea/codec.h>
18
#include <ichnaea/util/json.hpp>
20
#include <core/net/uri.h>
21
#include <core/net/http/client.h>
22
#include <core/net/http/request.h>
23
#include <core/net/http/response.h>
24
#include <core/net/http/status.h>
26
#include <boost/lexical_cast.hpp>
28
namespace http = core::net::http;
29
namespace json = nlohmann;
31
ichnaea::Client::Client(const std::string& host, const std::string& api_key, const std::shared_ptr<http::Client>& http_client)
32
: host{host}, api_key{api_key}, http_client{http_client}
36
ichnaea::Client::Client(const std::string& api_key, const std::shared_ptr<http::Client>& http_client)
37
: Client{Client::default_host, api_key, http_client}
41
void ichnaea::Client::geolocate(const geolocate::Parameters& parameters, const std::function<void(const Response<geolocate::Result>&)>& cb)
43
json::json root; root & parameters;
45
http::Request::Configuration config;
46
config.uri = http_client->uri_to_string(core::net::make_uri(host, {"v1", "geolocate"}, {{"key", api_key}}));
47
http::Request::Handler handler;
48
handler.on_response([cb](const http::Response& response)
50
auto root = json::json::parse(response.body);
52
if (response.status != http::Status::ok)
54
Error e{http::Status::bad_request, ""}; root & std::ref(e);
55
cb(Response<geolocate::Result>{e});
59
geolocate::Result r; root & std::ref(r);
60
cb(Response<geolocate::Result>{r});
64
http_client->post(config, root.dump(), "application/json")->async_execute(handler);
67
void ichnaea::Client::geosubmit(const geosubmit::Parameters& parameters, const std::function<void(const Response<geosubmit::Result>&)>& cb)
69
if (parameters.reports.empty())
72
json::json root; root & parameters;
74
http::Request::Configuration config;
75
config.uri = http_client->uri_to_string(core::net::make_uri(host, {"v2", "geosubmit"}, {}));
76
http::Request::Handler handler;
77
handler.on_response([cb](const http::Response& response)
79
auto root = json::json::parse(response.body);
81
if (response.status != http::Status::ok)
83
Error e{http::Status::bad_request, ""}; root & std::ref(e);
84
cb(Response<geosubmit::Result>{e});
88
geosubmit::Result r; root & std::ref(r);
89
cb(Response<geosubmit::Result>{r});
93
http_client->post(config, root.dump(), "application/json")->async_execute(handler);
96
void ichnaea::Client::region(const region::Parameters& parameters, const std::function<void(const Response<region::Result>&)>& cb)
98
json::json root; root & parameters;
100
http::Request::Configuration config;
101
config.uri = http_client->uri_to_string(core::net::make_uri(host, {"v1", "country"}, {{"key", api_key}}));
102
http::Request::Handler handler;
103
handler.on_response([cb](const http::Response& response)
105
auto root = json::json::parse(response.body);
107
if (response.status != http::Status::ok)
109
Error e{http::Status::bad_request, ""}; root & std::ref(e);
110
cb(Response<region::Result>{e});
114
region::Result r; root & std::ref(r);
115
cb(Response<region::Result>{r});
119
http_client->post(config, root.dump(), "application/json")->async_execute(handler);