Git Product home page Git Product logo

voyager's Introduction

Voyager

A lightweight and testable protocol oriented network layer

Installation

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding Voyager as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/iDevid/Voyager.git", .upToNextMajor(from: "1.0.0"))
]

Usage/Examples

Using Voyager for your networking calls it's really easy, you should just define a service that inherits from NetworkService

import Voyager

struct PokemonListService: NetworkService {

    typealias ResponseModel = PokemonListResponse
    
    var rootEndpoint: String { "https://pokeapi.co/api/v2/" }
    var endpoint: String { "pokemon" }
}

// MARK: - Response Models

struct PokemonListResponse: Decodable {
    let count: Int
    let results: [PokemonReference]
}

struct PokemonReference: Decodable {
    let name: String
    let url: String
}

Using the service

let service = PokemonListService()
service.perform { result in
    switch result {
    case let .failure(error):
        print("Error: \(error)")
    
    case let .success(responseModel):
        print("Response: \(responseModel)")
    }
}

How to perform query requests

To perform a query request you have to just define a typealias QueryRequestModel inside your service like this:

struct PokemonListService: NetworkService {
    typealias QueryRequestModel = PokemonListQueryRequest
}

struct PokemonListQueryRequest: NetworkRequest {
    let limit: Int
    let offset: Int
}

And then you should call the perform method with the defined query model, it will be converted automatically to query items:

let service = PokemonListService()
service.perform(queryRequest: PokemonListQueryRequest(limit: 1000, offset: 5)) { result in
    ...
}

How to perform body requests

This applies also for body request, just define a typealias BodyRequestModel = MyModel and then use it in the perform method:

service.perform(bodyRequest: MyModel()) {
    ...
}

Mocking a service

To mock a service in your unit tests you should use the protocol approach to define them. Let's consider the previous PokemonListService, it should be defined in this way:

protocol PokemonListServiceProtocol: NetworkService where ResponseModel == PokemonListResponse {}
extension PokemonListServiceProtocol {
    var rootEndpoint: String { "https://pokeapi.co/api/v2/" }
    var endpoint: String { "pokemon" }
}

// Concrete Type:
struct PokemonListService: PokemonListServiceProtocol {}

class ClassToTest {
    let service: any PokemonListServiceProtocol
    
    init(service: any PokemonListServiceProtocol) {
        self.service = service
    }
}

Now in your app you can instantiate the class just in this way:

ClassToTest(service: PokemonListService())

To create a mock you should import VoyagerMock framework and declaring a new service (it should be an object) that inherits from PokemonListServiceProtocol and also MockedService, this will automatically mock the perform method of the network service

import VoyagerMock

class PokemonListMockService: MockedService, PokemonListServiceProtocol {}

The MockedService has the following APIs to set the response:

let service = PokemonListMockService()
service.setNextResponse(MockType)

where MockType it's defined in this way:

public enum MockType {
    case json(name: String, bundle: Bundle = .main)
    case model(_ model: ResponseModel)
    case failure(_ error: Error)
}

voyager's People

Contributors

idevid avatar

Watchers

 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.