Code Monkey home page Code Monkey logo

ios-flamingo's Introduction

Flamingo

Build Status License

Description

Lightweight and easy to use Swift network manager. Based on URLSession and Swift.Codable.

Supported features:

  • Performing http requests
  • Easy response mapping
  • Easy response stubbing

Installation

With CocoaPods:

source 'https://github.com/elegion/ios-podspecs.git'
source 'https://github.com/CocoaPods/Specs.git'

pod 'Flamingo'

With Carthage:

github "elegion/ios-Flamingo"

Usage

Basic usage

Setup configuration

Create default network configuration:

let configuration = NetworkDefaultConfiguration(baseURL: "http://jsonplaceholder.typicode.com/")

Setup network client

let networkClient = NetworkDefaultClient(configuration: configuration, session: .shared)

Setup request info

Satisfy NetworkRequest protocol to add request (see more information below about response mapping):

struct UsersRequest: NetworkRequest {

    init() {

    }

    // MARK: - Implementation

    var URL: URLConvertible {
        return "users"
    }

    var useCache: Bool {
        return true
    }

    var responseSerializer: CodableJSONSerializer<[User]> {
        return CodableJSONSerializer<[User]>()
    }
}

Map responses

Map responses with custom implementation of ResponseSerialization or use one of the predefined DataResponseSerializer, StringResponseSerializer, CodableJSONSerializer:

struct UsersRequest: NetworkRequest {
    ...
    var responseSerializer: CodableJSONSerializer<[User]> {
        return CodableJSONSerializer<[User]>()
    }
    ...
}

class Address: Codable {
    var street: String
    var suite: String
    var city: String
    var geo: GeoLocation
}

class Company: Codable {
    var name: String
    var catchPhrase: String
    var bs: String
}

class User: Codable {
    var id: Int
    var name: String
    var username: String
    var email: String
    var address: Address
    var phone: String
    var website: String
    var company: Company
}

You can also create your own serializers. See ResponseSerialization for more details.

Send request

let request = UsersRequest()

networkClient.sendRequest(request) {
    (result, _) in

    switch result {
    case .success(let users):
        XCTAssert(!users.isEmpty, "Users array is empty")
    case .failure(let error):
        XCTFail("User not recieved, error: \(error)")
    }
    asyncExpectation.fulfill()
}

Client customization

Custom configuration types

Create custom configuration structure if you need more information to initialize client:

public struct NetworkCustomConfiguration: NetworkConfiguration {
    
    public let baseURL: URLStringConvertible?
    public let useMocks: Bool
    public let completionQueue: DispatchQueue
    public let defaultTimeoutInterval: TimeInterval
    public let clientToken: String?
    
    public init(baseURL: URLStringConvertible? = nil,
                debugMode: Bool = false,
                completionQueue: DispatchQueue = DispatchQueue.main,
                defaultTimeoutInterval: TimeInterval = 60.0,
                clientToken: String?) {
        
        self.baseURL = baseURL
        self.useMocks = useMocks
        self.debugMode = debugMode
        self.completionQueue = completionQueue
        self.defaultTimeoutInterval = defaultTimeoutInterval
        self.clientToken = clientToken
    }
}
...
let configuration = NetworkCustomConfiguration(baseURL: "http://jsonplaceholder.typicode.com/",
                                               clientToken: "202cb962ac59075b964b07152d234b70")

Stubs and mocks

There is StubsDefaultManager that can handle almost all mock logic, but still you can create your own, by conforming to NetworkClientMutater. StubsDefaultManager can be easily created from file by using StubsManagerFactory. StubsList.json is a great stubs file example. Stubs also can be added using protocol StubsManager methods.

Logging and reporting

To log requests and responses you can create instance of LogginClient and pass it to your network client using func addReporter(_ reporter: NetworkClientReporter, storagePolicy: StoragePolicy). LogginClient can be constructed with SimpleLogger or your own implementation of Logger protocol. Also OfflineCacheManager is a great example of implementing NetworkClientReporter, NetworkClientMutater protocols.

Codable extensions

https://github.com/jamesruston/CodableExtensions is integrated, so you don't need to embed them as a framework.

Requirements

Swift 4.1, xCode 9.1

Author

e-Legion

License

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

TODOs

  1. Documentation about stubs and mocks
  2. Redirect handle
  3. Null in stubs

P.S.

Pull requests are welcome ๐Ÿ’ช๐Ÿป

ios-flamingo's People

Contributors

aranoledur avatar arkadysmirnov avatar bzm97 avatar chebur avatar funkill avatar geor-kasapidi avatar gimungagap avatar kuler29 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.