Skip to content

Commit

Permalink
Allows frontend clients to send additional headers for the possibilit…
Browse files Browse the repository at this point in the history
…y of establishing the connection via another API that requires an authentication header (#86)

Co-authored-by: Francis Batista <[email protected]>
  • Loading branch information
francisbatista and Francis Batista authored Sep 17, 2024
1 parent 4578bb9 commit 36d2b03
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Sources/UnleashProxyClientSwift/Metrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ public class Metrics {
var timer: Timer?
var bucket: Bucket
let url: URL
let customClientHeaders: [String: String]

init(appName: String,
metricsInterval: TimeInterval,
clock: @escaping () -> Date,
disableMetrics: Bool = false,
poster: @escaping PosterHandler,
url: URL,
clientKey: String) {
clientKey: String,
customClientHeaders: [String: String] = [:]) {
self.appName = appName
self.metricsInterval = metricsInterval
self.clock = clock
Expand All @@ -88,6 +90,7 @@ public class Metrics {
self.url = url
self.clientKey = clientKey
self.bucket = Bucket(clock: clock)
self.customClientHeaders = customClientHeaders
}

func start() {
Expand Down Expand Up @@ -162,6 +165,11 @@ public class Metrics {
request.addValue("no-cache", forHTTPHeaderField: "Cache")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(clientKey, forHTTPHeaderField: "Authorization")
if !self.customClientHeaders.isEmpty {
for (key, value) in self.customClientHeaders {
request.setValue(value, forHTTPHeaderField: key)
}
}
return request
}
}
9 changes: 8 additions & 1 deletion Sources/UnleashProxyClientSwift/Poller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public class Poller {

private let session: PollerSession
var storageProvider: StorageProvider
let customClientHeaders: [String: String]

public init(refreshInterval: Int? = nil, unleashUrl: URL, apiKey: String, session: PollerSession = URLSession.shared, storageProvider: StorageProvider = DictionaryStorageProvider()) {
public init(refreshInterval: Int? = nil, unleashUrl: URL, apiKey: String, session: PollerSession = URLSession.shared, storageProvider: StorageProvider = DictionaryStorageProvider(), customClientHeaders: [String: String] = [:]) {
self.refreshInterval = refreshInterval
self.unleashUrl = unleashUrl
self.apiKey = apiKey
Expand All @@ -65,6 +66,7 @@ public class Poller {
self.etag = ""
self.session = session
self.storageProvider = storageProvider
self.customClientHeaders = customClientHeaders
}

public func start(context: Context, completionHandler: ((PollerError?) -> Void)? = nil) -> Void {
Expand Down Expand Up @@ -117,6 +119,11 @@ public class Poller {
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(self.apiKey, forHTTPHeaderField: "Authorization")
request.setValue(self.etag, forHTTPHeaderField: "If-None-Match")
if !self.customClientHeaders.isEmpty {
for (key, value) in self.customClientHeaders {
request.setValue(value, forHTTPHeaderField: key)
}
}
request.cachePolicy = .reloadIgnoringLocalCacheData

session.perform(request, completionHandler: { (data, response, error) in
Expand Down
6 changes: 3 additions & 3 deletions Sources/UnleashProxyClientSwift/UnleashProxyClientSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class UnleashClientBase {
var poller: Poller
var metrics: Metrics

public init(unleashUrl: String, clientKey: String, refreshInterval: Int = 15, metricsInterval: Int = 30, disableMetrics: Bool = false, appName: String = "unleash-swift-client", environment: String? = "default", context: [String: String]? = nil, poller: Poller? = nil, metrics: Metrics? = nil) {
public init(unleashUrl: String, clientKey: String, refreshInterval: Int = 15, metricsInterval: Int = 30, disableMetrics: Bool = false, appName: String = "unleash-swift-client", environment: String? = "default", context: [String: String]? = nil, poller: Poller? = nil, metrics: Metrics? = nil, customClientHeaders: [String: String] = [:]) {
guard let url = URL(string: unleashUrl), url.scheme != nil else {
fatalError("Invalid Unleash URL: \(unleashUrl)")
}
Expand All @@ -42,7 +42,7 @@ public class UnleashClientBase {
if let poller = poller {
self.poller = poller
} else {
self.poller = Poller(refreshInterval: refreshInterval, unleashUrl: url, apiKey: clientKey)
self.poller = Poller(refreshInterval: refreshInterval, unleashUrl: url, apiKey: clientKey, customClientHeaders: customClientHeaders)
}
if let metrics = metrics {
self.metrics = metrics
Expand All @@ -57,7 +57,7 @@ public class UnleashClientBase {
}
task.resume()
}
self.metrics = Metrics(appName: appName, metricsInterval: Double(metricsInterval), clock: { return Date() }, disableMetrics: disableMetrics, poster: urlSessionPoster, url: url, clientKey: clientKey)
self.metrics = Metrics(appName: appName, metricsInterval: Double(metricsInterval), clock: { return Date() }, disableMetrics: disableMetrics, poster: urlSessionPoster, url: url, clientKey: clientKey, customClientHeaders: customClientHeaders)
}

self.context = Context(appName: appName, environment: environment)
Expand Down

0 comments on commit 36d2b03

Please sign in to comment.