From ddfbb806c657dd14368883cbcedae5bb3875e584 Mon Sep 17 00:00:00 2001 From: Elliot <35050275+Apache-HB@users.noreply.github.com> Date: Tue, 20 Oct 2020 16:11:16 -0400 Subject: [PATCH] add clearer field names and more documentation --- Source/LDSwiftEventSource.swift | 6 ++---- Source/Types.swift | 5 +++++ Tests/LDSwiftEventSourceTests.swift | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Source/LDSwiftEventSource.swift b/Source/LDSwiftEventSource.swift index 55e7acf..cf9858c 100644 --- a/Source/LDSwiftEventSource.swift +++ b/Source/LDSwiftEventSource.swift @@ -8,8 +8,6 @@ import FoundationNetworking import os.log #endif -public typealias HeaderTransform = ([String: String]) -> [String: String] - public class EventSource { private let esDelegate: EventSourceDelegate @@ -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 @@ -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) diff --git a/Source/Types.swift b/Source/Types.swift index 397aace..5acf023 100644 --- a/Source/Types.swift +++ b/Source/Types.swift @@ -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. diff --git a/Tests/LDSwiftEventSourceTests.swift b/Tests/LDSwiftEventSourceTests.swift index ef5eae2..e7cea40 100644 --- a/Tests/LDSwiftEventSourceTests.swift +++ b/Tests/LDSwiftEventSourceTests.swift @@ -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 @@ -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)