summaryrefslogtreecommitdiffstats
path: root/server/src/rwgps.cpp
blob: 21584a80e4a1a700e086ccaa588c48c786536783 (about) (plain) (blame)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
module;

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/json.hpp>

#include <openssl/crypto.h>

module routemon:rwgps$impl;

import :rwgps;

namespace beast = boost::beast;
namespace bhttp = beast::http;
namespace json = boost::json;

namespace routemon::rwgps {

auto tag_invoke(json::value_to_tag<route_summary> const&, json::value const& jv)
    -> route_summary
{
  return {
    .id = json::value_to<std::int64_t>(jv.at("id")),
    .user_id = json::value_to<std::int64_t>(jv.at("user_id")),
    .url = json::value_to<std::string>(jv.at("url")),
    .name = json::value_to<std::string>(jv.at("name")),
    .description = json::value_to<std::string>(jv.at("description")),
  };
}

auto tag_invoke(json::value_to_tag<pagination> const&, json::value const& jv)
    -> pagination
{
  return {
    .record_count = json::value_to<std::size_t>(jv.at("record_count")),
    .page_count = json::value_to<std::size_t>(jv.at("page_count")),
    .page_size = json::value_to<std::size_t>(jv.at("page_size")),
    .next_page_url =
        json::value_to<std::optional<std::string>>(jv.at("next_page_url")),
  };
}

auto tag_invoke(
    json::value_to_tag<get_routes_meta> const&, json::value const& jv)
    -> get_routes_meta
{
  return {
    .pagination = json::value_to<pagination>(jv.at("pagination")),
  };
}

auto tag_invoke(
    json::value_to_tag<get_routes_response> const&, json::value const& jv)
    -> get_routes_response
{
  return {
    .routes = json::value_to<std::vector<route_summary>>(jv.at("routes")),
    .meta = json::value_to<get_routes_meta>(jv.at("meta")),
  };
}

auto json_value_to_get_routes_response(json::value const& jv)
    -> get_routes_response
{
  return json::value_to<get_routes_response>(jv);
}

static json::value const json_null = json::value{};

auto at_or_null(json::value const& v, std::string_view key)
    -> json::value const&
{
  if (v.is_null())
    return json_null;
  auto const* obj = v.if_object();
  if (!obj)
    throw std::system_error{make_error_code(json::error::not_object)};
  if (auto it = obj->find(key); it != obj->end())
    return it->value();
  return json_null;
}

auto tag_invoke(
    json::value_to_tag<notification_cause> const&, json::value const& jv)
    -> notification_cause
{
  if (jv == "created")
    return notification_cause::created;
  if (jv == "updated")
    return notification_cause::updated;
  if (jv == "deleted")
    return notification_cause::deleted;
  if (jv == "added")
    return notification_cause::added;
  if (jv == "removed")
    return notification_cause::removed;
  return notification_cause::unknown;
}

auto tag_invoke(
    json::value_to_tag<notification_item_type> const&, json::value const& jv)
    -> notification_item_type
{
  if (jv == "trip")
    return notification_item_type::trip;
  if (jv == "route")
    return notification_item_type::route;
  if (jv == "event")
    return notification_item_type::event;
  if (jv == "event_participant")
    return notification_item_type::event_participant;
  if (jv == "club_member")
    return notification_item_type::club_member;
  if (jv == "authorization")
    return notification_item_type::authorization;
  return notification_item_type::unknown;
}

auto tag_invoke(
    json::value_to_tag<notification_target_collection_type> const&,
    json::value const& jv) -> notification_target_collection_type
{
  if (jv == "pinned")
    return notification_target_collection_type::pinned;
  return notification_target_collection_type::unknown;
}

auto tag_invoke(
    json::value_to_tag<notification_target_collection> const&,
    json::value const& jv) -> notification_target_collection
{
  return {
    .id = json::value_to<std::int64_t>(at_or_null(jv, "id")),
    .type = json::value_to<notification_target_collection_type>(
        at_or_null(jv, "type")),
    .url = json::value_to<std::optional<std::string>>(at_or_null(jv, "url")),
  };
}

auto tag_invoke(json::value_to_tag<notification> const&, json::value const& jv)
    -> notification
{
  return {
    .user_id = json::value_to<std::int64_t>(at_or_null(jv, "user_id")),
    .item_type =
        json::value_to<notification_item_type>(at_or_null(jv, "item_type")),
    .item_id =
        json::value_to<std::optional<std::int64_t>>(at_or_null(jv, "item_id")),
    .item_user_id = json::value_to<std::optional<std::int64_t>>(
        at_or_null(jv, "item_user_id")),
    .item_url =
        json::value_to<std::optional<std::string>>(at_or_null(jv, "item_url")),
    .action = json::value_to<notification_cause>(at_or_null(jv, "action")),
    .collection = json::value_to<std::optional<notification_target_collection>>(
        at_or_null(jv, "collection")),
  };
}

auto tag_invoke(
    json::value_to_tag<webhook_request> const&, json::value const& jv)
    -> webhook_request
{
  return {
    .notifications = json::value_to<std::vector<notification>>(
        at_or_null(jv, "notifications")),
  };
}

credentials::credentials(
    std::string&& api_key, std::string&& api_secret, std::string&& auth_token)
  : api_key_{std::move(api_key)}, api_secret_{std::move(api_secret)},
    auth_token_{std::move(auth_token)}
{
}

credentials::~credentials()
{
  OPENSSL_cleanse(api_key_.data(), api_key_.capacity());
  OPENSSL_cleanse(api_secret_.data(), api_secret_.capacity());
  OPENSSL_cleanse(auth_token_.data(), auth_token_.capacity());
}

auto credentials::api_key() const -> std::string const& { return api_key_; }

auto credentials::api_secret() const -> std::string const&
{
  return api_secret_;
}

auto credentials::auth_token() const -> std::string const&
{
  return auth_token_;
}

constexpr std::string host = "ridewithgps.com";

// TODO: handle failure appropriately
auto client::get_routes_page(std::size_t page) -> get_routes_response
{
  auto req = bhttp::request<bhttp::string_body>{
    bhttp::verb::get,
    std::format("/api/v1/routes.json?page_size=200?page={}", page),
    11, // HTTP 1.1
  };
  req.set(bhttp::field::host, host);
  req.set("x-rwgps-api-key", creds_->api_key());
  req.set("x-rwgps-auth-token", creds_->auth_token());

  auto rsp = hc_.do_request(req);
  auto p = json::stream_parser{};
  for (auto const frag : rsp.body().cdata())
    p.write(static_cast<char const*>(frag.data()), frag.size());
  assert(p.done());
  return json_value_to_get_routes_response(p.release());
}

client::client(
    net::io_context& ioc, log::logger const& l,
    std::shared_ptr<credentials> creds)
  : l_{l.sub("rwgps-client")}, hc_{ioc}, creds_{std::move(creds)}
{
}

auto client::get_all_routes() -> std::vector<route_summary>
{
  // TODO: make sure that there are no duplicates here.
  // What does RWGPS sort on, by default?
  // Consider using an associative container instead of a vector.
  auto record_count = 0uz;
  auto current_page = 0uz;
  auto routes = std::vector<route_summary>{};

  while (true)
  {
    auto rsp = get_routes_page(current_page);
    if (rsp.meta.pagination.next_page_url)
      l_.debug("Next page URL: {}", *rsp.meta.pagination.next_page_url);
    routes.append_range(rsp.routes);
    if (rsp.meta.pagination.record_count > 0)
      record_count = rsp.meta.pagination.record_count;
    if (rsp.routes.empty() || routes.size() >= record_count)
    {
      break;
    }
  }

  return routes;
}

} // namespace routemon::rwgps