|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the AsyncHTTPClient open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the AsyncHTTPClient project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Logging |
| 16 | +import NIO |
| 17 | + |
| 18 | +@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 19 | +extension HTTPClient { |
| 20 | + #if compiler(>=6.0) |
| 21 | + /// Start & automatically shut down a new ``HTTPClient``. |
| 22 | + /// |
| 23 | + /// This method allows to start & automatically dispose of a ``HTTPClient`` following the principle of Structured Concurrency. |
| 24 | + /// The ``HTTPClient`` is guaranteed to be shut down upon return, whether `body` throws or not. |
| 25 | + /// |
| 26 | + /// This may be particularly useful if you cannot use the shared singleton (``HTTPClient/shared``). |
| 27 | + public static func withHTTPClient<Return>( |
| 28 | + eventLoopGroup: any EventLoopGroup = HTTPClient.defaultEventLoopGroup, |
| 29 | + configuration: Configuration = Configuration(), |
| 30 | + backgroundActivityLogger: Logger? = nil, |
| 31 | + isolation: isolated (any Actor)? = #isolation, |
| 32 | + _ body: (HTTPClient) async throws -> Return |
| 33 | + ) async throws -> Return { |
| 34 | + let logger = (backgroundActivityLogger ?? HTTPClient.loggingDisabled) |
| 35 | + let httpClient = HTTPClient( |
| 36 | + eventLoopGroup: eventLoopGroup, |
| 37 | + configuration: configuration, |
| 38 | + backgroundActivityLogger: logger |
| 39 | + ) |
| 40 | + return try await asyncDo { |
| 41 | + try await body(httpClient) |
| 42 | + } finally: { _ in |
| 43 | + try await httpClient.shutdown() |
| 44 | + } |
| 45 | + } |
| 46 | + #else |
| 47 | + /// Start & automatically shut down a new ``HTTPClient``. |
| 48 | + /// |
| 49 | + /// This method allows to start & automatically dispose of a ``HTTPClient`` following the principle of Structured Concurrency. |
| 50 | + /// The ``HTTPClient`` is guaranteed to be shut down upon return, whether `body` throws or not. |
| 51 | + /// |
| 52 | + /// This may be particularly useful if you cannot use the shared singleton (``HTTPClient/shared``). |
| 53 | + public static func withHTTPClient<Return: Sendable>( |
| 54 | + eventLoopGroup: any EventLoopGroup = HTTPClient.defaultEventLoopGroup, |
| 55 | + configuration: Configuration = Configuration(), |
| 56 | + backgroundActivityLogger: Logger? = nil, |
| 57 | + _ body: (HTTPClient) async throws -> Return |
| 58 | + ) async throws -> Return { |
| 59 | + let logger = (backgroundActivityLogger ?? HTTPClient.loggingDisabled) |
| 60 | + let httpClient = HTTPClient( |
| 61 | + eventLoopGroup: eventLoopGroup, |
| 62 | + configuration: configuration, |
| 63 | + backgroundActivityLogger: logger |
| 64 | + ) |
| 65 | + return try await asyncDo { |
| 66 | + try await body(httpClient) |
| 67 | + } finally: { _ in |
| 68 | + try await httpClient.shutdown() |
| 69 | + } |
| 70 | + } |
| 71 | + #endif |
| 72 | +} |
0 commit comments