~thomas-voss/location-service/refactor-location-position

« back to all changes in this revision

Viewing changes to 3rd-party/ichnaea/src/ichnaea/client.cpp

  • Committer: Thomas Voß
  • Date: 2016-08-14 19:36:05 UTC
  • Revision ID: thomas.voss@canonical.com-20160814193605-e6c0xitja9yncmw8
Add back mls provider relying on Mozilla's location service.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2016 Canonical Ltd.
 
2
// 
 
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.
 
7
// 
 
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.
 
12
// 
 
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>
 
17
 
 
18
#include <ichnaea/util/json.hpp>
 
19
 
 
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>
 
25
 
 
26
#include <boost/lexical_cast.hpp>
 
27
 
 
28
namespace http = core::net::http;
 
29
namespace json = nlohmann;
 
30
 
 
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}
 
33
{
 
34
}
 
35
 
 
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}
 
38
{
 
39
}
 
40
 
 
41
void ichnaea::Client::geolocate(const geolocate::Parameters& parameters, const std::function<void(const Response<geolocate::Result>&)>& cb)
 
42
{
 
43
    json::json root; root & parameters;
 
44
 
 
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)
 
49
    {
 
50
        auto root = json::json::parse(response.body);
 
51
 
 
52
        if (response.status != http::Status::ok)
 
53
        {
 
54
            Error e{http::Status::bad_request, ""}; root & std::ref(e);
 
55
            cb(Response<geolocate::Result>{e});
 
56
        }
 
57
        else
 
58
        {
 
59
            geolocate::Result r; root & std::ref(r);
 
60
            cb(Response<geolocate::Result>{r});
 
61
        }
 
62
    });
 
63
 
 
64
    http_client->post(config, root.dump(), "application/json")->async_execute(handler);
 
65
}
 
66
 
 
67
void ichnaea::Client::geosubmit(const geosubmit::Parameters& parameters, const std::function<void(const Response<geosubmit::Result>&)>& cb)
 
68
{
 
69
    if (parameters.reports.empty())
 
70
        return;
 
71
 
 
72
    json::json root; root & parameters;
 
73
 
 
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)
 
78
    {
 
79
        auto root = json::json::parse(response.body);
 
80
 
 
81
        if (response.status != http::Status::ok)
 
82
        {
 
83
            Error e{http::Status::bad_request, ""}; root & std::ref(e);
 
84
            cb(Response<geosubmit::Result>{e});
 
85
        }
 
86
        else
 
87
        {
 
88
            geosubmit::Result r; root & std::ref(r);
 
89
            cb(Response<geosubmit::Result>{r});
 
90
        }
 
91
    });
 
92
 
 
93
    http_client->post(config, root.dump(), "application/json")->async_execute(handler);
 
94
}
 
95
 
 
96
void ichnaea::Client::region(const region::Parameters& parameters, const std::function<void(const Response<region::Result>&)>& cb)
 
97
{
 
98
    json::json root; root & parameters;
 
99
 
 
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)
 
104
    {
 
105
        auto root = json::json::parse(response.body);
 
106
 
 
107
        if (response.status != http::Status::ok)
 
108
        {
 
109
            Error e{http::Status::bad_request, ""}; root & std::ref(e);
 
110
            cb(Response<region::Result>{e});
 
111
        }
 
112
        else
 
113
        {
 
114
            region::Result r; root & std::ref(r);
 
115
            cb(Response<region::Result>{r});
 
116
        }
 
117
    });
 
118
 
 
119
    http_client->post(config, root.dump(), "application/json")->async_execute(handler);
 
120
}