GithubHelp home page GithubHelp logo

plumbkit's Introduction

💧PlumbKit💧

Overview

PlumbKit is a pure-Swift networking package that leverages Combine, Codable, and URLSession to interact with APIs. Inspired and modified from WireKit

Installation

This package only supports SPM at the moment. The easiest way to import it is via Xcode with File > Add Packages... and copy and paste the GitHub URL into the top-right text box

Usage

PlumbKit was designed to be very easy to use, and allows the developer to perform HTTP network requests through creating a struct and using PlumbAPIClient.

Example .GET

  1. Create a request that conforms to the PlumbRequest protocol
struct GetTodo: PlumbRequest {
   typealias ReturnType = Todo
   var path: String = "/Todo"
   var body: [String : Any?]?
}
  1. Create a data manager class
import Combine

class TodoManager {
   static let shared = TodoManager()
   
   let apiClient = PlumbAPIClient(baseURL: "https://example.com")
   
   private var subscriptions = Set<AnyCancellable>()
   
   func getTodos(completion: @escaping (Todo) -> ()) {
      apiClient.dispatch(GetTodo())
         .sink(
            receiveCompletion: { completion in
               switch completion {
                  case .failure(let error):
                     print(error)
                  default:
                     break
               }
            },
            receiveValue: { todos in
               completion(todos)
            }
         )
         .store(in: &subscriptions)
   }
}
  1. Use this method however you like 🥳

Example using .GET with query parameters

In the following example, we are going to create a struct for the same .GET request as above but with pagination parameters:

struct GetTodos: PlumbRequest {
   typealias ReturnType = Todos
   var path: String = "/Todos"
   var queryItems: [String: String]? {
      [
         "pageSize": "15",
         "pageNumber": "\(pageNumber)"
      ]
   }
   var body: [String : Any?]?
   var pageNumber: Int
   init(forPage pageNumber: Int) {
      self.pageNumber = pageNumber
   }
}

Example .POST request

struct CreateTodo: PlumbRequest {
   typealias ReturnType = CreateTodoResponse
   var path: String = "/Todo/Create"
   var methor: HTTPMethod = .POST
   var body: [String : Any]?
   init(_ body: [String : String]) {
      self.body = body.asDictionary
   }
}

Current PlumbRequest Defaults

The following are the defaults for PlumbRequest

extension PlumbRequest {
    // Defaults
    var method: HTTPMethod { return .GET }
    var contentType: String { return "application/json" }
    var queryParams: [String: String]? { return nil }
    var body: [String: Any]? { return nil }
    var headers: [String: String]? { return nil }
    var queryItems: [String: String]? { return nil }
}

Notes

I hope you find this library useful and README useful. For a more complete packet using the same techniques, refer to WireKit above.

plumbkit's People

Contributors

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