Swiftpack.co -  shawnthroop/Request as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
shawnthroop/Request
A lightweight and typesafe wrapper around URLSession's URLRequest data publisher API
.package(url: "https://github.com/shawnthroop/Request.git", from: "v0.1.0")

Request

A lightweight and typesafe wrapper around URLSession's URLRequest data publisher API.

Some notable features:

  • Case insensitive header fields.
  • Composable and reusable URL building.

Example

Start by defining a resuable environment.

extension Request.Environment {
    
    static var pnut: Self {
        .init(host: "api.pnut.io", path: "/v1")
    }
}

Then build a request.

let global = Request(to: .pnut, path: "/posts/stream/global")

let globalPosts = global
    .publisher()
    .map(\.data)
    .decode(type: Response<Post>.self, decoder: JSONDecoder.pnut)

Lets send some data, in this case, to an authentication endpoint. Heres how to model the required values.

enum PasswordAuthorizationKey: String, Hashable, QueryItem {
    case clientID = "client_id"
    case passwordGrantSecret = "password_grant_secret"
    case username, password
    case grantType = "grant_type"
    case scope
}

Encoding the data is simplified by conforming to QueryItem and using String values.

let payload: [PasswordAuthorizationKey: String] = [
    .clientID: "913z4jqpnqfn5m90ksf0",
    .passwordGrantSecret: "sfhhsjhhfkddonnfjfloookffheheerrre",
    .username: "shawn", .password: "*******",
    .grantType: "password",
    .scope: "basic,write_post"
]

let auth = Request(to: .pnut, method: .post, path: "/oauth/access_token", body: .urlEncoded(payload))

Contact

This is meant to be my personal, lightweight, and type-safe networking library. That being said, feedback is welcome and if you find it useful please let me know. I'm @shawn on pnut.io and @shawnthroop most other places.

GitHub

link
Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API