diff options
Diffstat (limited to 'server/src/rwgps.cpp')
| -rw-r--r-- | server/src/rwgps.cpp | 168 |
1 files changed, 134 insertions, 34 deletions
diff --git a/server/src/rwgps.cpp b/server/src/rwgps.cpp index b8ebf11..21584a8 100644 --- a/server/src/rwgps.cpp +++ b/server/src/rwgps.cpp | |||
| @@ -4,6 +4,8 @@ module; | |||
| 4 | #include <boost/beast/http.hpp> | 4 | #include <boost/beast/http.hpp> |
| 5 | #include <boost/json.hpp> | 5 | #include <boost/json.hpp> |
| 6 | 6 | ||
| 7 | #include <openssl/crypto.h> | ||
| 8 | |||
| 7 | module routemon:rwgps$impl; | 9 | module routemon:rwgps$impl; |
| 8 | 10 | ||
| 9 | import :rwgps; | 11 | import :rwgps; |
| @@ -14,34 +16,6 @@ namespace json = boost::json; | |||
| 14 | 16 | ||
| 15 | namespace routemon::rwgps { | 17 | namespace routemon::rwgps { |
| 16 | 18 | ||
| 17 | export struct route_summary | ||
| 18 | { | ||
| 19 | std::int64_t id; | ||
| 20 | std::int64_t user_id; | ||
| 21 | std::string url; | ||
| 22 | std::string name; | ||
| 23 | std::string description; | ||
| 24 | }; | ||
| 25 | |||
| 26 | struct pagination | ||
| 27 | { | ||
| 28 | std::size_t record_count; | ||
| 29 | std::size_t page_count; | ||
| 30 | std::size_t page_size; | ||
| 31 | std::optional<std::string> next_page_url; | ||
| 32 | }; | ||
| 33 | |||
| 34 | struct get_routes_meta | ||
| 35 | { | ||
| 36 | pagination pagination; | ||
| 37 | }; | ||
| 38 | |||
| 39 | struct get_routes_response | ||
| 40 | { | ||
| 41 | std::vector<route_summary> routes; | ||
| 42 | get_routes_meta meta; | ||
| 43 | }; | ||
| 44 | |||
| 45 | auto tag_invoke(json::value_to_tag<route_summary> const&, json::value const& jv) | 19 | auto tag_invoke(json::value_to_tag<route_summary> const&, json::value const& jv) |
| 46 | -> route_summary | 20 | -> route_summary |
| 47 | { | 21 | { |
| @@ -91,6 +65,133 @@ auto json_value_to_get_routes_response(json::value const& jv) | |||
| 91 | return json::value_to<get_routes_response>(jv); | 65 | return json::value_to<get_routes_response>(jv); |
| 92 | } | 66 | } |
| 93 | 67 | ||
| 68 | static json::value const json_null = json::value{}; | ||
| 69 | |||
| 70 | auto at_or_null(json::value const& v, std::string_view key) | ||
| 71 | -> json::value const& | ||
| 72 | { | ||
| 73 | if (v.is_null()) | ||
| 74 | return json_null; | ||
| 75 | auto const* obj = v.if_object(); | ||
| 76 | if (!obj) | ||
| 77 | throw std::system_error{make_error_code(json::error::not_object)}; | ||
| 78 | if (auto it = obj->find(key); it != obj->end()) | ||
| 79 | return it->value(); | ||
| 80 | return json_null; | ||
| 81 | } | ||
| 82 | |||
| 83 | auto tag_invoke( | ||
| 84 | json::value_to_tag<notification_cause> const&, json::value const& jv) | ||
| 85 | -> notification_cause | ||
| 86 | { | ||
| 87 | if (jv == "created") | ||
| 88 | return notification_cause::created; | ||
| 89 | if (jv == "updated") | ||
| 90 | return notification_cause::updated; | ||
| 91 | if (jv == "deleted") | ||
| 92 | return notification_cause::deleted; | ||
| 93 | if (jv == "added") | ||
| 94 | return notification_cause::added; | ||
| 95 | if (jv == "removed") | ||
| 96 | return notification_cause::removed; | ||
| 97 | return notification_cause::unknown; | ||
| 98 | } | ||
| 99 | |||
| 100 | auto tag_invoke( | ||
| 101 | json::value_to_tag<notification_item_type> const&, json::value const& jv) | ||
| 102 | -> notification_item_type | ||
| 103 | { | ||
| 104 | if (jv == "trip") | ||
| 105 | return notification_item_type::trip; | ||
| 106 | if (jv == "route") | ||
| 107 | return notification_item_type::route; | ||
| 108 | if (jv == "event") | ||
| 109 | return notification_item_type::event; | ||
| 110 | if (jv == "event_participant") | ||
| 111 | return notification_item_type::event_participant; | ||
| 112 | if (jv == "club_member") | ||
| 113 | return notification_item_type::club_member; | ||
| 114 | if (jv == "authorization") | ||
| 115 | return notification_item_type::authorization; | ||
| 116 | return notification_item_type::unknown; | ||
| 117 | } | ||
| 118 | |||
| 119 | auto tag_invoke( | ||
| 120 | json::value_to_tag<notification_target_collection_type> const&, | ||
| 121 | json::value const& jv) -> notification_target_collection_type | ||
| 122 | { | ||
| 123 | if (jv == "pinned") | ||
| 124 | return notification_target_collection_type::pinned; | ||
| 125 | return notification_target_collection_type::unknown; | ||
| 126 | } | ||
| 127 | |||
| 128 | auto tag_invoke( | ||
| 129 | json::value_to_tag<notification_target_collection> const&, | ||
| 130 | json::value const& jv) -> notification_target_collection | ||
| 131 | { | ||
| 132 | return { | ||
| 133 | .id = json::value_to<std::int64_t>(at_or_null(jv, "id")), | ||
| 134 | .type = json::value_to<notification_target_collection_type>( | ||
| 135 | at_or_null(jv, "type")), | ||
| 136 | .url = json::value_to<std::optional<std::string>>(at_or_null(jv, "url")), | ||
| 137 | }; | ||
| 138 | } | ||
| 139 | |||
| 140 | auto tag_invoke(json::value_to_tag<notification> const&, json::value const& jv) | ||
| 141 | -> notification | ||
| 142 | { | ||
| 143 | return { | ||
| 144 | .user_id = json::value_to<std::int64_t>(at_or_null(jv, "user_id")), | ||
| 145 | .item_type = | ||
| 146 | json::value_to<notification_item_type>(at_or_null(jv, "item_type")), | ||
| 147 | .item_id = | ||
| 148 | json::value_to<std::optional<std::int64_t>>(at_or_null(jv, "item_id")), | ||
| 149 | .item_user_id = json::value_to<std::optional<std::int64_t>>( | ||
| 150 | at_or_null(jv, "item_user_id")), | ||
| 151 | .item_url = | ||
| 152 | json::value_to<std::optional<std::string>>(at_or_null(jv, "item_url")), | ||
| 153 | .action = json::value_to<notification_cause>(at_or_null(jv, "action")), | ||
| 154 | .collection = json::value_to<std::optional<notification_target_collection>>( | ||
| 155 | at_or_null(jv, "collection")), | ||
| 156 | }; | ||
| 157 | } | ||
| 158 | |||
| 159 | auto tag_invoke( | ||
| 160 | json::value_to_tag<webhook_request> const&, json::value const& jv) | ||
| 161 | -> webhook_request | ||
| 162 | { | ||
| 163 | return { | ||
| 164 | .notifications = json::value_to<std::vector<notification>>( | ||
| 165 | at_or_null(jv, "notifications")), | ||
| 166 | }; | ||
| 167 | } | ||
| 168 | |||
| 169 | credentials::credentials( | ||
| 170 | std::string&& api_key, std::string&& api_secret, std::string&& auth_token) | ||
| 171 | : api_key_{std::move(api_key)}, api_secret_{std::move(api_secret)}, | ||
| 172 | auth_token_{std::move(auth_token)} | ||
| 173 | { | ||
| 174 | } | ||
| 175 | |||
| 176 | credentials::~credentials() | ||
| 177 | { | ||
| 178 | OPENSSL_cleanse(api_key_.data(), api_key_.capacity()); | ||
| 179 | OPENSSL_cleanse(api_secret_.data(), api_secret_.capacity()); | ||
| 180 | OPENSSL_cleanse(auth_token_.data(), auth_token_.capacity()); | ||
| 181 | } | ||
| 182 | |||
| 183 | auto credentials::api_key() const -> std::string const& { return api_key_; } | ||
| 184 | |||
| 185 | auto credentials::api_secret() const -> std::string const& | ||
| 186 | { | ||
| 187 | return api_secret_; | ||
| 188 | } | ||
| 189 | |||
| 190 | auto credentials::auth_token() const -> std::string const& | ||
| 191 | { | ||
| 192 | return auth_token_; | ||
| 193 | } | ||
| 194 | |||
| 94 | constexpr std::string host = "ridewithgps.com"; | 195 | constexpr std::string host = "ridewithgps.com"; |
| 95 | 196 | ||
| 96 | // TODO: handle failure appropriately | 197 | // TODO: handle failure appropriately |
| @@ -102,8 +203,8 @@ auto client::get_routes_page(std::size_t page) -> get_routes_response | |||
| 102 | 11, // HTTP 1.1 | 203 | 11, // HTTP 1.1 |
| 103 | }; | 204 | }; |
| 104 | req.set(bhttp::field::host, host); | 205 | req.set(bhttp::field::host, host); |
| 105 | req.set("x-rwgps-api-key", api_key_); | 206 | req.set("x-rwgps-api-key", creds_->api_key()); |
| 106 | req.set("x-rwgps-auth-token", auth_token_); | 207 | req.set("x-rwgps-auth-token", creds_->auth_token()); |
| 107 | 208 | ||
| 108 | auto rsp = hc_.do_request(req); | 209 | auto rsp = hc_.do_request(req); |
| 109 | auto p = json::stream_parser{}; | 210 | auto p = json::stream_parser{}; |
| @@ -114,10 +215,9 @@ auto client::get_routes_page(std::size_t page) -> get_routes_response | |||
| 114 | } | 215 | } |
| 115 | 216 | ||
| 116 | client::client( | 217 | client::client( |
| 117 | net::io_context& ioc, log::logger const& l, std::string api_key, | 218 | net::io_context& ioc, log::logger const& l, |
| 118 | std::string auth_token) | 219 | std::shared_ptr<credentials> creds) |
| 119 | : l_{l.sub("rwgps-client")}, hc_{ioc}, api_key_{std::move(api_key)}, | 220 | : l_{l.sub("rwgps-client")}, hc_{ioc}, creds_{std::move(creds)} |
| 120 | auth_token_{std::move(auth_token)} | ||
| 121 | { | 221 | { |
| 122 | } | 222 | } |
| 123 | 223 | ||