-
-
Notifications
You must be signed in to change notification settings - Fork 3
Genericize ResponseProviding to an HTTPSRequester #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c73c872
a3d22af
9708523
d8039c1
e0bf4f9
92e0a50
763b044
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // | ||
| // ResponseProviding.swift | ||
| // ATResolve | ||
| // | ||
| // Created by Mark @ Germ on 12/1/25. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| ///Allows the client to choose between URLSession (Foundation) or AsyncHTTPClient, | ||
| ///Or another networking library of their choice | ||
|
|
||
| //from Dave Delong | ||
| public struct HTTPMethod: Hashable, Sendable { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is indeed very much like NIOHTTP1.HTTPRequest (an enum), but defined here to avoid adding library dependency on Swift NIO. Source is |
||
| public static let get = HTTPMethod(rawValue: "GET") | ||
| public static let post = HTTPMethod(rawValue: "POST") | ||
| public static let put = HTTPMethod(rawValue: "PUT") | ||
| public static let delete = HTTPMethod(rawValue: "DELETE") | ||
|
|
||
| public let rawValue: String | ||
| } | ||
|
|
||
| public struct Request { | ||
| public let host: String | ||
| public let path: String | ||
| public let method: HTTPMethod | ||
| public let headers: [String: String] | ||
| public let queryItems: [(String, String?)] | ||
| } | ||
|
|
||
| public protocol ResponseProviding { | ||
| func data(for: Request) async throws -> Data | ||
| } | ||
|
|
||
| extension ResponseProviding { | ||
| func decodeJSON<T: Decodable>( | ||
| host: String, | ||
| path: String, | ||
| method: HTTPMethod = .get, | ||
| headers: [String: String] = [:], | ||
| queryItems: [(String, String)] = [] | ||
| ) async throws -> T { | ||
| let result = try await data( | ||
| for: .init( | ||
| host: host, | ||
| path: path, | ||
| method: method, | ||
| headers: headers, | ||
| queryItems: queryItems | ||
| ) | ||
| ) | ||
|
|
||
| return try JSONDecoder().decode(T.self, from: result) | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // | ||
| // ATResolveAsyncHTTPTests.swift | ||
| // ATResolve | ||
| // | ||
| // Created by Mark @ Germ on 12/1/25. | ||
| // | ||
|
|
||
| #if canImport(AsyncHTTPClient) | ||
| import ATResolve | ||
| import Foundation | ||
| import AsyncHTTPClient | ||
| import Testing | ||
|
|
||
| //Test API with AsyncHTTPClient | ||
| struct ATResolveAsyncHTTPTests { | ||
| @Test | ||
| func resolveHandle() async throws { | ||
| let resolver = ATResolver(requester: HTTPClient.shared) | ||
|
|
||
| let data = try await resolver.resolveHandle("massicotte.org") | ||
|
|
||
| #expect(data?.did == "did:plc:klsh7edzj3jmxucibyjqstb3") | ||
| #expect(data?.handle == "massicotte.org") | ||
| #expect(data?.serviceEndpoint == "https://milkcap.us-west.host.bsky.network") | ||
| } | ||
| } | ||
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Github actions throws an error about finding two targets for Vision Pro, so this unblocks the macOS tests from executing.