Giter Club home page Giter Club logo

petitio's Introduction

Petitio ๐Ÿ“ซ

Easy non-blocking network requests for Swift packages

Installation

You can install Petitio with SPM

  1. Add or amend a Package.swift
  2. Add this dependency .package(url: "https://github.com/dalu93/Petitio", from: "0.1.0")
  3. Then import Petitio in your code and start using it ๐ŸŽ‰

Usage

Basic usage

Petitio provides an easy network interface based on vapor/http library and fairly easy to use.

let response = try Petitio.get(
    at: "http://www.recipepuppy.com/api/?i=onions,garlic&q=omelet&p=3",
    parameters: [:]
).wait()

Or using promises provided by SwiftNIO

try Petitio.get(
    at: "http://www.recipepuppy.com/api/?i=onions,garlic&q=omelet&p=3",
    parameters: [:]
).map { $0.body.data }

By default, Petitio ๐Ÿ“ซ provides few basic methods to achieve the most basic HTTP requests based on the HTTP methods GET, POST, PUT, PATCH, DELETE.

GET

public static func get(
    at url: URLRepresentable,
    headers: HTTPHeaders = [:],
    parameters: [String: Any],
    using encoder: RequestEncoder = URLEncoder(),
    on worker: MultiThreadedEventLoopGroup? = nil
) throws -> EventLoopFuture<HTTPResponse> { }

POST

public static func post(
    at url: URLRepresentable,
    headers: HTTPHeaders = [:],
    parameters: [String: Any],
    using encoder: RequestEncoder = BodyJSONEncoder(),
    on worker: MultiThreadedEventLoopGroup? = nil
) throws -> EventLoopFuture<HTTPResponse> { }

PUT

public static func put(
    at url: URLRepresentable,
    headers: HTTPHeaders = [:],
    parameters: [String: Any],
    using encoder: RequestEncoder = BodyJSONEncoder(),
    on worker: MultiThreadedEventLoopGroup? = nil
) throws -> EventLoopFuture<HTTPResponse> { }

PATCH

public static func patch(
    at url: URLRepresentable,
    headers: HTTPHeaders = [:],
    parameters: [String: Any],
    using encoder: RequestEncoder = BodyJSONEncoder(),
    on worker: MultiThreadedEventLoopGroup? = nil
) throws -> EventLoopFuture<HTTPResponse> {

DELETE

public static func delete(
    at url: URLRepresentable,
    headers: HTTPHeaders = [:],
    parameters: [String: Any],
    using encoder: RequestEncoder = URLEncoder(),
    on worker: MultiThreadedEventLoopGroup? = nil
) throws -> EventLoopFuture<HTTPResponse> { }

Advanced usage

Big data download

In case you want to download a big amount of data (ex: a video file), you can still use the GET request, but make sure you pass a custom worker to it as below

let worker = MultiThreadedEventLoopGroup(numberOfThreads: 1)

Petitio
    .get(
        at: "someUrl",
        parameters: [:],
        on: worker
    )
    .flatMap { response in
        return response.body.consumeData(on: worker)
    }

Define your own encoder

Few encoders are implemented in the library, but you can create your custom one by confirming to RequestEncoder protocol. Then, pass it as a parameter in the request you create

struct MyOwnEncoder: RequestEncoder {
    // implement the protocol here
}

Petitio.get(
    at: "someUrl",
    parameters: [:],
    using: MyOwnEncoder()
)

petitio's People

Contributors

dalu93 avatar

Watchers

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