Skip to content

Commit f3bd87d

Browse files
authored
feat: Swift error strategy (#721)
* Release flipt-client-swift-v0.1.0 Signed-off-by: Mark Phelps <209477+markphelps@users.noreply.github.com> * feat(swift): add error strategy support to swift sdk Signed-off-by: Mark Phelps <209477+markphelps@users.noreply.github.com> --------- Signed-off-by: Mark Phelps <209477+markphelps@users.noreply.github.com>
1 parent 4f5a77a commit f3bd87d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Add the following to your `Package.swift` file:
1212

1313
```swift
1414
dependencies: [
15-
.package(url: "https://github.com/flipt-io/flipt-client-swift.git", from: "0.0.x")
15+
.package(url: "https://github.com/flipt-io/flipt-client-swift.git", from: "1.x.x")
1616
]
1717
```
1818

@@ -86,6 +86,25 @@ defer {
8686
}
8787
```
8888

89+
### Client Options
90+
91+
The `FliptClient` initializer accepts several options that can be used to configure the client. The available options are:
92+
93+
- `namespace`: The namespace to fetch flag state from. If not provided, the client will default to the `default` namespace.
94+
- `url`: The URL of the upstream Flipt instance. If not provided, the client will default to `http://localhost:8080`.
95+
- `updateInterval`: The interval (in seconds) in which to fetch new flag state. If not provided, the client will default to 120 seconds.
96+
- `authentication`: The authentication strategy to use when communicating with the upstream Flipt instance. If not provided, the client will default to no authentication. See the [Authentication](#authentication) section for more information.
97+
- `reference`: The [reference](https://docs.flipt.io/guides/user/using-references) to use when fetching flag state. If not provided, reference will not be used.
98+
- `fetchMode`: The fetch mode to use when fetching flag state. If not provided, the client will default to polling.
99+
- `errorStrategy`: The error strategy to use when fetching flag state. If not provided, the client will default to `fail`. See the [Error Strategies](#error-strategies) section for more information.
100+
101+
### Error Strategies
102+
103+
The `FliptClient` supports the following error strategies:
104+
105+
- `fail`: The client will throw an error if the flag state cannot be fetched. This is the default behavior.
106+
- `fallback`: The client will maintain the last known good state and use that state for evaluation in case of an error.
107+
89108
### Authentication
90109

91110
The `FliptClient` supports the following authentication strategies:

Sources/FliptClient/FliptClient.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@ public class FliptClient {
99
private var ref: String = ""
1010
private var updateInterval: Int = 0
1111
private var fetchMode: FetchMode = .polling
12+
private var errorStrategy: ErrorStrategy = .fail
1213

1314
public init(namespace: String = "default",
1415
url: String = "",
1516
authentication: Authentication? = nil,
1617
ref: String = "",
1718
updateInterval: Int = 120,
18-
fetchMode: FetchMode = .polling) throws {
19+
fetchMode: FetchMode = .polling,
20+
errorStrategy: ErrorStrategy = .fail) throws {
1921
self.namespace = namespace
2022
self.url = url
2123
self.authentication = authentication
2224
self.ref = ref
2325
self.updateInterval = updateInterval
2426
self.fetchMode = fetchMode
27+
self.errorStrategy = errorStrategy
2528

2629
let clientOptions = ClientOptions(
2730
url: url,
2831
authentication: authentication,
2932
updateInterval: updateInterval,
3033
reference: ref,
31-
fetchMode: fetchMode
34+
fetchMode: fetchMode,
35+
errorStrategy: errorStrategy
3236
)
3337

3438
guard let jsonData = try? JSONEncoder().encode(clientOptions) else {
@@ -240,12 +244,18 @@ public enum FetchMode: String, Codable {
240244
case polling
241245
}
242246

247+
public enum ErrorStrategy: String, Codable {
248+
case fail
249+
case fallback
250+
}
251+
243252
public struct ClientOptions<T: Encodable>: Encodable {
244253
public let url: String
245254
public let authentication: T?
246255
public let updateInterval: Int
247256
public let reference: String
248257
public let fetchMode: FetchMode
258+
public let errorStrategy: ErrorStrategy
249259
}
250260

251261
public struct Flag: Codable {

0 commit comments

Comments
 (0)