Releases: henrik-dmg/HPNetwork
swift-http-types
Overview
This release migrates a lot of the types to use the swift-http-types
package by Apple. It brings breaking changes, so new major version.
Mocking
There's a new NetworkClientProtocol
type that you can schedule network requests on. To mock network requests, you can use NetworkClientMock
from the new HPNetworkMock
library.
Example usage:
networkClient.mockRequest(ofType: SomeRequest.self) { _ in
ReturnTypeOfRequest() // or throw an error
}
// To remove all mocks
networkClient.removeAllMocks()
You can also specify whether NetworkClientMock
should just fall-back to regular networking if there are no mocks configured for the request it's about the execute by using fallbackToURLSessionIfNoMatchingMock
If you need to go deeper, for example if you don't want to migrate to NetworkClient
, you can use URLSessionMock
to use with URLSession
Example usage of URLSessionMock
:
lazy var mockedURLSession: URLSession = {
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [URLSessionMock.self]
return URLSession(configuration: configuration)
}()
// ...
_ = URLSessionMock.mockRequest(to: url, ignoresQuery: false) { _ in
let response = HTTPURLResponse(
url: url,
statusCode: 200,
httpVersion: nil,
headerFields: ["Content-Type": ContentType.applicationJSON.rawValue]
)!
return (someDataYouWant, response)
}
Full Changelog: 3.1.2...4.0.0
Throwing URL Construction
This release is not really a v2 release, but it brings breaking changes. The url: URL?
property on NetworkRequest
has been removed and replaced by makeURL() throws -> URL
Synchronous requests and more
This release brings a couple new features and changes:
- you can schedule requests synchronously by calling
scheduleSynchronously(...
on aNetwork
instance or directly on your request instance. This will return the result of the request directly instead of aNetworkTask
- the order of arguments for
URLBuilder
query items has been changed to match that ofURLQueryItem
- you can now opt out of the JSON injection for
DecodableRequest
by providinginjectJSONOnError: Bool
Some QOL improvements
Not much new in this one, here's an overview:
musicUserToken
is not an authentication method anymore but rather a static property ofNetworkRequestHeaderField
(.musicUserToken(userToken)
)- the method
urlRequest() -> URLRequest
onNetworkRequest
is now private (this wasn't meant to be configurable anyways) - you can now schedule a request directly by passing a
Network
instance to it, like this for example:
// ...
request.schedule { result in
// Handle result
}
Ability to supply custom URLSession
You are now required to supply a URLSession
object to any type that conforms to NetworkRequest
- the built in DecodableRequest
and ImageDownloadRequest
already support this in their initialiser