Repository: Automattic/pocket-casts-ios
File: Modules/Server/Sources/PocketCastsServer/Public/Discover/DiscoverServerHandler.swift (Swift)

Code before:
    }

    public func discoverItem<T>(_ source: String?, authenticated: Bool, type: T.Type) -> AnyPublisher<T, Error> where T: Decodable {
        guard let source = source else {
            return Fail(error: DiscoverServerError.badRequest).eraseToAnyPublisher()
        }

        return Future { [unowned self] promise in
            self.discoverRequest(path: source, type: type, authenticated: authenticated) { discoverList, didError in
                if !didError, let discoverList = discoverList {
                    promise(.success(discoverList))
                } else {
                    promise(.failure(DiscoverServerError.unknown))
                }
            }
        }
        .eraseToAnyPublisher()
    }

    /// A method to check whether the response from the source URL authenticated successfully.
    /// - Parameters:
    ///   - item: An item which is `authenticated == true`
    /// - Returns: Whether or not the authentication succeeded
    public func checkSourceAuthentication(for item: DiscoverItem) async -> Bool {
        // If there's no source URL, consider authentication failed. This shouldn't happen.
        guard item.authenticated == true, let source = item.source else {
            return false
        }

        return await withCheckedContinuation { continuation in
            performDiscoverRequest(path: source, authenticated: item.authenticated == true) { data, response, error in
                let success = response?.extractStatusCode() == 200
                continuation.resume(returning: success)
            }
        }
    }

    private func performDiscoverRequest(
        path: String,
        authenticated: Bool?,
        completion: @escaping (Data?, URLResponse?, Error?) -> Void
    ) {
        let url = ServerHelper.asUrl(path)
        let request = URLRequest(url: url)

        if let cachedResponse = discoveryCache.cachedResponse(for: request),
           let expiryDate = cachedResponse.response.cacheExpiryDate(),
           expiryDate.timeIntervalSinceNow > 0 {
            completion(cachedResponse.data, cachedResponse.response, nil)
            return
        }

Review comment (security):
Nit: instead of checking against the optional `authenticated` I'm wondering if `DiscoverItem` can have a computed property `isAuthenticated` that unwraps it or fallback to false

Code after:
    }

    public func discoverItem<T>(_ source: String?, authenticated: Bool, type: T.Type) -> AnyPublisher<T, Error> where T: Decodable {
        guard let source = source else {
            return Fail(error: DiscoverServerError.badRequest).eraseToAnyPublisher()
        }

        return Future { [unowned self] promise in
            self.discoverRequest(path: source, type: type, authenticated: authenticated) { discoverList, didError in
                if !didError, let discoverList = discoverList {
                    promise(.success(discoverList))
                } else {
                    promise(.failure(DiscoverServerError.unknown))
                }
            }
        }
        .eraseToAnyPublisher()
    }

    /// A method to check whether the response from the source URL authenticated successfully.
    /// - Parameters:
    ///   - item: An item which is `authenticated == true`
    /// - Returns: Whether or not the authentication succeeded
    public func checkSourceAuthentication(for item: DiscoverItem) async -> Bool {
        // If there's no source URL, consider authentication failed. This shouldn't happen.
        guard item.isAuthenticated, let source = item.source else {
            return false
        }

        return await withCheckedContinuation { continuation in
            performDiscoverRequest(path: source, authenticated: item.isAuthenticated) { data, response, error in
                let success = response?.extractStatusCode() == 200
                continuation.resume(returning: success)
            }
        }
    }

    private func performDiscoverRequest(
        path: String,
        authenticated: Bool?,
        completion: @escaping (Data?, URLResponse?, Error?) -> Void
    ) {
        let url = ServerHelper.asUrl(path)
        let request = URLRequest(url: url)

        if let cachedResponse = discoveryCache.cachedResponse(for: request),
           let expiryDate = cachedResponse.response.cacheExpiryDate(),
           expiryDate.timeIntervalSinceNow > 0 {
            completion(cachedResponse.data, cachedResponse.response, nil)
            return
        }