Skip to content

Commit 02a5d4f

Browse files
committed
migrate to use async-http-client instead of URLSession
Apparently swift-corelibs-foundation is missing async URLSession methods :/ See swiftlang/swift-corelibs-foundation#3205 Unfortunately this change also means swift-log-matrix now has transitive dependencies on the entire NIO stack, which is quite a lot.
1 parent bde33dd commit 02a5d4f

File tree

4 files changed

+98
-25
lines changed

4 files changed

+98
-25
lines changed

Diff for: Package.resolved

+72
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
{
22
"pins" : [
3+
{
4+
"identity" : "async-http-client",
5+
"kind" : "remoteSourceControl",
6+
"location" : "https://github.com/swift-server/async-http-client.git",
7+
"state" : {
8+
"revision" : "864c8d9e0ead5de7ba70b61c8982f89126710863",
9+
"version" : "1.15.0"
10+
}
11+
},
12+
{
13+
"identity" : "swift-atomics",
14+
"kind" : "remoteSourceControl",
15+
"location" : "https://github.com/apple/swift-atomics.git",
16+
"state" : {
17+
"revision" : "ff3d2212b6b093db7f177d0855adbc4ef9c5f036",
18+
"version" : "1.0.3"
19+
}
20+
},
21+
{
22+
"identity" : "swift-collections",
23+
"kind" : "remoteSourceControl",
24+
"location" : "https://github.com/apple/swift-collections.git",
25+
"state" : {
26+
"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
27+
"version" : "1.0.4"
28+
}
29+
},
330
{
431
"identity" : "swift-log",
532
"kind" : "remoteSourceControl",
@@ -8,6 +35,51 @@
835
"revision" : "32e8d724467f8fe623624570367e3d50c5638e46",
936
"version" : "1.5.2"
1037
}
38+
},
39+
{
40+
"identity" : "swift-nio",
41+
"kind" : "remoteSourceControl",
42+
"location" : "https://github.com/apple/swift-nio.git",
43+
"state" : {
44+
"revision" : "9b2848d76f5caad08b97e71a04345aa5bdb23a06",
45+
"version" : "2.49.0"
46+
}
47+
},
48+
{
49+
"identity" : "swift-nio-extras",
50+
"kind" : "remoteSourceControl",
51+
"location" : "https://github.com/apple/swift-nio-extras.git",
52+
"state" : {
53+
"revision" : "cc1e5275079380c859417dbea8588531f1a90ec3",
54+
"version" : "1.18.0"
55+
}
56+
},
57+
{
58+
"identity" : "swift-nio-http2",
59+
"kind" : "remoteSourceControl",
60+
"location" : "https://github.com/apple/swift-nio-http2.git",
61+
"state" : {
62+
"revision" : "38feec96bcd929028939107684073554bf01abeb",
63+
"version" : "1.25.2"
64+
}
65+
},
66+
{
67+
"identity" : "swift-nio-ssl",
68+
"kind" : "remoteSourceControl",
69+
"location" : "https://github.com/apple/swift-nio-ssl.git",
70+
"state" : {
71+
"revision" : "4fb7ead803e38949eb1d6fabb849206a72c580f3",
72+
"version" : "2.23.0"
73+
}
74+
},
75+
{
76+
"identity" : "swift-nio-transport-services",
77+
"kind" : "remoteSourceControl",
78+
"location" : "https://github.com/apple/swift-nio-transport-services.git",
79+
"state" : {
80+
"revision" : "c0d9a144cfaec8d3d596aadde3039286a266c15c",
81+
"version" : "1.15.0"
82+
}
1183
}
1284
],
1385
"version" : 2

Diff for: Package.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ let package = Package(
2020
dependencies: [
2121
// Dependencies declare other packages that this package depends on.
2222
// .package(url: /* package url */, from: "1.0.0"),
23-
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0")
23+
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
24+
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.15.0"),
2425
],
2526
targets: [
2627
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2728
// Targets can depend on other targets in this package, and on products in packages this package depends on.
2829
.target(
2930
name: "LoggingMatrix",
3031
dependencies: [
31-
.product(name: "Logging", package: "swift-log")
32+
.product(name: "Logging", package: "swift-log"),
33+
.product(name: "AsyncHTTPClient", package: "async-http-client"),
3234
]),
3335
.testTarget(
3436
name: "LoggingMatrixTests",

Diff for: Sources/LoggingMatrix/MatrixLogHandler.swift

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1+
import AsyncHTTPClient
12
import Foundation
23
import Logging
3-
4-
#if canImport(FoundationNetworking)
5-
import FoundationNetworking
6-
#endif
4+
import NIO
75

86
public class MatrixLogHandler: LogHandler {
97

@@ -15,6 +13,8 @@ public class MatrixLogHandler: LogHandler {
1513
public var logLevel: Logger.Level
1614
private var showLocation: Bool
1715

16+
private var httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
17+
1818
private var timestamp: String {
1919
var buffer = [Int8](repeating: 0, count: 25)
2020
var timestamp = time(nil)
@@ -62,6 +62,10 @@ public class MatrixLogHandler: LogHandler {
6262
self.showLocation = showLocation
6363
}
6464

65+
deinit {
66+
_ = self.httpClient.shutdown()
67+
}
68+
6569
public func log(
6670
level: Logger.Level,
6771
message: Logger.Message,
@@ -98,31 +102,27 @@ public class MatrixLogHandler: LogHandler {
98102
let pathComponents = ["/_matrix", "/client", "/r0", "/rooms", "/\(self.roomID)", "/send", "/m.room.message"]
99103
let sendURL = pathComponents.reduce(self.homeserver, { $0.appendingPathComponent($1) })
100104
guard var urlComponents = URLComponents(url: sendURL, resolvingAgainstBaseURL: false) else {
101-
throw Error.urlComponents
105+
throw Error.invalidURL
102106
}
103107
urlComponents.queryItems = [URLQueryItem(name: "access_token", value: self.accessToken)]
104108

105-
guard var request = urlComponents.url.map({ URLRequest(url: $0) }) else { throw Error.urlComponents }
106-
request.httpMethod = "POST"
107-
request.httpBody = payload
108-
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
109-
request.addValue("application/json", forHTTPHeaderField: "Accept")
110-
111-
let (data, resp) = try await URLSession.shared.data(for: request)
112-
113-
do {
114-
_ = try JSONDecoder().decode(EventResponse.self, from: data)
115-
} catch {
116-
throw Error.invalidResponse(
117-
statusCode: (resp as? HTTPURLResponse)?.statusCode,
118-
message: String(data: data, encoding: .utf8)
119-
)
109+
guard var request = urlComponents.url.map({ HTTPClientRequest(url: $0.absoluteString) }) else {
110+
throw Error.invalidURL
111+
}
112+
request.method = .POST
113+
request.body = .bytes(ByteBuffer(bytes: payload))
114+
request.headers.add(name: "Content-Type", value: "application/json")
115+
request.headers.add(name: "Accept", value: "application/json")
116+
117+
let response = try await httpClient.execute(request, timeout: .seconds(30))
118+
guard response.status == .ok else {
119+
throw Error.invalidResponse(statusCode: response.status.code)
120120
}
121121
}
122122

123123
private enum Error: Swift.Error {
124-
case urlComponents
125-
case invalidResponse(statusCode: Int?, message: String?)
124+
case invalidURL
125+
case invalidResponse(statusCode: UInt)
126126
}
127127

128128
private struct EventResponse: Decodable {

Diff for: Sources/LoggingMatrix/Message.swift

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Foundation
21
import Logging
32

43
extension MatrixLogHandler {

0 commit comments

Comments
 (0)