Giter VIP home page Giter VIP logo

resty's Introduction

Resty

Resty is a networking μframework built on Apple's Combine asynchronous streams framework. It provides a simple, composable, protocol-oriented way to structure wrappers around simple REST APIs.

Wrapping an API

To wrap an API, conform to the API protocol, add the baseURL for your service, and write Publisher returning functions. Conforming to the API protocol gives you access to the five HTTP verb methods (get(path:), post(path:), put(path:), patch(path:), delete(path:)), to which you can declaratively add request options and get a publisher from, on which you can use all the functional declarative operators that come with Publisher.

import Resty

struct SwiftForums: API {
    let baseURL = URL(string: "https://forums.swift.org")!
    lazy var decoder: JSONDecoder = {
        var decoder = JSONDecoder()
        decoder.keyDecodingStrategy = .convertFromSnakeCase
        return decoder
    }()
    
    func search(term: String, includeBlurbs blurbs: Bool = false) -> AnyPublisher<SearchResult, Error> {
        get(path: "/search")
            .queryItems(["q": term, "include_blurbs": "\(blurbs)"])
            .publisher()
    }
    
    func latestPosts() -> AnyPublisher<[LatestPost], Error> {
        struct LatestPosts: Decodable {
            let latestPosts: [LatestPost]
        }
        
        return get(path: "/posts.json")
            .publisher()
            .decode(type: LatestPosts.self, decoder: decoder)
            .map(\.latestPosts)
            .eraseToAnyPublisher()
    }
}

From here, you'll write a method for each endpoint you need from the API and they will each return an AnyPublisher<Output, Failure>, with the Output being the type of the response you recieve, or Void, if no response. From there you can use your API's Publishers in your app, binding them to UI controls in viewDidLoad() for UIKit or assigning them to @Published properties on your SwiftUI ViewModel.

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.