First you have to create a service, by providing a network access. You can use URLSession out of the box or provide your own custom solution by implementing NetworkAccess.
let networkAccess = URLSession(configuration: .default)
let networkService = BasicNetworkService(networkAccess: networkAccess)
Create a resource with a request to fetch your data.
let url = URL(staticString: "https://httpbin.org")
let request = URLRequest(path: "/", baseURL: url, HTTPMethod: .GET)
let resource = Resource(request: request, parse: { String(data: $0, encoding: .utf8) })
Request your resource and handle the result
networkService.request(resource, onCompletion: { htmlText in
print(htmlText)
}, onError: { error in
//Handle errors
})
Load types conforming to Swift-Decodable
struct IPOrigin: Decodable {
let origin: String
}
let url = URL(staticString: "https://www.httpbin.org")
let request = URLRequest(path: "ip", baseURL: url)
let resource = Resource<IPOrigin>(request: request, decoder: JSONDecoder())
networkService.request(resource, onCompletion: { origin in
print(origin)
}, onError: { error in
//Handle errors
})
Accessing HTTPResponse
Request your resource and handle the result & http response. This is similar to just requesting a resulting model.
networkService.request(resource, onCompletionWithResponse: { origin, response in
print(origin, response)
}, onError: { error in
//Handle errors
})
Protocol oriented architecture / Exchangability
The following table shows all the protocols and their default implementations.
Protocol
Default Implementation
NetworkAccess
URLSession
NetworkService
BasicNetworkService
NetworkTask
URLSessionTask
Composable Features
Class
Feature
RetryNetworkService
Retrys requests after a given delay when an error meets given criteria.
ModifyRequestNetworkService
Modify matching requests. Can be used to add auth tokens or API Keys
NetworkServiceMock
Mocks a NetworkService. Can be use during unit tests
Feel free to submit a pull request with new features, improvements on tests or documentation and bug fixes. Keep in mind that we welcome code that is well tested and documented.
DBNetworkStack
The idea behind this project comes from this talk.objc.io article.
Basic Demo
Lets say you want to fetch a
html
string.First you have to create a service, by providing a network access. You can use URLSession out of the box or provide your own custom solution by implementing
NetworkAccess
.Create a resource with a request to fetch your data.
Request your resource and handle the result
Load types conforming to Swift-
Decodable
Accessing HTTPResponse
Request your resource and handle the result & http response. This is similar to just requesting a resulting model.
Protocol oriented architecture / Exchangability
The following table shows all the protocols and their default implementations.
NetworkAccess
URLSession
NetworkService
BasicNetworkService
NetworkTask
URLSessionTask
Composable Features
RetryNetworkService
ModifyRequestNetworkService
NetworkServiceMock
Requirements
Installation
Swift Package Manager
SPM is integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.
Specify the following in your
Package.swift
:Contributing
Feel free to submit a pull request with new features, improvements on tests or documentation and bug fixes. Keep in mind that we welcome code that is well tested and documented.
Contact
Lukas Schmidt (Mail, @lightsprint09), Christian Himmelsbach (Mail)
License
DBNetworkStack is released under the MIT license. See LICENSE for details.