Skip to content

Commit b7dacec

Browse files
committed
feat(swift): switch api key helper
1 parent 0b0fa26 commit b7dacec

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

clients/algoliasearch-client-swift/Sources/Core/Networking/BaseConfiguration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public protocol BaseConfiguration {
2323
var hosts: [RetryableHost] { get }
2424

2525
/// Default headers that should be applied to every request.
26-
var defaultHeaders: [String: String]? { get }
26+
var defaultHeaders: [String: String]? { get set }
2727

2828
/// Compression type
2929
var compression: CompressionAlgorithm { get }
@@ -48,7 +48,7 @@ public struct DefaultConfiguration: BaseConfiguration {
4848
public let writeTimeout: TimeInterval = 30
4949
public let readTimeout: TimeInterval = 5
5050
public let logLevel: LogLevel = .info
51-
public let defaultHeaders: [String: String]? = [:]
51+
public var defaultHeaders: [String: String]? = [:]
5252
public var hosts: [RetryableHost] = []
5353
public let compression: CompressionAlgorithm = .none
5454
}

clients/algoliasearch-client-swift/Sources/Core/Networking/Transporter.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Foundation
1212
// MARK: - Transporter
1313

1414
open class Transporter {
15-
let configuration: BaseConfiguration
15+
var configuration: BaseConfiguration
1616
let retryStrategy: RetryStrategy
1717
let requestBuilder: RequestBuilder
1818
let exposeIntermediateErrors: Bool
@@ -42,6 +42,10 @@ open class Transporter {
4242

4343
self.requestBuilder = requestBuilder
4444
}
45+
46+
public func setApiKey(apiKey: String) {
47+
self.configuration.defaultHeaders?.updateValue(apiKey, forKey: "X-Algolia-API-Key")
48+
}
4549

4650
public func send<T: Decodable>(
4751
method: String, path: String, data: Codable?, requestOptions: RequestOptions? = nil,

templates/swift/api.mustache

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ import Foundation
3131
self.init(configuration: try {{classname}}Configuration(appID: appID, apiKey: apiKey{{#hasRegionalHost}}, region: region{{/hasRegionalHost}}))
3232
}
3333

34+
open func setApiKey(apiKey: String) {
35+
self.configuration.apiKey = apiKey
36+
self.transporter.setApiKey(apiKey: apiKey)
37+
}
38+
3439
{{#operation}}
3540
{{#allParams}}
3641
{{#isEnum}}
@@ -144,4 +149,4 @@ import Foundation
144149
}{{/vendorExtensions}}
145150
{{/operation}}
146151
}
147-
{{/operations}}
152+
{{/operations}}

templates/swift/client_configuration.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct {{#lambda.client-to-name}}{{{client}}}{{/lambda.client-to-name}}Cl
1414
{{/hasRegionalHost}}
1515

1616
public let appID: String
17-
public let apiKey: String
17+
public var apiKey: String
1818
public var writeTimeout: TimeInterval
1919
public var readTimeout: TimeInterval
2020
public var logLevel: LogLevel

0 commit comments

Comments
 (0)