The Hyphen Toggle OpenFeature Provider is an OpenFeature provider implementation for the Hyphen Toggle platform using Swift. It enables feature flag evaluation using the OpenFeature standard.
Using the Swift Package Manager: either through Xcode > File > Swift Packages > Add Package Dependency... and enter this repo URL (including the .git
extension), , then choose Toggle
target. Or add the line .package(url: "https://github.com/hyphen/hyphen-openfeature-swift", from: "0.2.0")
in the dependencies
section of your Package.swift
file.
To integrate the Hyphen Toggle provider into your application, follow these steps:
- Configure the provider with your
publicKey
and provider options. - Register the provider with OpenFeature.
import Toggle
import OpenFeature
let configuration = HyphenConfiguration(using: "project-public-key",
application: "hyphen-example-app",
environment: "development")
let provider = HyphenProvider(using: Self.configuration)
await OpenFeatureAPI.shared.setProviderAndWait(provider: provider)
- Configure the context needed for feature targeting evaluations, incorporating user or application context.
let provider = HyphenProvider(using: configuration)
let context = hyphenEvaluationContext(
targetingKey: "user-123",
values: [
"CustomAttributes": .structure([
"theme": .string("dark"),
"betaAccess": .boolean(true)
]),
"User": .structure([
"Email": .string("[email protected]"),
"Name": .string("Tester"),
"CustomAttributes": .structure([
"subscription": .string("pro")
])
])
]
)
await OpenFeatureAPI.shared.setProviderAndWait(provider: provider, initialContext: context)
let client = OpenFeatureAPI.shared.getClient()
let flagDetails: FlagEvaluationDetails<Bool> = client.getDetails(key: ToggleKey.bool, defaultValue: false)
Option | Type | Required | Description |
---|---|---|---|
Public Key |
string | Yes | The public key from the Hyphen project |
Application |
string | Yes | The application id or alternate id |
Environment |
string | Yes | The environment identifier for the Hyphen project (project environment ID or alternateId). |
HorizonUrls |
string[] | No | Hyphen Horizon URLs for fetching flags |
EnableToggleUsage |
bool? | No | Enable/disable telemetry (default: True). |
Property | Type | Default | Description |
---|---|---|---|
useCellularAccess |
bool | true | Use Cellular Access to retrieve toggle evaluations |
timeout |
number | 10 | Timeout in seconds for retrieving the toggle evaluations |
maxRetries |
number | 3 | The number of times we will retry the retrieval of toggles |
retryDelay |
number | 3 | The amount of time we will wait between requests |
cacheExpiration |
number | 900 | ttl for the evaluation response |
Provide a HyphenEvaluationContext
to pass contextual data for feature evaluation.
Field | Type | Required | Description |
---|---|---|---|
TargetingKey |
string | Yes | Caching evaluation key |
CustomAttributes |
Dictionary<string, object> | No | Additional context information |
User |
UserContext | No | User-specific information |
User.Id |
string | No | Unique identifier of the user |
User.Email |
string | No | Email address of the user |
User.Name |
string | No | Name of the user |
User.CustomAttributes |
Dictionary<string, object> | No | Custom attributes specific to the user |
This project is licensed under the MIT License. See the LICENSE file for full details.
- OpenFeature is an open specification that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool or in-house solution.
- License: APACHE 2.0
- Repo: https://github.com/open-feature/swift-sdk