Repository: vapor/vapor
File: Sources/Vapor/Client/FoundationClient.swift (Swift)

Code before:
/// `Client` wrapper around `Foundation.URLSession`.
public final class FoundationClient: Client, ServiceType {
    /// See `ServiceType`.
    public static var serviceSupports: [Any.Type] {
        return [Client.self]
    }

    /// See `ServiceType`.
    public static func makeService(for worker: Container) throws -> FoundationClient {
        return .default(on: worker)
    }

    /// See `Client`.
    public var container: Container

    /// The `URLSession` powering this client.
    private let urlSession: URLSession
    
    /// Default caching policy
    public let cachePolicy: URLRequest.CachePolicy

    /// Creates a new `FoundationClient`.
    public init(_ urlSession: URLSession, cachePolicy: URLRequest.CachePolicy = .reloadIgnoringLocalAndRemoteCacheData, on container: Container) {
        self.urlSession = urlSession
        self.cachePolicy = cachePolicy
        self.container = container
    }

    /// Creates a `FoundationClient` with default settings.
    public static func `default`(on container: Container) -> FoundationClient {
        return .init(.init(configuration: .default), on: container)
    }

    /// See `Client`.
    public func send(_ req: Request) -> Future<Response> {
        let urlReq = req.http.convertToFoundationRequest(defaultCachePolicy: cachePolicy)
        let promise = req.eventLoop.newPromise(Response.self)
        self.urlSession.dataTask(with: urlReq) { data, urlResponse, error in
            if let error = error {
                promise.fail(error: error)
                return
            }

            guard let httpResponse = urlResponse as? HTTPURLResponse else {
                let error = VaporError(identifier: "httpURLResponse", reason: "URLResponse was not a HTTPURLResponse.")
                promise.fail(error: error)
                return
            }

Review comment (performance):
we're looking again at this and ready to merge, just need one small change here that we think this would be better as:

```swift
cachePolicy: URLRequest.CachePolicy? = nil,
```
Only because this is the current behavior and might be expected by users on some end for a 3.1 or 3.0.x release.

Code after:
/// `Client` wrapper around `Foundation.URLSession`.
public final class FoundationClient: Client, ServiceType {
    /// See `ServiceType`.
    public static var serviceSupports: [Any.Type] {
        return [Client.self]
    }

    /// See `ServiceType`.
    public static func makeService(for worker: Container) throws -> FoundationClient {
        return .default(on: worker)
    }

    /// See `Client`.
    public var container: Container

    /// The `URLSession` powering this client.
    private let urlSession: URLSession
    
    /// Default caching policy
    public let cachePolicy: URLRequest.CachePolicy?

    /// Creates a new `FoundationClient`.
    public init(_ urlSession: URLSession, cachePolicy: URLRequest.CachePolicy? = nil, on container: Container) {
        self.urlSession = urlSession
        self.cachePolicy = cachePolicy
        self.container = container
    }

    /// Creates a `FoundationClient` with default settings.
    public static func `default`(on container: Container) -> FoundationClient {
        return .init(.init(configuration: .default), on: container)
    }

    /// See `Client`.
    public func send(_ req: Request) -> Future<Response> {
        let urlReq = req.http.convertToFoundationRequest(defaultCachePolicy: cachePolicy)
        let promise = req.eventLoop.newPromise(Response.self)
        self.urlSession.dataTask(with: urlReq) { data, urlResponse, error in
            if let error = error {
                promise.fail(error: error)
                return
            }

            guard let httpResponse = urlResponse as? HTTPURLResponse else {
                let error = VaporError(identifier: "httpURLResponse", reason: "URLResponse was not a HTTPURLResponse.")
                promise.fail(error: error)
                return
            }