BottleRocketStudios / iOS-Hyperspace

An extremely lightweight wrapper around URLSession to make working with APIs a breeze.
Apache License 2.0
47 stars 17 forks source link

Improve response handling of non-JSON #114

Closed earlgaspard closed 2 years ago

earlgaspard commented 4 years ago

Implement a way for Hyperspace to handle non-JSON responses such as HTML

tylermilner commented 4 years ago

Related to #115? (using transformSuccess(_:), you should be able to go from Data to an HTML String or any custom struct that represents HTML)

earlgaspard commented 4 years ago

Is that (using transformSuccess(_:)) something Hyperspace does or something a user has to implement?

tylermilner commented 4 years ago

Hyperspace provides a default implementation of transformSuccess(_:) when the request's ResponseType conforms to Decodable. The user would need to implement it for special scenarios like parsing errors from 200 OK responses.

wmcginty commented 2 years ago

I'm going to close this one out - if the user is wanting to use a non-Decodable type, they can use the following initializer on Request, where they are free to convert TransportSuccess into Response in any way desired.

public init(method: HTTP.Method, url: URL, headers: [HTTP.HeaderKey: HTTP.HeaderValue]? = nil, 
 body: HTTP.Body? = nil, cachePolicy: URLRequest.CachePolicy = RequestDefaults.defaultCachePolicy,
 timeout: TimeInterval = RequestDefaults.defaultTimeout, successTransformer: @escaping Transformer)