HPNetwork
is a protocol-based networking stack written in pure Swift
Starting with v4 HPNetwork is only available via Swift Package Manager.
.package(url: "https://github.com/henrik-dmg/HPNetwork", from: "4.0.0")
https://github.com/henrik-dmg/HPNetwork
Documentation has been moved to https://henrik-dmg.github.io/HPNetwork/. That way I don't have to copy it back and forth between the README and the .docc
catalogs.
link |
Stars: 2 |
Last commit: 1 week ago |
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.
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: https://github.com/henrik-dmg/HPNetwork/compare/3.1.2...4.0.0
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics