DBNetworkStack 0.7

DBNetworkStack 0.7

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.

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 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
})

JSON Mapping Demo

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
})

Extendability

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.

Protocol oriented architecture / Exchangability

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>

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

Requirements

  • iOS 9.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 8.0+
  • Swift 3.0

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.