Giter Club home page Giter Club logo

littlefire's Introduction

LittleFire

Lightweight networking library for basic HTTP functionality for your iOS app.

Why another networking library?

This is intended to be small. There are other small libraries out there, which are fantastic. But staying small is another thing. I have used AlamoFire and Moya extensively in my career, and love them. But sometimes you just need something small, or a corporation or client you're working with has restrictions on dependencies, or even makes the process so difficult to bring in a dependency that it's easier to write your own. The goal with LittleFire is to allow people to reuse these few files if need be, or bring in via SwiftPM.

Usage

LittleFire introduces an extension to the Codable types, originally created by my friend Ben Winters, called ResponseCodable. It is the combination of ResponseDecodable and RequestEncodable. The types you expect to decode upon an HTTP response should conform to ResponseDecodable, and the bodies you add to be encoded in a request should conform to RequestEncodable.

Start by creating a provider.

let provider = ServiceProvider()

Next, create a service. The service is generic over the type you expect to decode. It's recommended to create your own service as an enum, that conforms to the Service protocol.

provider.requestPublisher(service: MyTodoService<MyTodoObject>.getTodo(1))

Currently, LittleFire uses Combine publishers to handle async calls. So, the call above to request a MyTodoObject can be fetched like so:

provider.requestPublisher(service: MyTodoService<MyTodoObject>.getTodo(1))
  .sink { todo in self.updateUI(with: todo) }

Creating your own service might looks something like this:

import LittleFire

enum JSONPlaceholderTodoService<T>: Service where T: ResponseDecodable {
  typealias ResponseType = T

  case getTodo(Int)
}

extension JSONPlaceholderTodoService {
  var baseURL: String { "https://jsonplaceholder.typicode.com" }

  var path: String {
    switch self {
    case .getTodo(let id): return "todos/\(id)"
    }
  }

  var parameters: [String : Any]? { nil }
  var method: LittleFire.ServiceMethod { .get }
  var body: Data? { nil }
}

By telling the protocol that your ResponseType is the generic T, you can specify the decodable type at the callsite. So, say for instance that if you had a get case and a post case for the same endpoint, just different paths, you could have different decodable response bodies in the same Service, so long as they conform to ResponseDecodable.

littlefire's People

Contributors

sixfivesoftware avatar

Stargazers

 avatar  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.