GithubHelp home page GithubHelp logo

kaqu / dbnetworkstack Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dbsystel/dbnetworkstack

0.0 2.0 0.0 4.83 MB

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

License: MIT License

Ruby 3.25% Swift 95.53% Objective-C 1.21%

dbnetworkstack's Introduction

DBNetworkStack

Build Status codebeat badge codecov Carthage Compatible Swift Package Manager compatible Version

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.

let networkAccess = URLSession(configuration: .default)
let networkService = BasicNetworkService(networkAccess: networkAccess)

Create a resource with a request to fetch your data.

let url = URL(string: "https://httpbin.org")!
let request = URLRequest(path: "/", baseURL: url)
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! = URL(string: "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
URLRequestConvertible URLRequest
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

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.

dbnetworkstack's People

Contributors

lightsprint09 avatar ch-one avatar dennispost avatar philippseibel avatar

Watchers

James Cloos avatar Kacper avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.