Skip to content
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

Cannot run example code #51

Closed
X901 opened this issue May 25, 2022 · 3 comments
Closed

Cannot run example code #51

X901 opened this issue May 25, 2022 · 3 comments
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@X901
Copy link

X901 commented May 25, 2022

I tried your example , but I get error

Instance method 'fetch' requires that 'HTTPRequest' conform to 'APIResourceConvertible'"

That error appeared hear

public extension HTTPClient {
func fetch<T: APIResourceConvertible>(_ convertible: T) async throws -> T.Result {
let result = try await fetch(convertible.request())
return try result.decode(T.Result.self)
}
}
@ilai1823
Copy link

ilai1823 commented Jul 7, 2022

@X901 just specify the module as shown below:
public extension HTTPClient { func fetch<T: APIResourceConvertible>(_ convertible: T) async throws -> T.Result { let result = try await RealHTTP.fetch(convertible.request()) return try result.decode(T.Result.self) } }

@NicFontana
Copy link

NicFontana commented Jul 7, 2022

@ilai1823 almost.

If you use RealHTTP.fetch() it will always use the shared HTTPClient, see:

extension RealHTTP {
    /// Fetch the request with the default client.
    public static func fetch(_ request: HTTPRequest) async throws -> HTTPResponse {
        try await HTTPClient.shared.fetch(request)
    }
    ...
}

The way to go should be:

extension HTTPClient {
    func fetch<T: APIResourceConvertible>(_ convertible: T) async throws -> T.Result {
        let result = try await convertible.request().fetch(self)
        return try result.decode(T.Result.self)
    }
}

@malcommac malcommac added documentation Improvements or additions to documentation question Further information is requested labels Sep 12, 2022
@malcommac
Copy link
Collaborator

thanks for your support @NicFontana

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants