Skip to content

Commit 40fb264

Browse files
authored
Merge pull request #10 from launchdarkly/ehaisley/ch91505/lazy-headers
Support setting http headers via a closure when connecting and reconnecting.
2 parents 4f817e1 + ddfbb80 commit 40fb264

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Source/LDSwiftEventSource.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class EventSource {
5555
public var lastEventId: String? = nil
5656
/// Additional headers to be set on the request
5757
public var headers: [String: String] = [:]
58+
/// Provides the ability to add conditional headers
59+
public var headerTransform: HeaderTransform = { $0 }
5860
/// The minimum amount of time to wait before reconnecting after a failure
5961
public var reconnectTime: TimeInterval = 1.0
6062
/// The maximum amount of time to wait before reconnecting after a failure
@@ -144,7 +146,9 @@ class EventSourceDelegate: NSObject, URLSessionDataDelegate {
144146
urlRequest.httpMethod = self.config.method
145147
urlRequest.httpBody = self.config.body
146148
urlRequest.setValue(self.lastEventId, forHTTPHeaderField: "Last-Event-ID")
147-
urlRequest.allHTTPHeaderFields?.merge(self.config.headers, uniquingKeysWith: { $1 })
149+
urlRequest.allHTTPHeaderFields = self.config.headerTransform(
150+
urlRequest.allHTTPHeaderFields?.merging(self.config.headers) { $1 } ?? self.config.headers
151+
)
148152
let task = session.dataTask(with: urlRequest)
149153
task.resume()
150154
sessionTask = task

Source/Types.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import Foundation
55
/// it has the ability to tell EventSource to stop reconnecting.
66
public typealias ConnectionErrorHandler = (Error) -> ConnectionErrorAction
77

8+
/// Type for a function that will take in the current http headers
9+
/// and return a new set of http headers to be used when connecting
10+
/// and reconnecting to a stream.
11+
public typealias HeaderTransform = ([String: String]) -> [String: String]
12+
813
/// Potential actions a ConnectionErrorHandler can return
914
public enum ConnectionErrorAction {
1015
/// Specifies that the error should be logged normally and dispatched to the EventHandler.

Tests/LDSwiftEventSourceTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ final class LDSwiftEventSourceTests: XCTestCase {
2929
config.body = testBody
3030
config.lastEventId = "eventId"
3131
config.headers = testHeaders
32+
config.headerTransform = { _ in [:] }
3233
config.reconnectTime = 2.0
3334
config.maxReconnectTime = 60.0
3435
config.backoffResetThreshold = 120.0
@@ -39,6 +40,7 @@ final class LDSwiftEventSourceTests: XCTestCase {
3940
XCTAssertEqual(config.body, testBody)
4041
XCTAssertEqual(config.lastEventId, "eventId")
4142
XCTAssertEqual(config.headers, testHeaders)
43+
XCTAssertEqual(config.headerTransform(config.headers), [:])
4244
XCTAssertEqual(config.reconnectTime, 2.0)
4345
XCTAssertEqual(config.maxReconnectTime, 60.0)
4446
XCTAssertEqual(config.backoffResetThreshold, 120.0)

0 commit comments

Comments
 (0)