Latest 0.6.0
License MIT
Platforms ios 10.0

CI Status


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



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

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

NetworkLayer().get(urlString: "",
                   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: "",
                    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
            self.view?.displayEmployees(employees: employees)
        let errorHandler: (String) -> Void = { (error) in
            self.view?.displayError(error: error)

        networkLayer.get(urlString: "",
                         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()

class DummyViewController: ViewProtocol {
    var displayEmployeesCalled = false

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

PresenterTests under Example project shows all variants of using DummyNetworkLayer.


Swapnil Sankla, [email protected]


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

Latest podspec

    "name": "Swift_SimpleNetworkLibrary",
    "version": "0.6.0",
    "summary": "A simple and lightweight network library.",
    "description": "A simple and lightweight network library. The intent is also demonstrate how easy it is to build a network layer based on URLSession. Mostly on projects we use libraries like Alamofire but never explore how things actually work under the hood.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Swapnil Sankla": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.6.0"
    "social_media_url": "",
    "platforms": {
        "ios": "10.0"
    "source_files": "Swift_SimpleNetworkLibrary/Classes/**/*",
    "swift_version": "3.2"

Pin It on Pinterest

Share This