Skip to content

Commit

Permalink
Merge pull request #10 from launchdarkly/ehaisley/ch91505/lazy-headers
Browse files Browse the repository at this point in the history
Support setting http headers via a closure when connecting and reconnecting.
  • Loading branch information
apache-hb authored Oct 21, 2020
2 parents 4f817e1 + ddfbb80 commit 40fb264
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Source/LDSwiftEventSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class EventSource {
public var lastEventId: String? = nil
/// Additional headers to be set on the request
public var headers: [String: String] = [:]
/// Provides the ability to add conditional headers
public var headerTransform: HeaderTransform = { $0 }
/// The minimum amount of time to wait before reconnecting after a failure
public var reconnectTime: TimeInterval = 1.0
/// The maximum amount of time to wait before reconnecting after a failure
Expand Down Expand Up @@ -144,7 +146,9 @@ class EventSourceDelegate: NSObject, URLSessionDataDelegate {
urlRequest.httpMethod = self.config.method
urlRequest.httpBody = self.config.body
urlRequest.setValue(self.lastEventId, forHTTPHeaderField: "Last-Event-ID")
urlRequest.allHTTPHeaderFields?.merge(self.config.headers, uniquingKeysWith: { $1 })
urlRequest.allHTTPHeaderFields = self.config.headerTransform(
urlRequest.allHTTPHeaderFields?.merging(self.config.headers) { $1 } ?? self.config.headers
)
let task = session.dataTask(with: urlRequest)
task.resume()
sessionTask = task
Expand Down
5 changes: 5 additions & 0 deletions Source/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import Foundation
/// it has the ability to tell EventSource to stop reconnecting.
public typealias ConnectionErrorHandler = (Error) -> ConnectionErrorAction

/// Type for a function that will take in the current http headers
/// and return a new set of http headers to be used when connecting
/// and reconnecting to a stream.
public typealias HeaderTransform = ([String: String]) -> [String: String]

/// Potential actions a ConnectionErrorHandler can return
public enum ConnectionErrorAction {
/// Specifies that the error should be logged normally and dispatched to the EventHandler.
Expand Down
2 changes: 2 additions & 0 deletions Tests/LDSwiftEventSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class LDSwiftEventSourceTests: XCTestCase {
config.body = testBody
config.lastEventId = "eventId"
config.headers = testHeaders
config.headerTransform = { _ in [:] }
config.reconnectTime = 2.0
config.maxReconnectTime = 60.0
config.backoffResetThreshold = 120.0
Expand All @@ -39,6 +40,7 @@ final class LDSwiftEventSourceTests: XCTestCase {
XCTAssertEqual(config.body, testBody)
XCTAssertEqual(config.lastEventId, "eventId")
XCTAssertEqual(config.headers, testHeaders)
XCTAssertEqual(config.headerTransform(config.headers), [:])
XCTAssertEqual(config.reconnectTime, 2.0)
XCTAssertEqual(config.maxReconnectTime, 60.0)
XCTAssertEqual(config.backoffResetThreshold, 120.0)
Expand Down

0 comments on commit 40fb264

Please sign in to comment.