Giter Site home page Giter Site logo

conn's Introduction

Conn

CI Status Version License Platform

Introduction

When writing networking layers, it´s common to end with a lot of boilerplate or repeated code.

There are currently some libraries that one can use in order to improve code readability and to avoid writing boilerplate, but they are usually so bloated that one doesn´t use the half of what that library offers.

On the other hand, there are some other libraries that are lightweight, but they often fall short when you need more advanced functionality.

Conn is the library that resolves this issue. It is very lightweight (118 lines counting empty lines until now), but it is still highly modular while it doesn't require the developer to write any boilerplate.

You can read more about the reasoning behind this library in my article in Medium.

Features

  • Declarative style
  • Very lightweight
  • Parses JSON to models
  • Replaceable network dispatcher
  • Minimal boilerplate required

Usage

The first thing you have to do is describing your requests as structs, classes or enums conforming to the RequestType protocol. For instance:

struct GetAllUsers: RequestType {
    typealias ResponseType = [User]
    var data: RequestData {
        return RequestData(path: "https://jsonplaceholder.typicode.com/users")
    }
}

ResponseType is an associatedtype that declares the format of the response. In this case, we expect to have an array of User as a response. The type associated to the request must implement Codable in order to be parsed. Take User as an example:

struct User: Codable {
    let id: Int
    let username: String
}

RequestData is a plain struct that defines the format of the request. In this case we are only defining a path (url), but RequestData also supports defining a http method, headers and params (body).

This is all you have to do. Then, to execute the request, RequestType has a method called execute() that actually dispatches the request:

GetAllUsers().execute(
    onSuccess: { (users: [User]) in
        // Do something with users
    },
    onError: { (error: Error) in
        // Do something with error
    }
)

Please note that users are already parsed as an array of User.

Advanced Usage

If you need to do something more complex, use other networking library like Alamofire, or anything else, the execute method in RequestType accepts an optional argument named dispatcher of type NetworkDispatcher.

NetworkDispatcher is a protocol that only requires implementing a single method called dispatch:

public protocol NetworkDispatcher {
    func dispatch(request: RequestData, onSuccess: @escaping (Data) -> Void, onError: @escaping (Error) -> Void)
}

By default, dispatch uses URLSessionNetworkDispatcher that dispatches the request using URLSession. But you can define your own NetworkDispatcher as you want.

Example

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

Requirements

Installation

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

pod 'Conn'

Author

fmo91, [email protected]

License

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

conn's People

Contributors

fmo91 avatar sgbasaraner avatar

Watchers

James Cloos 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.