-
Notifications
You must be signed in to change notification settings - Fork 2
Global API #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Global API #54
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
240ac77
feat: add global context manager
NeaguGeorgiana23 8563306
feat: add client api and boolean evaluation
NeaguGeorgiana23 04b1de2
feat: add OpenFeature API singleton and update ProviderRepository shu…
NeaguGeorgiana23 06b0169
apply gemini suggestions.
NeaguGeorgiana23 27bba7d
Merge branch 'main' into feature/split-3-api
NeaguGeorgiana23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #include "openfeature/openfeature_api.h" | ||
|
|
||
| #include "openfeature/client_api.h" | ||
| #include "openfeature/global_context_manager.h" | ||
|
|
||
| namespace openfeature { | ||
|
|
||
| OpenFeatureAPI::OpenFeatureAPI() { | ||
| // provider_repository_ is automatically constructed. | ||
| // It guarantees a NoopProvider is set by default. | ||
| // TODO: init hooks & events. | ||
| } | ||
|
|
||
| OpenFeatureAPI& OpenFeatureAPI::GetInstance() { | ||
| static OpenFeatureAPI instance; | ||
| return instance; | ||
| } | ||
|
|
||
| void OpenFeatureAPI::SetProvider(std::shared_ptr<FeatureProvider> provider) { | ||
| provider_repository_.SetProvider( | ||
| provider, | ||
| GlobalContextManager::GetInstance().GetGlobalEvaluationContext(), false); | ||
| } | ||
|
|
||
| void OpenFeatureAPI::SetProvider(std::string_view domain, | ||
| std::shared_ptr<FeatureProvider> provider) { | ||
| provider_repository_.SetProvider( | ||
| domain, provider, | ||
| GlobalContextManager::GetInstance().GetGlobalEvaluationContext(), false); | ||
| } | ||
|
|
||
| void OpenFeatureAPI::SetProviderAndWait( | ||
| std::shared_ptr<FeatureProvider> provider) { | ||
| provider_repository_.SetProvider( | ||
| provider, | ||
| GlobalContextManager::GetInstance().GetGlobalEvaluationContext(), true); | ||
| } | ||
|
|
||
| void OpenFeatureAPI::SetProviderAndWait( | ||
| std::string_view domain, std::shared_ptr<FeatureProvider> provider) { | ||
| provider_repository_.SetProvider( | ||
| domain, provider, | ||
| GlobalContextManager::GetInstance().GetGlobalEvaluationContext(), true); | ||
| } | ||
|
|
||
| std::shared_ptr<FeatureProvider> OpenFeatureAPI::GetProvider( | ||
| std::string_view domain) const { | ||
| return provider_repository_.GetProvider(domain); | ||
| } | ||
|
|
||
| std::shared_ptr<Client> OpenFeatureAPI::GetClient() { return GetClient(""); } | ||
|
|
||
| std::shared_ptr<Client> OpenFeatureAPI::GetClient(std::string_view domain) { | ||
| auto client = std::make_shared<ClientAPI>(provider_repository_, domain); | ||
| return client; | ||
| } | ||
|
|
||
| void OpenFeatureAPI::SetEvaluationContext(const EvaluationContext& ctx) { | ||
| GlobalContextManager::GetInstance().SetGlobalEvaluationContext(ctx); | ||
| } | ||
|
|
||
| EvaluationContext OpenFeatureAPI::GetEvaluationContext() const { | ||
| return GlobalContextManager::GetInstance().GetGlobalEvaluationContext(); | ||
| } | ||
|
|
||
| Metadata OpenFeatureAPI::GetProviderMetadata(std::string_view domain) const { | ||
| std::shared_ptr<FeatureProvider> provider = | ||
| provider_repository_.GetProvider(domain); | ||
| if (provider) { | ||
| return provider->GetMetadata(); | ||
| } | ||
| return Metadata(); // Return empty metadata if provider not found | ||
| } | ||
|
|
||
| ProviderStatus OpenFeatureAPI::GetProviderStatus( | ||
| std::string_view domain) const { | ||
| return provider_repository_.GetProviderStatus(domain); | ||
| } | ||
|
|
||
| void OpenFeatureAPI::Shutdown() { provider_repository_.Shutdown(); } | ||
|
|
||
| } // namespace openfeature |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #ifndef CPP_SDK_INCLUDE_OPENFEATURE_OPENFEATURE_API_H_ | ||
| #define CPP_SDK_INCLUDE_OPENFEATURE_OPENFEATURE_API_H_ | ||
|
|
||
| #include <memory> | ||
| #include <shared_mutex> | ||
| #include <string_view> | ||
|
|
||
| #include "openfeature/client.h" | ||
| #include "openfeature/evaluation_context.h" | ||
| #include "openfeature/global_context_manager.h" | ||
| #include "openfeature/metadata.h" | ||
| #include "openfeature/openfeature.h" | ||
| #include "openfeature/provider.h" | ||
| #include "openfeature/provider_repository.h" | ||
|
|
||
| namespace openfeature { | ||
|
|
||
| // A global singleton which holds base configuration for the OpenFeature | ||
| // library. | ||
| class OpenFeatureAPI : public OpenFeature { | ||
| public: | ||
| ~OpenFeatureAPI() = default; | ||
|
|
||
| // Get the singleton instance of the OpenFeatureAPI. | ||
| static OpenFeatureAPI& GetInstance(); | ||
|
|
||
| OpenFeatureAPI(const OpenFeatureAPI&) = delete; | ||
| OpenFeatureAPI& operator=(const OpenFeatureAPI&) = delete; | ||
|
|
||
| // Set the default provider. | ||
| void SetProvider(std::shared_ptr<FeatureProvider> provider) override; | ||
|
|
||
| // Set a provider for a specific domain. | ||
| void SetProvider(std::string_view domain, | ||
| std::shared_ptr<FeatureProvider> provider) override; | ||
|
|
||
| // Set the default provider and blocks until it successfully initializes. | ||
| void SetProviderAndWait(std::shared_ptr<FeatureProvider> provider) override; | ||
|
|
||
| // Set a named provider and blocks until it successfully initializes. | ||
| void SetProviderAndWait(std::string_view domain, | ||
| std::shared_ptr<FeatureProvider> provider) override; | ||
| // If the domain is empty then GetProvider returns the default provider | ||
| // otherwise it returns the provider for the domain. If this domain has no | ||
| // provider bound, it returns the default provider. | ||
| std::shared_ptr<FeatureProvider> GetProvider( | ||
| std::string_view domain = "") const override; | ||
|
|
||
| // Gets a client for the default domain. | ||
| std::shared_ptr<Client> GetClient() override; | ||
|
|
||
| // Gets a client for a named domain. | ||
| std::shared_ptr<Client> GetClient(std::string_view domain) override; | ||
|
|
||
| // Sets the global evaluation context. | ||
| void SetEvaluationContext(const EvaluationContext& ctx) override; | ||
|
|
||
| // Gets the global evaluation context. | ||
| EvaluationContext GetEvaluationContext() const override; | ||
|
|
||
| // Get metadata about the default provider if domain is empty | ||
| // or about a named provider if domain is provided. | ||
| Metadata GetProviderMetadata(std::string_view domain = "") const override; | ||
|
|
||
| // Fetches the status of a provider for a domain. If the domain is not set or | ||
| // not found, it returns the default provider status. | ||
| ProviderStatus GetProviderStatus(std::string_view domain = "") const override; | ||
|
|
||
| // Shuts down all providers and resets the API to its initial state. | ||
| void Shutdown() override; | ||
|
|
||
| // TODO: Add methods to add and get Hooks. | ||
| // TODO: Add overload function for "GetClient()" to accept "Evaluation | ||
| // Options" | ||
|
|
||
| private: | ||
| ProviderRepository provider_repository_; | ||
|
|
||
| OpenFeatureAPI(); | ||
| }; | ||
|
|
||
| } // namespace openfeature | ||
|
|
||
| #endif // CPP_SDK_INCLUDE_OPENFEATURE_OPENFEATURE_API_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Shutdown()method re-initializes the repository with aNoopProvider. While this is correct for explicit shutdowns via the API, it's inefficient whenShutdown()is called from the~ProviderRepository()destructor, as the object is about to be destroyed.A better approach would be to avoid this re-initialization in the destructor. This could be done by parameterizing the
Shutdownmethod. For example:Since this change spans multiple files and locations, I'm not providing a direct code suggestion. Please consider this refactoring to improve efficiency and adhere to best practices for destructors.