Skip to content
A simple and lightweight network library. The intent is also demonstrate how easy it is to build a network layer based on URLSession. Unit testing and stubbing the network layer is also very easy.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Example Easy unit testing Jul 10, 2018
Swift_SimpleNetworkLibrary Bug fix in POST method Jul 10, 2018
.gitignore
.travis.yml Update travis settings Jul 9, 2018
DummyNetworkLayer.swift
LICENSE Initial commit Jul 9, 2018
README.md
Swift_SimpleNetworkLibrary.podspec Bug fix in POST method Jul 10, 2018
_Pods.xcodeproj Initial commit Jul 9, 2018

README.md

Swift_SimpleNetworkLibrary

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

Swift_SimpleNetworkLibrary is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Swift_SimpleNetworkLibrary'

GET Request

Let's try to use a cool REST API http://dummy.restapiexample.com/api/v1/employees. This API provides us a dummy set of employees. The employee looks something like below.

struct Employee: Codable {
    let employee_name: String
    let employee_salary: String
    let employee_age: String
}

Now to fetch employee details from the API, we just need to call get method on NetworkLayer.

let successHandler: ([Employee]) -> Void = { (employees) in
  print(employees)
}

let errorHandler: (String) -> Void = { (error) in
  print(error)
}

NetworkLayer().get(urlString: "http://dummy.restapiexample.com/api/v1/employees",
                   headers: ["key": "value"],
                   successHandler: successHandler,
                   errorHandler: errorHandler)

The coolest part is the Codable protocol which makes the network layer generic. Also check that Swift's strong type inference incredibly helps us as while calling get method, we don't specify the type in which we are expecting a response. Cool isn't it?

POST Request

POST is equally simple!

let emp1 = Employee(name: "Test_123", salary: "12234", age: "22")
NetworkLayer().post(urlString: "http://dummy.restapiexample.com/api/v1/create",
                    headers: ["key": "value"],
                    body: emp1,
                    errorHandler: errorHandler)

Unit testing

We all know that Swift recommends to not to use mocks and rather rely on the other types of test doubles. This package contains a dummy network layer for simplifying the unit testing of the caller methods.

Let's assume you want to test getExample method from the code below.

class Presenter {
    let networkLayer: NetworkLayer
    var view: ViewProtocol?

    init(view: ViewProtocol,
         networkLayer: NetworkLayer = NetworkLayer()) {
        self.view = view
        self.networkLayer = networkLayer
    }

    func getExample() {
        let successHandler: ([Employee]) -> Void = { (employees) in
            print(employees)
            self.view?.displayEmployees(employees: employees)
        }
        let errorHandler: (String) -> Void = { (error) in
            print(error)
            self.view?.displayError(error: error)
        }

        networkLayer.get(urlString: "http://dummy.restapiexample.com/api/v1/employees",
                         successHandler: successHandler,
                         errorHandler: errorHandler)
    }

Below is how you write the test. We get the DummyNetworkLayer out of the box.

class PresenterTests: XCTestCase {
    let view = DummyViewController()
    let networkLayer = DummyNetworkLayer()

    func test_getExample_callsDisplayEmployees_onSuccess() {
        networkLayer.successResponse = [Employee(name: "Swapnil",
                                                 salary: "123456",
                                                 age: "30")]
        Presenter(view: view, networkLayer: networkLayer).getExample()
        XCTAssertTrue(view.displayEmployeesCalled)
    }

class DummyViewController: ViewProtocol {
    var displayEmployeesCalled = false

    func displayEmployees(employees: [Employee]) {
        displayEmployeesCalled = true
    }

PresenterTests under Example project shows all variants of using DummyNetworkLayer.

Author

Swapnil Sankla, swapnil.sankla@gmail.com

License

Swift_SimpleNetworkLibrary is available under the MIT license. See the LICENSE file for more info.

You can’t perform that action at this time.