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

Remove Query Parameters from NetworkRequest #37

Closed wmcginty closed 6 years ago

wmcginty commented 6 years ago

It seems like one of the biggest complexities that NetworkRequest is currently set up to deal with is the encoding of the URL query. Would it make sense to remove that functionality from NetworkRequest and either create a separate builder object for the request's URL or leave it out of Hyperspace altogether (similarly to how URLRequest does not concern itself with the components of the URL?

It would simplify the NetworkRequest protocol drastically:

public protocol NetworkRequest {

    /// The model type that this NetworkRequest will attempt to transform Data into.
    associatedtype ResponseType
    associatedtype ErrorType: Swift.Error

    /// The HTTP method to be use when executing this request.
    var method: HTTP.Method { get }

    /// The URL to use when executing this network request.
    var url: URL { get }

    /// The header field keys/values to use when executing this network request.
    var headers: [HTTP.HeaderKey: HTTP.HeaderValue]? { get }

    /// The payload body for this network request, if any.
    var body: Data? { get }

    /// The cache policy to use when executing this network request.
    var cachePolicy: URLRequest.CachePolicy { get }

    /// The timeout to use when executing this network request.
    var timeout: TimeInterval { get }

    /// The URLRequest that represents this network request.
    var urlRequest: URLRequest { get }

    /// Attempts to parse the provided Data into the associated response model type for this request.
    ///
    /// - Parameter data: The raw Data retrieved from the network.
    /// - Returns: A result indicating the successful or failed transformation of the data into the associated response type.
    func transformData(_ data: Data) -> Result<ResponseType, ErrorType>
}
tylermilner commented 6 years ago

It definitely would simplify things. I like your suggestion about having a separate builder object. I wouldn't want to remove the functionality completely since I think URL-encoding query parameters is pretty standard stuff that we wouldn't want our projects to rewrite every time.