Skip to content

Commit

Permalink
Add support for updating the base url (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaafanador3 authored May 24, 2024
1 parent e718cc8 commit 322abf8
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ jobs:
- uses: ammaraskar/gcc-problem-matcher@master

- name: Configure
run: cmake --preset ${{ matrix.preset }} -DCMAKE_VERBOSE_MAKEFILE=${RUNNER_DEBUG:-OFF}
run: cmake --preset ${{ matrix.preset }} -DCMAKE_VERBOSE_MAKEFILE=${RUNNER_DEBUG:-OFF} -DREALM_ENABLE_EXPERIMENTAL=1

- name: Compile
run: cmake --build --preset ${{ matrix.preset }} --config ${{ matrix.configuration }}
Expand Down Expand Up @@ -345,7 +345,7 @@ jobs:
- uses: ammaraskar/gcc-problem-matcher@master

- name: Configure
run: cmake --preset linux -DCMAKE_VERBOSE_MAKEFILE=${RUNNER_DEBUG:-OFF}
run: cmake --preset linux -DCMAKE_VERBOSE_MAKEFILE=${RUNNER_DEBUG:-OFF} -DREALM_ENABLE_EXPERIMENTAL=1

- name: Compile
run: cmake --build --preset linux --config ${{ matrix.configuration }}
Expand Down Expand Up @@ -431,7 +431,7 @@ jobs:
- uses: ammaraskar/msvc-problem-matcher@master

- name: Configure
run: cmake --preset windows-x64 -DENABLE_STATIC=1
run: cmake --preset windows-x64 -DENABLE_STATIC=1 -DREALM_ENABLE_EXPERIMENTAL=1

- name: Compile
run: cmake --build --preset windows-x64 --config ${{ matrix.configuration }}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Enhancements
* Updated default base URL to be `https://services.cloud.mongodb.com` to support the new domains (was `https://realm.mongodb.com`)
* Added support for updating Atlas Device Sync's base url, in case the need to roam between servers (cloud and/or edge server). Add `-DREALM_ENABLE_EXPERIMENTAL=1` to your CMake command when generating the build to enable this feature.

### Compatibility
* Fileformat: Generates files with format v24. Reads and automatically upgrade from fileformat v10.
Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ set(REALM_NO_TESTS)
set(REALM_BUILD_LIB_ONLY)
set(REALM_INSTALL_LIBEXECDIR)
set(REALM_ENABLE_ENCRYPTION 1)
option(REALM_ENABLE_EXPERIMENTAL "Enable experimental Realm API's" OFF)
if(REALM_ENABLE_EXPERIMENTAL)
add_compile_definitions(REALM_ENABLE_EXPERIMENTAL)
endif()

set(CMAKE_DEBUG_POSTFIX "-dbg")

Expand All @@ -45,6 +49,8 @@ add_compile_definitions(REALM_ENABLE_ENCRYPTION)
add_compile_definitions(REALM_INSTALL_LIBEXECDIR)
add_compile_definitions(REALM_BUILD_LIB_ONLY)

configure_file(src/cpprealm/util/config.h.in include/cpprealm/util/config.h)

add_subdirectory(src)
target_include_directories(${PROJECT_NAME}
PRIVATE
Expand Down Expand Up @@ -158,6 +164,9 @@ install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION cmake
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/cpprealm/util/config.h
DESTINATION include/cpprealm/util
COMPONENT devel)

if (BUILD_SHARED_LIBS)
set_target_properties(cpprealm
Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ let cppSdkTarget: Target = .target(
exclude: [
"src/cpprealm/internal/curl",
"src/cpprealm/internal/network",
"src/cpprealm/util/config.in.h",
"realm-core"
],
sources: ["src"],
Expand Down
12 changes: 12 additions & 0 deletions include/cpprealm/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ class App {
void clear_cached_apps();
std::optional<App> get_cached_app(const std::string& app_id, const std::optional<std::string>& base_url);
std::string get_base_url() const;

#ifdef REALM_ENABLE_EXPERIMENTAL
/**
Updates the base url used by Atlas device sync, in case the need to roam between
servers (cloud and/or edge server).
@param name The new base url to connect to.
@return A void future once the operation has completed.
This handler is executed on the thread the method was called from.
*/
[[nodiscard]] std::future<void> update_base_url(std::optional<std::string> base_url) const;
#endif
private:
std::shared_ptr<app::App> m_app;
App(std::shared_ptr<app::App>&& a) : m_app(std::move(a)) { }
Expand Down
4 changes: 4 additions & 0 deletions include/cpprealm/sdk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#ifndef CPPREALM_SDK_HPP
#define CPPREALM_SDK_HPP

#if __has_include(<cpprealm/config.h>)
#include <cpprealm/config.h>
#endif

#include <utility>

#include <cpprealm/bson.hpp>
Expand Down
14 changes: 14 additions & 0 deletions src/cpprealm/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,18 @@ namespace realm {
return std::nullopt;
}

#ifdef REALM_ENABLE_EXPERIMENTAL
[[nodiscard]] std::future<void> App::update_base_url(std::optional<std::string> base_url) const {
std::promise<void> p;
std::future<void> f = p.get_future();
m_app->update_base_url(base_url, ([p = std::move(p)](auto err) mutable {
if (err) {
p.set_exception(std::make_exception_ptr(app_error(std::move(*err))));
} else {
p.set_value();
}
}));
return f;
}
#endif
}
1 change: 1 addition & 0 deletions src/cpprealm/util/config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#cmakedefine01 REALM_ENABLE_EXPERIMENTAL
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ endif()

if (NOT BUILD_FROM_PACKAGE_MANAGER)
target_compile_definitions(cpprealm_sync_tests PUBLIC CPPREALM_ENABLE_SYNC_TESTS)
if (REALM_ENABLE_EXPERIMENTAL)
target_compile_definitions(cpprealm_sync_tests PRIVATE REALM_ENABLE_EXPERIMENTAL)
endif()
endif()

if (DEFINED USES_CONAN)
Expand Down
5 changes: 2 additions & 3 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ int main(int argc, char *argv[]) {
}

#ifdef CPPREALM_ENABLE_SYNC_TESTS
std::string baas_api_key = getenv("APIKEY");
std::optional<Admin::baas_manager> baas_manager;
if (!baas_api_key.empty()) {
baas_manager.emplace(baas_api_key);
if (const char* api_key = getenv("APIKEY")) {
baas_manager.emplace(std::string(api_key));
baas_manager->start();
auto url = baas_manager->wait_for_container();
Admin::Session::shared().prepare(url);
Expand Down
21 changes: 21 additions & 0 deletions tests/sync/app_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ TEST_CASE("app", "[app]") {
CHECK(with_url_provided_app.get_base_url() == "https://foobar.com");
}

#ifdef REALM_ENABLE_EXPERIMENTAL
SECTION("update_base_url") {
auto no_url_provided_app = realm::App(realm::App::configuration({"NA"}));
CHECK(no_url_provided_app.get_base_url() == "https://services.cloud.mongodb.com");
REQUIRE_THROWS_AS(no_url_provided_app.update_base_url("https://foobar.com").get(), realm::app_error);
CHECK(no_url_provided_app.get_base_url() == "https://services.cloud.mongodb.com");
REQUIRE_THROWS(no_url_provided_app.update_base_url("asdfghjkl").get());
CHECK(no_url_provided_app.get_base_url() == "https://services.cloud.mongodb.com");

CHECK(app.get_base_url() == Admin::Session::shared().base_url());
REQUIRE_THROWS_AS(app.update_base_url("https://foobar.com").get(), realm::app_error);
CHECK(app.get_base_url() == Admin::Session::shared().base_url());
// Cannot be changed because app id not available in atlas
REQUIRE_THROWS_AS(app.update_base_url(std::nullopt).get(), realm::app_error);
CHECK(app.get_base_url() == Admin::Session::shared().base_url());
// This succeeds but the url is the same
app.update_base_url(Admin::Session::shared().base_url()).get();
CHECK(app.get_base_url() == Admin::Session::shared().base_url());
}
#endif

SECTION("get_current_user") {
auto user = app.login(realm::App::credentials::anonymous()).get();

Expand Down

0 comments on commit 322abf8

Please sign in to comment.