Skip to content

Commit 0d9ab71

Browse files
committed
Add ability to have custom HTTP sessions.
1 parent ef033e8 commit 0d9ab71

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

Sources/Segment/Configuration.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ public extension Configuration {
246246

247247
/// Specify a custom UserAgent string. This bypasses the OS dependent check entirely.
248248
@discardableResult
249-
func httpSession(_ httpSession: @escaping @autoclosure () -> HTTPSession) -> Configuration {
250-
values.httpSession = httpSession
249+
func userAgent(_ userAgent: String) -> Configuration {
250+
values.userAgent = userAgent
251251
return self
252252
}
253253

@@ -273,6 +273,16 @@ public extension Configuration {
273273
values.anonymousIdGenerator = generator
274274
return self
275275
}
276+
277+
/// Use a custom HTTP session; Useful for non-apple platforms where Swift networking isn't as mature
278+
/// or has issues to work around.
279+
/// - Parameter httpSession: A class conforming to the HTTPSession protocol
280+
/// - Returns: The current configuration
281+
@discardableResult
282+
func httpSession(_ httpSession: @escaping @autoclosure () -> any HTTPSession) -> Configuration {
283+
values.httpSession = httpSession
284+
return self
285+
}
276286
}
277287

278288
extension Analytics {

Sources/Segment/Plugins/SegmentDestination.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class SegmentDestination: DestinationPlugin, Subscriber, FlushCompletion
4141
internal struct UploadTaskInfo {
4242
let url: URL?
4343
let data: Data?
44-
let task: URLSessionDataTask
44+
let task: DataTask
4545
// set/used via an extension in iOSLifecycleMonitor.swift
4646
typealias CleanupClosure = () -> Void
4747
var cleanup: CleanupClosure? = nil

Sources/Segment/Utilities/HTTPClient.swift renamed to Sources/Segment/Utilities/Networking/HTTPClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class HTTPClient {
5252
/// - batch: The array of the events, considered a batch of events.
5353
/// - completion: The closure executed when done. Passes if the task should be retried or not if failed.
5454
@discardableResult
55-
func startBatchUpload(writeKey: String, batch: URL, completion: @escaping (_ result: Result<Bool, Error>) -> Void) -> URLSessionDataTask? {
55+
func startBatchUpload(writeKey: String, batch: URL, completion: @escaping (_ result: Result<Bool, Error>) -> Void) -> (any DataTask)? {
5656
guard let uploadURL = segmentURL(for: apiHost, path: "/b") else {
5757
self.analytics?.reportInternalError(HTTPClientErrors.failedToOpenBatch)
5858
completion(.failure(HTTPClientErrors.failedToOpenBatch))
@@ -77,7 +77,7 @@ public class HTTPClient {
7777
/// - batch: The array of the events, considered a batch of events.
7878
/// - completion: The closure executed when done. Passes if the task should be retried or not if failed.
7979
@discardableResult
80-
func startBatchUpload(writeKey: String, data: Data, completion: @escaping (_ result: Result<Bool, Error>) -> Void) -> URLSessionDataTask? {
80+
func startBatchUpload(writeKey: String, data: Data, completion: @escaping (_ result: Result<Bool, Error>) -> Void) -> (any UploadTask)? {
8181
guard let uploadURL = segmentURL(for: apiHost, path: "/b") else {
8282
self.analytics?.reportInternalError(HTTPClientErrors.failedToOpenBatch)
8383
completion(.failure(HTTPClientErrors.failedToOpenBatch))

Sources/Segment/Utilities/Networking/HTTPSession.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,30 @@ import FoundationNetworking
55
#endif
66

77
public protocol DataTask {
8-
var state: URLSessionTask.State { get }
9-
func resume()
8+
var state: URLSessionTask.State { get }
9+
func resume()
1010
}
1111

1212
public protocol UploadTask: DataTask {}
1313

1414
// An enumeration of default `HTTPSession` configurations to be used
1515
// This can be extended buy consumer to easily refer back to their configured session.
1616
public enum HTTPSessions {
17-
/// An implementation of `HTTPSession` backed by Apple's `URLSession`.
18-
public static func urlSession() -> any HTTPSession {
19-
let configuration = URLSessionConfiguration.ephemeral
20-
configuration.httpMaximumConnectionsPerHost = 2
21-
let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: nil)
22-
return session
23-
}
17+
/// An implementation of `HTTPSession` backed by Apple's `URLSession`.
18+
public static func urlSession() -> any HTTPSession {
19+
let configuration = URLSessionConfiguration.ephemeral
20+
configuration.httpMaximumConnectionsPerHost = 2
21+
let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: nil)
22+
return session
23+
}
2424
}
2525

2626
public protocol HTTPSession {
27-
associatedtype DataTaskType: DataTask
28-
associatedtype UploadTaskType: UploadTask
29-
30-
func uploadTask(with request: URLRequest, fromFile file: URL, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> UploadTaskType
31-
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> DataTaskType
32-
func finishTasksAndInvalidate()
27+
associatedtype DataTaskType: DataTask
28+
associatedtype UploadTaskType: UploadTask
29+
30+
func uploadTask(with request: URLRequest, fromFile file: URL, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> UploadTaskType
31+
func uploadTask(with request: URLRequest, from bodyData: Data?, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> UploadTaskType
32+
func dataTask(with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void) -> DataTaskType
33+
func finishTasksAndInvalidate()
3334
}

0 commit comments

Comments
 (0)