Skip to content

Commit

Permalink
add clearer field names and more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
apache-hb committed Oct 20, 2020
1 parent 4f498eb commit ddfbb80
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
6 changes: 2 additions & 4 deletions Source/LDSwiftEventSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import FoundationNetworking
import os.log
#endif

public typealias HeaderTransform = ([String: String]) -> [String: String]

public class EventSource {
private let esDelegate: EventSourceDelegate

Expand Down Expand Up @@ -58,7 +56,7 @@ public class EventSource {
/// Additional headers to be set on the request
public var headers: [String: String] = [:]
/// Provides the ability to add conditional headers
public var extraHeaders: HeaderTransform = { $0 }
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 @@ -148,7 +146,7 @@ class EventSourceDelegate: NSObject, URLSessionDataDelegate {
urlRequest.httpMethod = self.config.method
urlRequest.httpBody = self.config.body
urlRequest.setValue(self.lastEventId, forHTTPHeaderField: "Last-Event-ID")
urlRequest.allHTTPHeaderFields = self.config.extraHeaders(
urlRequest.allHTTPHeaderFields = self.config.headerTransform(
urlRequest.allHTTPHeaderFields?.merging(self.config.headers) { $1 } ?? self.config.headers
)
let task = session.dataTask(with: urlRequest)
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
4 changes: 2 additions & 2 deletions Tests/LDSwiftEventSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class LDSwiftEventSourceTests: XCTestCase {
config.body = testBody
config.lastEventId = "eventId"
config.headers = testHeaders
config.extraHeaders = { _ in [:] }
config.headerTransform = { _ in [:] }
config.reconnectTime = 2.0
config.maxReconnectTime = 60.0
config.backoffResetThreshold = 120.0
Expand All @@ -40,7 +40,7 @@ final class LDSwiftEventSourceTests: XCTestCase {
XCTAssertEqual(config.body, testBody)
XCTAssertEqual(config.lastEventId, "eventId")
XCTAssertEqual(config.headers, testHeaders)
XCTAssertEqual(config.extraHeaders(config.headers), [:])
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 ddfbb80

Please sign in to comment.