Skip to content

Commit e3b3a5b

Browse files
committed
Allow specifying specific URLSession used for calls.
1 parent 59a786d commit e3b3a5b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Sources/ComposableArchitecturePattern/Server.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ public protocol Server: Actor {
4040
/// The logger to use with communicating server activity.
4141
var logger: Logger { get }
4242

43+
/// The `URLSession` to use for all server calls.
44+
var urlSession: URLSession { get }
45+
4346
/// Designated initializer
4447
init(
48+
urlSession: URLSession,
4549
environments: [ServerEnvironment],
4650
currentEnvironment: ServerEnvironment?,
4751
additionalHTTPHeaders: [String: String]?,
@@ -111,6 +115,10 @@ public extension Server {
111115
return Logger(subsystem: Bundle.main.bundleIdentifier ?? "com.Coordinator", category: String(describing: Self.self))
112116
}
113117

118+
var urlSession: URLSession {
119+
URLSession.shared
120+
}
121+
114122
// GETs
115123
func get<T: Codable>(_ api: any ServerAPI, additionalHeaders: [String: String]? = nil, queries: [URLQueryItem]? = nil, httpBodyOverride httpBody: Data? = nil, timeoutInterval: TimeInterval? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
116124
if self.blockAllAPIsNotSupported {
@@ -341,7 +349,7 @@ public extension Server {
341349
self.logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Start]")
342350
}
343351

344-
let (data, response) = try await URLSession.shared.data(for: request)
352+
let (data, response) = try await self.urlSession.data(for: request)
345353

346354
self.requestsBeingProcessed.remove(requestUID)
347355

@@ -371,7 +379,7 @@ public extension Server {
371379
logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Start]")
372380
}
373381

374-
let (_, response) = try await URLSession.shared.data(for: request)
382+
let (_, response) = try await self.urlSession.data(for: request)
375383

376384
self.requestsBeingProcessed.remove(requestUID)
377385

0 commit comments

Comments
 (0)