Skip to content

Commit 4af940a

Browse files
grdsdevclaude
andcommitted
refactor: move FetchTransportAdapter to Helpers module for reuse
- Move FetchTransportAdapter from Functions to Sources/Helpers/HTTP/ - Remove duplicate implementation from FunctionsClient - Keep package visibility for sharing across modules - Enables reuse for other module migrations in future PRs 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 8196dce commit 4af940a

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

Sources/Functions/FunctionsClient.swift

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,6 @@ import OpenAPIURLSession
1111

1212
let version = Helpers.version
1313

14-
/// A ClientTransport implementation that adapts the old Fetch api.
15-
struct FetchTransportAdapter: ClientTransport {
16-
let fetch: FunctionsClient.FetchHandler
17-
18-
init(fetch: @escaping FunctionsClient.FetchHandler) {
19-
self.fetch = fetch
20-
}
21-
22-
func send(
23-
_ request: HTTPTypes.HTTPRequest,
24-
body: HTTPBody?,
25-
baseURL: URL,
26-
operationID: String
27-
) async throws -> (HTTPTypes.HTTPResponse, HTTPBody?) {
28-
guard var urlRequest = URLRequest(httpRequest: request) else {
29-
throw URLError(.badURL)
30-
}
31-
32-
if let body {
33-
urlRequest.httpBody = try await Data(collecting: body, upTo: .max)
34-
}
35-
36-
let (data, response) = try await fetch(urlRequest)
37-
38-
guard let httpURLResponse = response as? HTTPURLResponse,
39-
let httpResponse = httpURLResponse.httpResponse
40-
else {
41-
throw URLError(.badServerResponse)
42-
}
43-
44-
let body = HTTPBody(data)
45-
return (httpResponse, body)
46-
}
47-
}
4814

4915
/// An actor representing a client for invoking functions.
5016
public final class FunctionsClient: Sendable {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Foundation
2+
import HTTPTypes
3+
import HTTPTypesFoundation
4+
import OpenAPIRuntime
5+
6+
#if canImport(FoundationNetworking)
7+
import FoundationNetworking
8+
#endif
9+
10+
/// A ClientTransport implementation that adapts the old Fetch api.
11+
package struct FetchTransportAdapter: ClientTransport {
12+
let fetch: @Sendable (_ request: URLRequest) async throws -> (Data, URLResponse)
13+
14+
package init(fetch: @escaping @Sendable (_ request: URLRequest) async throws -> (Data, URLResponse)) {
15+
self.fetch = fetch
16+
}
17+
18+
package func send(
19+
_ request: HTTPTypes.HTTPRequest,
20+
body: HTTPBody?,
21+
baseURL: URL,
22+
operationID: String
23+
) async throws -> (HTTPTypes.HTTPResponse, HTTPBody?) {
24+
guard var urlRequest = URLRequest(httpRequest: request) else {
25+
throw URLError(.badURL)
26+
}
27+
28+
if let body {
29+
urlRequest.httpBody = try await Data(collecting: body, upTo: .max)
30+
}
31+
32+
let (data, response) = try await fetch(urlRequest)
33+
34+
guard let httpURLResponse = response as? HTTPURLResponse,
35+
let httpResponse = httpURLResponse.httpResponse
36+
else {
37+
throw URLError(.badServerResponse)
38+
}
39+
40+
let body = HTTPBody(data)
41+
return (httpResponse, body)
42+
}
43+
}

0 commit comments

Comments
 (0)