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