Realm C++ SDK Version v2.2.0

app.hpp

1
2//
3// Copyright 2022 Realm Inc.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
18
19#ifndef CPPREALM_APP_HPP
20#define CPPREALM_APP_HPP
21
22#include <cpprealm/bson.hpp>
23#include <cpprealm/db.hpp>
24
25#include <cpprealm/internal/bridge/realm.hpp>
26#include <cpprealm/internal/bridge/sync_error.hpp>
27#include <cpprealm/internal/bridge/sync_manager.hpp>
28#include <cpprealm/internal/bridge/sync_session.hpp>
29#include <cpprealm/internal/bridge/utils.hpp>
30#include <cpprealm/networking/http.hpp>
31#include <cpprealm/networking/websocket.hpp>
32
33#include <future>
34#include <utility>
35
36namespace realm {
37
38 using proxy_config = sync_config::proxy_config;
39 using sync_session = internal::bridge::sync_session;
40
41 namespace app {
42 class App;
43 class User;
44 struct AppError;
45 }// namespace app
46 namespace internal::bridge {
47 struct sync_error;
48 }
49
50// MARK: User
51
52// Represents an error state from the server.
53struct app_error {
54 app_error() = delete;
55 app_error(const app_error& other);
56 app_error& operator=(const app_error& other) ;
57 app_error(app_error&& other);
58 app_error& operator=(app_error&& other);
59 ~app_error();
60 app_error(realm::app::AppError&& error); //NOLINT(google-explicit-constructor)
61
62 [[nodiscard]] std::string_view message() const;
63
64 [[nodiscard]] std::string_view link_to_server_logs() const;
65
66 [[nodiscard]] bool is_json_error() const;
67
68 [[nodiscard]] bool is_service_error() const;
69
70 [[nodiscard]] bool is_http_error() const;
71
72 [[nodiscard]] bool is_custom_error() const;
73
74 [[nodiscard]] bool is_client_error() const;
75private:
76#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES
77 internal::bridge::storage::AppError m_error[1];
78#else
79 std::shared_ptr<realm::app::AppError> m_error;
80#endif
81};
82
92struct user {
93 user() = default;
94 user(const user&) = default;
95 user(user&&) = default;
96 user& operator=(const user&) = default;
97 user& operator=(user&&) = default;
98 explicit user(std::shared_ptr<app::User> user);
99 bool is_logged_in() const;
100
104 enum class state : uint8_t {
105 logged_out,
106 logged_in,
107 removed,
108 };
109
114 [[nodiscard]] std::string identifier() const;
115
119 [[nodiscard]] state state() const;
120
127 [[nodiscard]] std::string access_token() const;
128
135 [[nodiscard]] std::string refresh_token() const;
136
137 struct internal::bridge::sync_manager sync_manager() const;
138
139 [[nodiscard]] db_config flexible_sync_configuration() const;
140
148 void log_out(std::function<void(std::optional<app_error>)>&& callback) const;
149
150 [[nodiscard]] std::future<void> log_out() const;
151
152
153 [[deprecated("Replaced by `get_custom_data()`. This method will be removed in a future release.")]]
154 [[nodiscard]] std::optional<std::string> custom_data() const;
155
160 [[nodiscard]] std::optional<bsoncxx::document> get_custom_data() const;
161
170 [[deprecated("This function is deprecated and will replaced by the BSON based call_function API.")]]
171 void call_function(const std::string& name, const std::string& args_ejson,
172 std::function<void(std::optional<std::string>, std::optional<app_error>)> callback) const;
173
182 [[deprecated("This function is deprecated and will replaced by the BSON based call_function API.")]]
183 [[nodiscard]] std::future<std::optional<std::string>> call_function(const std::string& name,
184 const std::string& args_ejson) const;
185
194 void call_function(const std::string& name, const std::vector<bsoncxx>& args_bson,
195 std::function<void(std::optional<bsoncxx>, std::optional<app_error>)> callback) const;
196
204 [[nodiscard]] std::future<std::optional<bsoncxx>> call_function(const std::string& name, const std::vector<bsoncxx>& args_bson) const;
205
209 void refresh_custom_user_data(std::function<void(std::optional<app_error>)> callback);
210
214 [[nodiscard]] std::future<void> refresh_custom_user_data() const;
215
216 std::shared_ptr<app::User> m_user;
217};
218
219bool operator==(const user& lhs, const user& rhs);
220bool operator!=(const user& lhs, const user& rhs);
221
222namespace app {
223 struct AppCredentials;
224}
225
226class App {
227public:
238 std::string app_id;
240 std::optional<std::string> base_url;
242 std::optional<std::string> path;
243 [[deprecated("Network options must be supplied via custom network implementations.")]]
244 std::optional<std::map<std::string, std::string>> custom_http_headers;
246 std::optional<std::array<char, 64>> metadata_encryption_key;
248 bool enable_caching = true;
249 [[deprecated("Network options must be supplied via custom network implementations.")]]
250 std::optional<sync_config::proxy_config> proxy_configuration;
257 std::shared_ptr<networking::http_transport_client> http_transport_client;
261 std::shared_ptr<networking::sync_socket_provider> sync_socket_provider;
262
263 };
264
265
266 App(const configuration&);
267
268 struct credentials {
269 static credentials anonymous();
270 static credentials api_key(const std::string& key);
271 static credentials facebook(const std::string& access_token);
272 static credentials apple(const std::string& id_token);
273 static credentials google_auth_code(const std::string& auth_code);
274 static credentials google_id_token(const std::string& id_token);
275 static credentials custom(const std::string& token);
276 static credentials username_password(const std::string& username, const std::string& password);
277 static credentials function(const std::string& payload);
278 credentials();
279 credentials(const credentials& other) ;
280 credentials& operator=(const credentials& other) ;
281 credentials(credentials&& other);
282 credentials& operator=(credentials&& other);
283 ~credentials();
284 private:
285 credentials(app::AppCredentials&& credentials) noexcept;
286 operator app::AppCredentials() const;
287 friend class App;
288
289#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES
290 internal::bridge::storage::AppCredentials m_credentials[1];
291#else
292 std::shared_ptr<app::AppCredentials> m_credentials;
293#endif
294 };
295
296 std::future<void> register_user(const std::string& username, const std::string& password);
297 std::future<user> login(const credentials &credentials);
298 void login(const credentials& credentials, std::function<void(user, std::optional<app_error>)>&& callback);
299 [[nodiscard]] internal::bridge::sync_manager get_sync_manager() const;
300 [[nodiscard]] std::optional<user> get_current_user() const;
301 void clear_cached_apps();
302 std::optional<App> get_cached_app(const std::string& app_id, const std::optional<std::string>& base_url);
303 std::string get_base_url() const;
304
305#ifdef REALM_ENABLE_EXPERIMENTAL
314 [[nodiscard]] std::future<void> update_base_url(std::string base_url) const;
315#endif
316
317 [[nodiscard]] std::string path_for_realm(const realm::sync_config&) const;
318private:
319 std::shared_ptr<app::App> m_app;
320 App(std::shared_ptr<app::App>&& a) : m_app(std::move(a)) { }
321};
322
323}
324#endif /* CPPREALM_APP_HPP */
Definition: app.hpp:226
Definition: app.hpp:236
bool enable_caching
Caches an App and its configuration for a given App ID. On by default.
Definition: app.hpp:248
std::optional< std::string > base_url
A custom base URL to request against. If not set or set to nil, the default base url for app services...
Definition: app.hpp:240
std::optional< std::string > path
Custom location for Realm files.
Definition: app.hpp:242
std::optional< std::array< char, 64 > > metadata_encryption_key
Custom encryption key for the metadata Realm.
Definition: app.hpp:246
std::shared_ptr< networking::sync_socket_provider > sync_socket_provider
Definition: app.hpp:261
std::shared_ptr< networking::http_transport_client > http_transport_client
Definition: app.hpp:257
std::string app_id
The App ID for your Atlas Device Sync Application.
Definition: app.hpp:238
Definition: app.hpp:268
Definition: app.hpp:53
Definition: sync_manager.hpp:33
Definition: app.hpp:92
std::string refresh_token() const
Definition: app.cpp:181
void call_function(const std::string &name, const std::string &args_ejson, std::function< void(std::optional< std::string >, std::optional< app_error >)> callback) const
Definition: app.cpp:245
std::optional< bsoncxx::document > get_custom_data() const
Definition: app.cpp:236
state
Definition: app.hpp:104
std::string access_token() const
Definition: app.cpp:170
std::future< void > refresh_custom_user_data() const
Definition: app.cpp:311
std::string identifier() const
Definition: app.cpp:151