Skip to content

Commit 84a9d03

Browse files
committed
Extract some duplicated code in PushClient
1 parent f249b08 commit 84a9d03

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

src/realm/object-store/sync/push_client.cpp

+13-20
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,29 @@ namespace realm::app {
2727

2828
PushClient::~PushClient() = default;
2929

30-
namespace {
31-
util::UniqueFunction<void(const Response&)>
32-
wrap_completion(util::UniqueFunction<void(util::Optional<AppError>)>&& completion)
30+
void PushClient::request(const std::shared_ptr<User>& user, HttpMethod method, std::string&& body,
31+
util::UniqueFunction<void(util::Optional<AppError>)>&& completion)
3332
{
34-
return [completion = std::move(completion)](const Response& response) {
35-
completion(AppUtils::check_for_errors(response));
36-
};
33+
auto push_route = util::format("/app/%1/push/providers/%2/registration", m_app_id, m_service_name);
34+
std::string route = m_auth_request_client->url_for_path(push_route);
35+
m_auth_request_client->do_authenticated_request(method, std::move(route), std::move(body), user,
36+
RequestTokenType::AccessToken,
37+
[completion = std::move(completion)](const Response& response) {
38+
completion(AppUtils::check_for_errors(response));
39+
});
3740
}
38-
} // anonymous namespace
3941

40-
void PushClient::register_device(const std::string& registration_token, const std::shared_ptr<User>& sync_user,
42+
void PushClient::register_device(const std::string& registration_token, const std::shared_ptr<User>& user,
4143
util::UniqueFunction<void(util::Optional<AppError>)>&& completion)
4244
{
43-
auto push_route = util::format("/app/%1/push/providers/%2/registration", m_app_id, m_service_name);
44-
std::string route = m_auth_request_client->url_for_path(push_route);
45-
4645
bson::BsonDocument args{{"registrationToken", registration_token}};
47-
m_auth_request_client->do_authenticated_request(HttpMethod::put, std::move(route), bson::Bson(args).to_string(),
48-
sync_user, RequestTokenType::AccessToken,
49-
wrap_completion(std::move(completion)));
46+
request(user, HttpMethod::put, bson::Bson(args).to_string(), std::move(completion));
5047
}
5148

52-
void PushClient::deregister_device(const std::shared_ptr<User>& sync_user,
49+
void PushClient::deregister_device(const std::shared_ptr<User>& user,
5350
util::UniqueFunction<void(util::Optional<AppError>)>&& completion)
5451
{
55-
auto push_route = util::format("/app/%1/push/providers/%2/registration", m_app_id, m_service_name);
56-
57-
m_auth_request_client->do_authenticated_request(HttpMethod::del, m_auth_request_client->url_for_path(push_route),
58-
"", sync_user, RequestTokenType::AccessToken,
59-
wrap_completion(std::move(completion)));
52+
request(user, HttpMethod::del, "", std::move(completion));
6053
}
6154

6255
} // namespace realm::app

src/realm/object-store/sync/push_client.hpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616
//
1717
////////////////////////////////////////////////////////////////////////////
1818

19-
#ifndef PUSH_CLIENT_HPP
20-
#define PUSH_CLIENT_HPP
19+
#ifndef REALM_OS_PUSH_CLIENT_HPP
20+
#define REALM_OS_PUSH_CLIENT_HPP
2121

22+
#include <realm/object-store/sync/generic_network_transport.hpp>
2223
#include <realm/util/functional.hpp>
2324

24-
#include <memory>
25-
#include <optional>
26-
#include <string>
27-
2825
namespace realm::app {
2926
class AuthRequestClient;
3027
class User;
@@ -49,25 +46,28 @@ class PushClient {
4946

5047
/// Register a device for push notifications.
5148
/// @param registration_token GCM registration token for the device.
52-
/// @param sync_user The sync user requesting push registration.
49+
/// @param user The sync user requesting push registration.
5350
/// @param completion An error will be returned should something go wrong.
54-
void register_device(const std::string& registration_token, const std::shared_ptr<User>& sync_user,
51+
void register_device(const std::string& registration_token, const std::shared_ptr<User>& user,
5552
util::UniqueFunction<void(std::optional<AppError>)>&& completion);
5653

5754

5855
/// Deregister a device for push notificatons, no token or device id needs to be passed
5956
/// as it is linked to the user in MongoDB Realm Cloud.
60-
/// @param sync_user The sync user requesting push degistration.
57+
/// @param user The sync user requesting push degistration.
6158
/// @param completion An error will be returned should something go wrong.
62-
void deregister_device(const std::shared_ptr<User>& sync_user,
59+
void deregister_device(const std::shared_ptr<User>& user,
6360
util::UniqueFunction<void(std::optional<AppError>)>&& completion);
6461

6562
private:
6663
std::string m_service_name;
6764
std::string m_app_id;
6865
std::shared_ptr<AuthRequestClient> m_auth_request_client;
66+
67+
void request(const std::shared_ptr<User>& user, HttpMethod method, std::string&& body,
68+
util::UniqueFunction<void(std::optional<AppError>)>&& completion);
6969
};
7070

7171
} // namespace realm::app
7272

73-
#endif /* PUSH_CLIENT_HPP */
73+
#endif /* REALM_OS_PUSH_CLIENT_HPP */

0 commit comments

Comments
 (0)