Easy non-blocking network requests for Swift packages
You can install Petitio with SPM
Package.swift
.package(url: "https://github.com/dalu93/Petitio", from: "0.1.0")
Petitio
in your code and start using it 🎉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.
public static func get(
at url: URLRepresentable,
headers: HTTPHeaders = [:],
parameters: [String: Any],
using encoder: RequestEncoder = URLEncoder(),
on worker: MultiThreadedEventLoopGroup? = nil
) throws -> EventLoopFuture<HTTPResponse> { }
public static func post(
at url: URLRepresentable,
headers: HTTPHeaders = [:],
parameters: [String: Any],
using encoder: RequestEncoder = BodyJSONEncoder(),
on worker: MultiThreadedEventLoopGroup? = nil
) throws -> EventLoopFuture<HTTPResponse> { }
public static func put(
at url: URLRepresentable,
headers: HTTPHeaders = [:],
parameters: [String: Any],
using encoder: RequestEncoder = BodyJSONEncoder(),
on worker: MultiThreadedEventLoopGroup? = nil
) throws -> EventLoopFuture<HTTPResponse> { }
public static func patch(
at url: URLRepresentable,
headers: HTTPHeaders = [:],
parameters: [String: Any],
using encoder: RequestEncoder = BodyJSONEncoder(),
on worker: MultiThreadedEventLoopGroup? = nil
) throws -> EventLoopFuture<HTTPResponse> {
public static func delete(
at url: URLRepresentable,
headers: HTTPHeaders = [:],
parameters: [String: Any],
using encoder: RequestEncoder = URLEncoder(),
on worker: MultiThreadedEventLoopGroup? = nil
) throws -> EventLoopFuture<HTTPResponse> { }
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)
}
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()
)
link |
Stars: 0 |
Last commit: 5 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics