Skip to content

Repository files navigation

DBNetworkStack

Build Statuscodebeat badgecodecovCarthage CompatibleSwift Package Manager compatibleVersion

Main Features
🛡Typed network resources
🏠Value oriented architecture
🔀Exchangeable implementations
🚄Extendable API
🎹 Composable Features
Fully unit tested
📕 Documented here

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.

letnetworkAccess=URLSession(configuration:.default)letnetworkService=BasicNetworkService(networkAccess: networkAccess)

Create a resource with a request to fetch your data.

leturl=URL(string:"https://httpbin.org")!
letrequest=URLRequest(path:"/", baseURL: url)letresource=Resource(request: request, parse:{String(data: $0, encoding:.utf8)})

Request your resource and handle the result

networkService.request(resource, onCompletion:{ htmlText inprint(htmlText)}, onError:{ error in
//Handle errors
})

Load types conforming to Swift-Decodable

structIPOrigin:Decodable{letorigin:String}leturl:URL!=URL(string:"https://www.httpbin.org")letrequest=URLRequest(path:"ip", baseURL: url)letresource=Resource<IPOrigin>(request: request, decoder:JSONDecoder())
networkService.request(resource, onCompletion:{ origin inprint(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 inprint(origin, response)}, onError:{ error in
//Handle errors
})

Protocol oriented architecture / Exchangability

The following table shows all the protocols and their default implementations.

ProtocolDefault Implementation
NetworkAccessURLSession
NetworkServiceBasicNetworkService
URLRequestConvertibleURLRequest
NetworkTaskURLSessionTask

Composable Features

ClassFeature
RetryNetworkServiceRetrys requests after a given delay when an error meets given criteria.
ModifyRequestNetworkServiceModify 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 9.0+
  • Swift 3.2/Swift4.0

Installation

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

Specify the following in your Cartfile:

github "dbsystel/dbnetworkstack" ~> 1.0

CocoaPods

pod "DBNetworkStack"

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.

About

DBNetworkStack is a network abstraction for fetching request and mapping them to model objects

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages