Frisbee


项目主页    by: ronanrodrigo     Star:    Fork:     

Frisbee

Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application.

Build Status CocoaPods CocoaPods Carthage codecov codebeat badge Join at Telegram Linux Compatible

Install

Carthage

To integrate Frisbee into your Xcode project using Carthage, specify it in your Cartfile:

github "ronanrodrigo/Frisbee" ~> 0.1.6

Run carthage update to build the framework and drag the built Frisbee.framework into your Xcode project.

CocoaPods

To integrate Frisbee into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'Frisbee', '0.1.6'
end

Then, run the following command:

$ pod install

Swift Package Manager

To integrate Frisbee into your Swift Package Manager project, set the dependencies in your Package.swift:

// swift-tools-version:4.0

import PackageDescription

let package = Package(
    name: "<Your Packege Name>",
    dependencies: [
        .package(url: "https://github.com/ronanrodrigo/Frisbee.git", from: "0.1.6")
    ],
    targets: [
        .target(name: "<Your Packege Name>", dependencies: ["Frisbee"])
    ]
)

Usage

Create a decodable entity

struct Movie: Decodable {
    let name: String
}

This is an example of some code that will request some data across network

class MoviesController {
    private let getRequest: Getable
    var moviesQuantity = 0
    // Expect something that conforms to Getable
    init(getRequest: Getable) {
        self.getRequest = getRequest
    }

    func didTouchAtListMovies() {
        getRequest.get(url: "http://www.com.br/movies.json") { (moviesResult: Result<[Movie]>) in
            switch moviesResult {
                case let .success(movies): self.moviesQuantity = movies.count
                case let .fail(error): print(error)
            }
        }
    }
}

In production-ready code you must inject an instance of NetworkGetter.

// Who will call the MoviesController must inject a NetworkGetter instance
MoviesController(getRequest: NetworkGetter())

Query parameters

It is easy to use query strings paramenters. Just create an Encodable struct and use it in get(url:query:onComplete) method.

struct MovieQuery: Encodable {
    let page: Int
}
let query = MovieQuery(page: 10)
NetworkGetter().get(url: url, query: query) { (result: Result<Movie>) in
    // ...
}

Usage in tests

In test target code you can create your own Getable mock.

public class MockGetter: Getable {
    var decodableMock: Decodable!

    public func get<Entity: Decodable>(url: URL, completionHandler: @escaping (Result<Entity>) -> Void) {
        get(url: url.absoluteString, completionHandler: completionHandler)
    }

    public func get<Entity: Decodable>(url: String, completionHandler: @escaping (Result<Entity>) -> Void) {
        if let decodableMock = decodableMock as? Entity {
            completionHandler(.success(decodableMock))
        }
    }

}

And instead NetworkGetter you will use to test the MockGetter on MoviesController

class MoviesControllerTests: XCTestCase {
    func testDidTouchAtListMoviesWhenHasMoviesThenPresentAllMovies() {
        let mockGet = MockGetter()
        let movies = [Movie(name: "Star Wars")]
        mockGet.decodableMock = movies
        let controller = MoviesController(getRequest: mockGet)

        controller.didTouchAtListMovies()

        XCTAssertEqual(controller.moviesQuantity, movies.count)
    }
}

Frisbee next features

  • Get request
  • Create Carthage distribution
  • Create Cocoapod distribution
  • Query parameter builder
  • Issue # 8 Implement other HTTP verbs
  • Issue # 7 Ready for use mock
  • Issue # 2 Propagate Swift Errors

Telegram

To collaborate, resolve questions and find out what's new about the Frisbee library, join the group on Telegram: https://t.me/FrisbeeLib

相关文章