Skip to content

Commit

Permalink
Extract some duplicated code in PushClient
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Apr 9, 2024
1 parent f249b08 commit 84a9d03
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
33 changes: 13 additions & 20 deletions src/realm/object-store/sync/push_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,29 @@ namespace realm::app {

PushClient::~PushClient() = default;

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

void PushClient::register_device(const std::string& registration_token, const std::shared_ptr<User>& sync_user,
void PushClient::register_device(const std::string& registration_token, const std::shared_ptr<User>& user,
util::UniqueFunction<void(util::Optional<AppError>)>&& completion)
{
auto push_route = util::format("/app/%1/push/providers/%2/registration", m_app_id, m_service_name);
std::string route = m_auth_request_client->url_for_path(push_route);

bson::BsonDocument args{{"registrationToken", registration_token}};
m_auth_request_client->do_authenticated_request(HttpMethod::put, std::move(route), bson::Bson(args).to_string(),
sync_user, RequestTokenType::AccessToken,
wrap_completion(std::move(completion)));
request(user, HttpMethod::put, bson::Bson(args).to_string(), std::move(completion));
}

void PushClient::deregister_device(const std::shared_ptr<User>& sync_user,
void PushClient::deregister_device(const std::shared_ptr<User>& user,
util::UniqueFunction<void(util::Optional<AppError>)>&& completion)
{
auto push_route = util::format("/app/%1/push/providers/%2/registration", m_app_id, m_service_name);

m_auth_request_client->do_authenticated_request(HttpMethod::del, m_auth_request_client->url_for_path(push_route),
"", sync_user, RequestTokenType::AccessToken,
wrap_completion(std::move(completion)));
request(user, HttpMethod::del, "", std::move(completion));
}

} // namespace realm::app
22 changes: 11 additions & 11 deletions src/realm/object-store/sync/push_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
//
////////////////////////////////////////////////////////////////////////////

#ifndef PUSH_CLIENT_HPP
#define PUSH_CLIENT_HPP
#ifndef REALM_OS_PUSH_CLIENT_HPP
#define REALM_OS_PUSH_CLIENT_HPP

#include <realm/object-store/sync/generic_network_transport.hpp>
#include <realm/util/functional.hpp>

#include <memory>
#include <optional>
#include <string>

namespace realm::app {
class AuthRequestClient;
class User;
Expand All @@ -49,25 +46,28 @@ class PushClient {

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


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

private:
std::string m_service_name;
std::string m_app_id;
std::shared_ptr<AuthRequestClient> m_auth_request_client;

void request(const std::shared_ptr<User>& user, HttpMethod method, std::string&& body,
util::UniqueFunction<void(std::optional<AppError>)>&& completion);
};

} // namespace realm::app

#endif /* PUSH_CLIENT_HPP */
#endif /* REALM_OS_PUSH_CLIENT_HPP */

0 comments on commit 84a9d03

Please sign in to comment.