Skip to content

Commit

Permalink
use keychain accessibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ski-u committed Dec 14, 2024
1 parent bbb0f88 commit 58e0db5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Features/Sources/APIKeyClient/APIKeyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Models

@DependencyClient
public struct APIKeyClient: Sendable {
public var getKey: @Sendable () throws -> APIKey?
public var setKey: @Sendable (APIKey?) throws -> Void
public var getKey: @Sendable () -> APIKey?
public var setKey: @Sendable (APIKey?) -> Void
}

extension APIKeyClient: TestDependencyKey {
Expand Down
8 changes: 2 additions & 6 deletions Features/Sources/APIKeyClientLive/LiveKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ extension APIKeyClient: DependencyKey {

return .init(
getKey: {
try keychain.get("APIKey").map(APIKey.init(rawValue:))
keychain["APIKey"].map(APIKey.init(rawValue:))
},
setKey: {
if let key = $0 {
try keychain.set(key.rawValue, key: "APIKey")
} else {
try keychain.remove("APIKey")
}
keychain.accessibility(.whenUnlockedThisDeviceOnly)["APIKey"] = $0?.rawValue
}
)
}
Expand Down
10 changes: 5 additions & 5 deletions NASAClientTests/APIKeyClientLive/APIKeyClientLiveTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ struct APIKeyClientLiveTests {
}

@Test
func liveValue() throws {
func liveValue() {
let client = APIKeyClient.liveValue(service: "test")
let key = APIKey(rawValue: "key")

try client.setKey(key)
#expect(try client.getKey() == key)
client.setKey(key)
#expect(client.getKey() == key)

try client.setKey(nil)
#expect(try client.getKey() == nil)
client.setKey(nil)
#expect(client.getKey() == nil)
}
}

0 comments on commit 58e0db5

Please sign in to comment.