Giter Club home page Giter Club logo

networklayerexample's Introduction

NetworkLayerExample

Improved version of the Network Layer solution by Tomasz Szulc presented in the article: http://szulctomasz.com/how-do-I-build-a-network-layer/

The idea is to create a modular network layer, 100% testable, that implements NSOperation to manage easily a requests queue, with priority, dependecy, cancel operation etc.

Architecture

diagram

  • Request

A struct that implement BackendAPIRequest protocol. It's an object that knows which endpoint to hit, what method to use, should it be GET, POST, PUT or different, how to configure the body of a request and what headers to pass.

public protocol BackendAPIRequest {
    var endpoint: String { get }
    var method: Method { get }
    var query: Query { get }
    var parameters: Parameters? { get }
    var headers: [String: String]? { get }
}
  • Mapper

The mapper separates the logic of parsing and mapping JSON to useful items.

There are two type of parsers:

  • The first type return just a single object of specific type.

  • The second type is a parser that parses array of such items.

  • Operation

This is the NSOperation object, ServiceOperation subclass, that instantiates a request, creates the network request and manage the network response.

To test this object we use dependency injection, passing a mock object that implements the BackendService protocol.

public init(uniqueId: String, service: BackendService = MyBackendService(BackendConfiguration.shared)) {
    request = UserShoppingRequest(uniqueId: uniqueId)
    super.init(service: service)
}
  • BackendService

It is the class that takes requests (Request objects described above) related to the backend. It uses NetworkService internally.

  • NetworkService

It allows you to execute HTTP request, it incorporates NSURLSession internally. Every network service can execute just one request at a time, can cancel the request (big advantage), and has callbacks for success and failure responses.

Implementation

For the implementation read the original blog post and take a look to Network/Request, Network/Operations and Network/Mappers.

Unit Test

You can find some test examples in NetworkLayerExampleTest/NetworkLayerExampleTest.swift.

networklayerexample's People

Contributors

darthpelo avatar fawxy avatar tomkowz avatar benpackard avatar

Watchers

Toan Quach 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.