A simple swifty networking layer. Supports custom interceptions
let request = ListUsersRequest()
network.send(request) { result in
switch result {
case .failure(let error):
showError(error)
case .success(let users):
showUsers(users)
}
}
Looking for an Rx version? Checkout RxECNetworking
.package(url: "https://github.com/EvanCooper9/ECNetworking", from: "3.0.0")
Checkout the example project for detailed usage
Network
NetworkConfiguration
defines a base URL that is used for configuring all requests.
let configuration = NetworkConfiguration(
baseURL: URL(string: "https://example.com")!,
logging: true
)
let network = URLSessionNetwork(configuration: configuration)
To model a request, conform to Request
struct ListUsersRequest {
let online: Bool
}
extension ListUsersRequest: Request {
typealias Response = [User]
func buildRequest(with baseURL: URL) -> NetworkRequest {
let url = baseURL.appendingPathComponent("users")
return .init(method: .post, url: url, body: self)
}
}
Initialize a request and send it!
let request = ListUsersRequest(online: true)
network.send(request) { result in
switch result {
case .failure(let error):
// handle error
case .success(let response):
// handle response which is of type ListUsersRequest.Response = [User]
}
}
Actions are used to add custom behaviour during the lifecyle of a network request. Things like logging and adding authentication headers can be accomplished through Actions. There are 4 types of actions.
RequestWillBeginAction
- modify/handle a request before it beginsRequestBeganAction
- notifies a request has begunResponseBeganAction
- notifies a response has been receivedResponseCompletedAction
- modify/handle a response before passing it to the callerNote: A default logging action is available for use through the
logging
property ofNetworkConfiguration
. The example project has an additionalAuthenticationAction
example.
It may be useful to add other properties to a request. An authentication action, for example, might add authentication headers to requests before they're sent, but not all requests require authentication, like a login request.
Note: A
Request
is transformed to aNetworkRequest
throughbuildRequest(baseURL:)
when being sent. Additional information can be persisted throughcustomProperties
. When implementing actions, you can act on the data stored withincustomProperties
of a givenNetworkRequest
.
protocol MyRequest: Request {
var requiresAuthentication: Bool { get }
}
extension MyRequest {
// Provide default implementation if you want
var requiresAuthentication: Bool { true }
// Implement `customProperties` from `Request`
var customProperties: [AnyHashable: Any] {
["requiresAuthentication": requiresAuthentication]
}
}
extension NetworkRequest {
var requiresAuthentication: Bool {
customProperties["requiresAuthentication"] as? Bool ?? false
}
}
And then when modelling requests,
struct LoginRequest {
let username: String
let password: String
}
// Conform to `MyRequest` instead of `Request`
extension LoginRequest: MyRequest {
var requiresAuthentication: Bool { false }
// ...
}
And then in your action,
struct AuthenticationAction: RequestWillBeginAction {
func requestWillBegin(_ request: NetworkRequest, completion: @escaping RequestCompletion) {
guard request.requiresAuthentication else { return }
// Add authentication headers
}
}
ECNetworking is available under the MIT license. See the LICENSE file for more info.
link |
Stars: 2 |
Last commit: 3 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics