Skip to content

Commit

Permalink
0.40.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Mar 10, 2024
1 parent fd7b907 commit 14b7138
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
20 changes: 10 additions & 10 deletions Example/Sources/PetStore/PetStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ public struct PetStore {

var client: APIClient

public init(baseURL: BaseURL, fileID: String, line: UInt) {
client = APIClient(baseURL: baseURL.url)
.fileIDLine(fileID: fileID, line: line)
.bodyDecoder(PetStoreDecoder())
.tokenRefresher { client, _ in
try await client.path("token").post()
} auth: {
.bearer(token: $0)
}
}
public init(baseURL: BaseURL, fileID: String, line: UInt) {
client = APIClient(baseURL: baseURL.url)
.fileIDLine(fileID: fileID, line: line)
.bodyDecoder(PetStoreDecoder())
.tokenRefresher { client, _ in
try await client.path("token").post()
} auth: {
.bearer(token: $0)
}
}
}

// MARK: - "pet" path
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "0.39.0")
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "0.40.0")
],
targets: [
.target(
Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftAPIClient/APIClientConfigs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public extension APIClient {
private var values: [PartialKeyPath<APIClient.Configs>: Any] = [:]

/// Initializes a new configuration set for `APIClient`.
public init() {
}
public init() {}

/// Provides subscript access to configuration values based on their key paths.
/// - Parameter keyPath: A `WritableKeyPath` to the configuration property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
public extension HTTPClientMiddleware where Self == TokenRefresherMiddleware {

static func tokenRefresher(
cacheService: TokenCacheService = valueFor(live: .keychain, test: .mock),
cacheService: TokenCacheService = valueFor(live: .keychain, test: .mock),
expiredStatusCodes: Set<HTTPStatusCode> = [.unauthorized],
refreshToken: @escaping (APIClient.Configs) async throws -> String,
auth: @escaping (String) -> AuthModifier
Expand All @@ -17,23 +17,23 @@ public extension HTTPClientMiddleware where Self == TokenRefresherMiddleware {
}
}

extension APIClient {
public extension APIClient {

public func tokenRefresher(
cacheService: TokenCacheService = valueFor(live: .keychain, test: .mock),
expiredStatusCodes: Set<HTTPStatusCode> = [.unauthorized],
refreshToken: @escaping (APIClient, APIClient.Configs) async throws -> String,
auth: @escaping (String) -> AuthModifier
) -> Self {
httpClientMiddleware(
TokenRefresherMiddleware(
cacheService: cacheService,
expiredStatusCodes: expiredStatusCodes,
refreshToken: { try await refreshToken(self, $0) },
auth: auth
)
)
}
func tokenRefresher(
cacheService: TokenCacheService = valueFor(live: .keychain, test: .mock),
expiredStatusCodes: Set<HTTPStatusCode> = [.unauthorized],
refreshToken: @escaping (APIClient, APIClient.Configs) async throws -> String,
auth: @escaping (String) -> AuthModifier
) -> Self {
httpClientMiddleware(
TokenRefresherMiddleware(
cacheService: cacheService,
expiredStatusCodes: expiredStatusCodes,
refreshToken: { try await refreshToken(self, $0) },
auth: auth
)
)
}
}

public struct TokenRefresherMiddleware: HTTPClientMiddleware {
Expand Down

0 comments on commit 14b7138

Please sign in to comment.