CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | โ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Nov 2017 |
| SwiftSwift Version | 4.0 |
| SPMSupports SPM | โ |
Maintained by Lukas Schmidt.
| Main Features | |
|---|---|
| Typed network resources | |
| Protocol oriented architecture | |
| Exchangeable implementations | |
| Extendable API | |
|
|
Composable Features ย ย ย ย ย |
| Fully unit tested |
The idea behind this project comes from this talk.objc.io article.
Lets say you want to fetch a html string.
First you have to create a service, by providing a network access. You can use NSURLSession out of the box or provide your own custom solution by implementing NetworkAccessProviding. In addition you need to register baseURLs endpoints for request mapping. This gives you the flexibility to change your endpoints very easily when your environment changes.
let url = NSURL(string: "https://httpbin.org")!
let baseURLKey = "httpBin"
let networkAccess = NSURLSession(configuration: .defaultSessionConfiguration())
let networkService = NetworkService(networkAccess: networkAccess, endPoints: [baseURLKey: url])
Create a resource with a request to fetch your data.
let request = NetworkRequest(path: "/", baseURLKey: baseURLKey)
let resource = Resource(request: request, parse: { String(data: $0, encoding: NSUTF8StringEncoding) })
Request your resource and handle the response
networkService.request(resource, onCompletion: { htmlText in
print(htmlText)
}, onError: { error in
//Handle errors
})
struct IPOrigin {
let ipAddress: String
}
extension IPOrigin: JSONMappable {
init(object: Dictionary<String, AnyObject>) throws {
/// Do your mapping
}
}
let request = NetworkRequest(path: "/ip", baseURLKey: baseURLKey)
let resource = JSONResource<IPOrigin>(request: request)
networkService.request(resource, onCompletion: { origin in
print(origin)
}, onError: { error in
//Handle errors
})The following example outlines how to extend DBNetworkStack to support XML response models:
protocol XMLMappable {
init(object: Dictionary<String, AnyObject>) throws
}
struct XMLResource<T : XMLMappable> : ResourceModeling {
let request: NetworkRequestRepresening
init(request: NetworkRequestRepresening) {
self.request = request
}
var parse: (data: NSData) throws -> T {
return { data in
let xmlObject = // Your data to xml object conversion
try! T(object: xmlObject) as T
}
}
}XMLMappable defines the protocol, response model objects must conform to. The model class conforming to this protocol is responsible to convert a generic representation of the model into itโs specialized form.
XMLResource<T : XMLMappable> defines a resource based on a given XMLMappable model. The parse function is responsible of converting raw response data to a generic representation.
The following table shows all the protocols and their default implementations.
| Protocol | Default Implementation |
|---|---|
NetworkAccessProviding |
NSURLSession |
NetworkServiceProviding |
NetworkService |
NetworkRequestRepresenting |
NetworkRequest |
NetworkTaskRepresenting |
NSURLSessionTask |
ResourceModelling |
Resource<Model> |
| 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 |
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.
Lukas Schmidt (Mail, @lightsprint09), Christian Himmelsbach (Mail)
DBNetworkStack is released under the MIT license. See LICENSE for details.