Skip to content

Commit

Permalink
Allow specifying specific URLSession used for calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnyholland committed Nov 28, 2024
1 parent 59a786d commit e3b3a5b
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Sources/ComposableArchitecturePattern/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ public protocol Server: Actor {
/// The logger to use with communicating server activity.
var logger: Logger { get }

/// The `URLSession` to use for all server calls.
var urlSession: URLSession { get }

/// Designated initializer
init(
urlSession: URLSession,
environments: [ServerEnvironment],
currentEnvironment: ServerEnvironment?,
additionalHTTPHeaders: [String: String]?,
Expand Down Expand Up @@ -111,6 +115,10 @@ public extension Server {
return Logger(subsystem: Bundle.main.bundleIdentifier ?? "com.Coordinator", category: String(describing: Self.self))
}

var urlSession: URLSession {
URLSession.shared
}

// GETs
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 {
if self.blockAllAPIsNotSupported {
Expand Down Expand Up @@ -341,7 +349,7 @@ public extension Server {
self.logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Start]")
}

let (data, response) = try await URLSession.shared.data(for: request)
let (data, response) = try await self.urlSession.data(for: request)

self.requestsBeingProcessed.remove(requestUID)

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

let (_, response) = try await URLSession.shared.data(for: request)
let (_, response) = try await self.urlSession.data(for: request)

self.requestsBeingProcessed.remove(requestUID)

Expand Down

0 comments on commit e3b3a5b

Please sign in to comment.