Skip to content

Commit

Permalink
Merge pull request #73 from dbsystel/develop
Browse files Browse the repository at this point in the history
All changes for 1.0
  • Loading branch information
lightsprint09 authored Jan 15, 2018
2 parents a72f917 + 60f4adf commit a13db0c
Show file tree
Hide file tree
Showing 53 changed files with 1,005 additions and 1,351 deletions.
46 changes: 18 additions & 28 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,32 @@ matrix:
before_install:
- wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import -
- cd ..
- export SWIFT_VERSION=swift-4.0-RELEASE
- wget https://swift.org/builds/swift-3.1-release/ubuntu1404/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu14.04.tar.gz
- export SWIFT_VERSION=swift-4.0.3-RELEASE
- wget https://swift.org/builds/swift-4.0.3-release/ubuntu1404/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu14.04.tar.gz
- tar xzf $SWIFT_VERSION-ubuntu14.04.tar.gz
- export PATH="${PWD}/${SWIFT_VERSION}-ubuntu14.04/usr/bin:${PATH}"
- cd DBNetworkStack
script:
- swift test --verbose
- os: osx
osx_image: xcode9
osx_image: xcode9.2
language: objective-c
env: "macOS"
before_install:
- gem install jazzy
script:
- set -o pipefail && xcodebuild -scheme DBNetworkStack -destination 'platform=OS X' test CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO -enableCodeCoverage NO | xcpretty
- os: osx
osx_image: xcode9
language: objective-c
env: "iOS"
script:
- set -o pipefail && xcodebuild -scheme DBNetworkStack -destination 'platform=iOS Simulator,name=iPhone SE,OS=latest' test CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO -enableCodeCoverage YES | xcpretty
- set -o pipefail && xcodebuild -scheme DBNetworkStack -destination "platform=macOS" test | xcpretty
- set -o pipefail && xcodebuild -scheme DBNetworkStack -destination "platform=tvOS Simulator,name=Apple TV" test | xcpretty
- set -o pipefail && xcodebuild -scheme DBNetworkStack -destination "platform=watchOS Simulator,name=Apple Watch - 38mm" build | xcpretty
- set -o pipefail && xcodebuild -scheme DBNetworkStack -destination "platform=iOS Simulator,name=iPhone SE" test | xcpretty
- swift test & pod spec lint --allow-warnings & carthage build --no-skip-current
- jazzy --clean --author "DBSystel" --github_url https://github.com/dbsystel/DBNetworkStack --module DBNetworkStack --output docs
after_success:
- bash <(curl -s https://codecov.io/bash)
- os: osx
osx_image: xcode9
language: objective-c
env: "watchOS"
script:
- set -o pipefail && xcodebuild -scheme DBNetworkStack -destination 'platform=watchOS Simulator,name=Apple Watch Series 2 - 38mm' build CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO | xcpretty
- os: osx
osx_image: xcode9
language: objective-c
env: "tvOS"
script:
- set -o pipefail && xcodebuild -scheme DBNetworkStack -destination 'platform=tvOS Simulator,name=Apple TV 1080p,OS=latest' test CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO -enableCodeCoverage NO | xcpretty
- os: osx
osx_image: xcode9
language: objective-c
env: "SPM, Carthage, CocoaPods"
script:
- swift test & pod spec lint & carthage build --no-skip-current
deploy:
provider: pages
local_dir: ${TRAVIS_BUILD_DIR}/docs
skip_cleanup: true
github_token: $GITHUB_TOKEN
on:
branch: master
3 changes: 1 addition & 2 deletions DBNetworkStack.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "DBNetworkStack"
s.version = "0.5.0"
s.version = "1.0.0"
s.summary = "DBNetworkStack is a network abstraction for fetching request and mapping them to model objects."

# This description is used to generate tags and improve search results.
Expand All @@ -27,7 +27,6 @@ Pod::Spec.new do |s|
s.description = "DBNetworkStack is a network abstraction for fetching request and mapping them to model objects. It supports mocking via protocols."

s.homepage = "https://github.com/dbsystel/DBNetworkStack"
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"


# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
114 changes: 44 additions & 70 deletions DBNetworkStack.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions JSONExample.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//: Playground - noun: a place where people can play

import DBNetworkStack
import PlaygroundSupport

Expand All @@ -9,7 +7,7 @@ URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)

//Prepare NetworkAccess & NetworkService
let networkAccess = URLSession(configuration: .default)
let networkService = NetworkService(networkAccess: networkAccess)
let networkService = BasicNetworkService(networkAccess: networkAccess)

struct IPOrigin: Decodable {
let origin: String
Expand All @@ -20,8 +18,8 @@ let request = URLRequest(path: "ip", baseURL: url)

let resource = Resource<IPOrigin>(request: request, decoder: JSONDecoder())

networkService.request(resource, onCompletion: { origin in
print(origin)
networkService.request(resource, onCompletionWithResponse: { origin, response in
print(origin, response)
}, onError: { error in
print(error)
})
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
| 🚄 | Extendable API |
| 🎹        | Composable Features           |
| &#9989; | Fully unit tested |
| 📕  | [Documented here](https://dbsystel.github.io/DBNetworkStack/)            |

The idea behind this project comes from this [talk.objc.io article](https://talk.objc.io/episodes/S01E01-networking).

## 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 ```NetworkAccessProviding```.
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```.

```swift

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

```

Expand All @@ -49,7 +50,7 @@ networkService.request(resource, onCompletion: { htmlText in

```

## Loade types conforming to `Decodable`
## Load types conforming to Swift-`Decodable`
```swift
struct IPOrigin: Decodable {
let origin: String
Expand All @@ -69,13 +70,13 @@ networkService.request(resource, onCompletion: { origin in

## Accessing HTTPResponse

Request your resource and handle the result & response. This is similar to just requesting a resulting model.
Request your resource and handle the result & http response. This is similar to just requesting a resulting model.
```swift
extension Resource where Model: XMLDocument {
public init(request: URLRequestConvertible) {
self.init(request: request, parse: { try XMLDocument(data: $0 })
}
}
networkService.request(resource, onCompletionWithResponse: { origin, response in
print(origin, response)
}, onError: { error in
//Handle errors
})
```

## Protocol oriented architecture / Exchangability
Expand All @@ -84,10 +85,10 @@ The following table shows all the protocols and their default implementations.

| Protocol | Default Implementation |
| -------------------------------- | ---------------------- |
| ```NetworkAccessProviding``` | ```URLSession``` |
| ```NetworkServiceProviding``` | ```NetworkService``` |
| ```NetworkAccess``` | ```URLSession``` |
| ```NetworkService``` | ```BasicNetworkService``` |
| ```URLRequestConvertible``` | ```URLRequest``` |
| ```NetworkTaskRepresenting``` | ```URLSessionTask``` |
| ```NetworkTask``` | ```URLSessionTask``` |

## Composable Features

Expand All @@ -111,7 +112,7 @@ The following table shows all the protocols and their default implementations.
Specify the following in your `Cartfile`:

```ogdl
github "dbsystel/dbnetworkstack" ~> 0.7
github "dbsystel/dbnetworkstack" ~> 1.0
```

### CocoaPods
Expand Down
6 changes: 2 additions & 4 deletions SimpleDemo.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//: Playground - noun: a place where people can play

import DBNetworkStack
import PlaygroundSupport

Expand All @@ -9,11 +7,11 @@ URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)

//Prepare NetworkAccess & NetworkService
let networkAccess = URLSession(configuration: .default)
let networkService = NetworkService(networkAccess: networkAccess)
let networkService = BasicNetworkService(networkAccess: networkAccess)

//Create a Resource with a given URLRequest and parsing
let url: URL! = URL(string: "https://bahn.de")
let request = URLRequest(path: "/p/view/index.shtml", baseURL: url)
let request = URLRequest(path: "p/view/index.shtml", baseURL: url)
let resource = Resource(request: request, parse: { String(data: $0, encoding: .utf8) })

networkService.request(resource, onCompletion: { htmlText in
Expand Down
88 changes: 88 additions & 0 deletions Source/BasicNetworkService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// Copyright (C) 2017 DB Systel GmbH.
// DB Systel GmbH; Jürgen-Ponto-Platz 1; D-60329 Frankfurt am Main; Germany; http://www.dbsystel.de/
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

import Foundation
import Dispatch

/**
`BasicNetworkService` handles network request for resources by using a given `NetworkAccess`.

**Example**:
```swift
// Just use an URLSession for the networkAccess.
let basicNetworkService: NetworkService = BasicNetworkService(networkAccess: URLSession(configuration: .default))
```

- seealso: `NetworkService`
*/
public final class BasicNetworkService: NetworkService {
let networkAccess: NetworkAccess
let networkResponseProcessor: NetworkResponseProcessor

/**
Creates an `BasicNetworkService` instance with a given network access to execute requests on.

- parameter networkAccess: provides basic access to the network.
*/
public init(networkAccess: NetworkAccess) {
self.networkAccess = networkAccess
self.networkResponseProcessor = NetworkResponseProcessor()
}

/**
Fetches a resource asynchronously from remote location. Execution of the requests starts immediately.
Execution happens on no specific queue. It dependes on the network access which queue is used.
Once execution is finished either the completion block or the error block gets called.
You decide on which queue these blocks get executed.

**Example**:
```swift
let networkService: NetworkService = //
let resource: Resource<String> = //

networkService.request(queue: .main, resource: resource, onCompletionWithResponse: { htmlText, response in
print(htmlText, response)
}, onError: { error in
// Handle errors
})
```

- parameter queue: The `DispatchQueue` to execute the completion and error block on.
- parameter resource: The resource you want to fetch.
- parameter onCompletionWithResponse: Callback which gets called when fetching and transforming into model succeeds.
- parameter onError: Callback which gets called when fetching or transforming fails.

- returns: a running network task
*/
@discardableResult
public func request<Result>(queue: DispatchQueue, resource: Resource<Result>, onCompletionWithResponse: @escaping (Result, HTTPURLResponse) -> Void,
onError: @escaping (NetworkError) -> Void) -> NetworkTask {
let request = resource.request.asURLRequest()
let dataTask = networkAccess.load(request: request, callback: { data, response, error in
self.networkResponseProcessor.processAsyncResponse(queue: queue, response: response, resource: resource, data: data,
error: error, onCompletion: onCompletionWithResponse, onError: onError)
})
return dataTask
}

}
6 changes: 1 addition & 5 deletions Source/DBNetworkStack.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//
// DBNetworkStack.h
//
// Copyright (C) 2016 DB Systel GmbH.
// Copyright (C) 2017 DB Systel GmbH.
// DB Systel GmbH; Jürgen-Ponto-Platz 1; D-60329 Frankfurt am Main; Germany; http://www.dbsystel.de/
//
// Permission is hereby granted, free of charge, to any person obtaining a
Expand All @@ -22,8 +20,6 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Created by Lukas Schmidt on 22.08.16.
//

#import <Foundation/Foundation.h>

Expand Down
8 changes: 2 additions & 6 deletions Source/HTTPMethod.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//
// HTTPMethod.swift
//
// Copyright (C) 2016 DB Systel GmbH.
// DB Systel GmbH; Jürgen-Ponto-Platz 1; D-60329 Frankfurt am Main; Germany; http://www.dbsystel.de/
// Copyright (C) 2017 DB Systel GmbH.
// DB Systel GmbH; Jürgen-Ponto-Platz 1; D-60329 Frankfurt am Main; Germany; http://www.dbsystel.de/
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand All @@ -22,8 +20,6 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Created by Lukas Schmidt on 21.07.16.
//

import Foundation

Expand Down
2 changes: 1 addition & 1 deletion Source/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.7.0</string>
<string>1.0.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
52 changes: 0 additions & 52 deletions Source/JSONArrayResource.swift

This file was deleted.

Loading

0 comments on commit a13db0c

Please sign in to comment.