All Projects → carson-katri → Swift Request

carson-katri / Swift Request

Licence: mit
Declarative HTTP networking, designed for SwiftUI

Programming Languages

swift
15916 projects
dsl
153 projects
declarative
70 projects

Projects that are alternatives of or similar to Swift Request

Onie
Open Network Install Environment
Stars: ✭ 411 (-14.55%)
Mutual labels:  networking
Networkservicemesh
The Hybrid/Multi-cloud IP Service Mesh
Stars: ✭ 456 (-5.2%)
Mutual labels:  networking
Ucx
Unified Communication X (mailing list - https://elist.ornl.gov/mailman/listinfo/ucx-group)
Stars: ✭ 471 (-2.08%)
Mutual labels:  networking
Actix Net
A collection of lower-level libraries for composable network services.
Stars: ✭ 415 (-13.72%)
Mutual labels:  networking
Kohttp
Kotlin DSL http client
Stars: ✭ 436 (-9.36%)
Mutual labels:  networking
Wesher
wireguard overlay mesh network manager
Stars: ✭ 461 (-4.16%)
Mutual labels:  networking
Hp Socket
High Performance TCP/UDP/HTTP Communication Component
Stars: ✭ 4,420 (+818.92%)
Mutual labels:  networking
Wireguard Manager
Self-hosted Wireguard Installer / Manager for CentOS, Debian, Ubuntu, Arch, Fedora, Redhat, Raspbian
Stars: ✭ 478 (-0.62%)
Mutual labels:  networking
Mio
Metal IO library for Rust
Stars: ✭ 4,613 (+859.04%)
Mutual labels:  networking
Goby
Attack surface mapping
Stars: ✭ 446 (-7.28%)
Mutual labels:  networking
Mobly
E2E test framework for tests with complex environment requirements.
Stars: ✭ 424 (-11.85%)
Mutual labels:  networking
Bamboots
Bamboots - Extension 4 Alamofire
Stars: ✭ 434 (-9.77%)
Mutual labels:  networking
Curlcpp
An object oriented C++ wrapper for CURL (libcurl)
Stars: ✭ 462 (-3.95%)
Mutual labels:  networking
Gnet
🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go./ gnet 是一个高性能、轻量级、非阻塞的事件驱动 Go 网络框架。
Stars: ✭ 5,736 (+1092.52%)
Mutual labels:  networking
Cnp3
Computer Networking : Principles, Protocols and Practice (first and second edition, third edition is being written on https://github.com/cnp3/ebook)
Stars: ✭ 471 (-2.08%)
Mutual labels:  networking
Kube Iptables Tailer
A service for better network visibility for your Kubernetes clusters.
Stars: ✭ 413 (-14.14%)
Mutual labels:  networking
Tenus
Linux networking in Go
Stars: ✭ 458 (-4.78%)
Mutual labels:  networking
Gns3 Server
GNS3 server
Stars: ✭ 477 (-0.83%)
Mutual labels:  networking
Peerdiscovery
Pure-Go library for cross-platform local peer discovery using UDP multicast 👩 🔁 👩
Stars: ✭ 476 (-1.04%)
Mutual labels:  networking
Enet Csharp
Reliable UDP networking library
Stars: ✭ 464 (-3.53%)
Mutual labels:  networking

Request

swift 5.1 SwiftUI iOS macOS tvOS Build codecov

Installation - Getting Started - Building a Request - Codable - Combine - How it Works - Request Groups - Request Chains - Json - Contributing - License

Using with SwiftUI

Installation

swift-request can be installed via the Swift Package Manager.

In Xcode 11, go to File > Swift Packages > Add Package Dependency..., then paste in https://github.com/carson-katri/swift-request

Now just import Request, and you're ready to Get Started

Getting Started

The old way:

var request = URLRequest(url: URL(string: "https://jsonplaceholder.typicode.com/todos")!)
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: url!) { (data, res, err) in
    if let data = data {
        ...
    } else if let error = error {
        ...
    }
}
task.resume()

The declarative way:

Request {
    Url("https://jsonplaceholder.typicode.com/todo")
    Header.Accept(.json)
}
.onData { data in
    ...
}
.onError { error in
    ...
}
.call()

The benefit of declaring requests becomes abundantly clear when your data becomes more complex:

Request {
    Url("https://jsonplaceholder.typicode.com/posts")
    Method(.post)
    Header.ContentType(.json)
    Body(Json([
        "title": "foo",
        "body": "bar",
        "usedId": 1
    ]).stringified)
}

Once you've built your Request, you can specify the response handlers you want to use. .onData, .onString, .onJson, and .onError are available. You can chain them together to handle multiple response types, as they return a modified version of the Request.

To perform the Request, just use .call(). This will run the Request, and give you the response when complete.

Request also conforms to Publisher, so you can manipulate it like any other Combine publisher (read more):

let cancellable = Request {
    Url("https://jsonplaceholder.typicode.com/todo")
    Header.Accept(.json)
}
.sink(receiveCompletion: { ... }, receiveValue: { ... })

Building a Request

There are many different tools available to build a Request:

  • Url

Exactly one must be present in each Request

Url("https://example.com")
Url(protocol: .secure, url: "example.com")
  • Method

Sets the MethodType of the Request (.get by default)

Method(.get) // Available: .get, .head, .post, .put, .delete, .connect, .options, .trace, and .patch 
  • Header

Sets an HTTP header field

Header.Any(key: "Custom-Header", value: "value123")
Header.Accept(.json)
Header.Authorization(.basic(username: "carsonkatri", password: "password123"))
Header.CacheControl(.noCache)
Header.ContentLength(16)
Header.ContentType(.xml)
Header.Host("en.example.com", port: "8000")
Header.Origin("www.example.com")
Header.Referer("redirectfrom.example.com")
Header.UserAgent(.firefoxMac)
  • Query

Creates the query string

Query(["key": "value"]) // ?key=value
  • Body

Sets the request body

Body(["key": "value"])
Body("myBodyContent")
Body(myJson)
  • Timeout

Sets the timeout for a request or resource:

Timeout(60)
Timeout(60, for: .request)
Timeout(30, for: .resource)
  • RequestParam

Add a param directly

Important: You must create the logic to handle a custom RequestParam. You may also consider adding a case to RequestParamType. If you think your custom parameter may be useful for others, see Contributing

Codable

Let's look at an example. Here we define our data:

struct Todo: Codable {
    let title: String
    let completed: Bool
    let id: Int
    let userId: Int
}

Now we can use AnyRequest to pull an array of Todos from the server:

AnyRequest<[Todo]> {
    Url("https://jsonplaceholder.typicode.com/todos")
}
.onObject { todos in ... }

In this case, onObject gives us [Todo]? in response. It's that easy to get data and decode it.

Request is built on AnyRequest, so they support all of the same parameters.

If you use onObject on a standard Request, you will receive Data in response.

Combine

Request and RequestGroup both conform to Publisher:

Request {
    Url("https://jsonplaceholder.typicode.com/todos")
}
.sink(receiveCompletion: { ... }, receiveValue: { ... })

RequestGroup {
    Request {
        Url("https://jsonplaceholder.typicode.com/todos")
    }
    Request {
        Url("https://jsonplaceholder.typicode.com/posts")
    }
    Request {
        Url("https://jsonplaceholder.typicode.com/todos/1")
    }
}
.sink(receiveCompletion: { ... }, receiveValue: { ... })

Request publishes the result using URLSession.DataTaskPublisher. RequestGroup collects the result of each Request in its body, and publishes the array of results.

You can use all of the Combine operators you'd expect on Request:

Request {
    Url("https://jsonplaceholder.typicode.com/todos")
}
.map(\.data)
.decode([Todo].self, decoder: JSONDecoder())
.sink(receiveCompletion: { ... }, receiveValue: { ... })

However, Request also comes with several convenience Publishers to simplify the process of decoding:

  1. objectPublisher - Decodes the data of an AnyRequest using JSONDecoder
  2. stringPublisher - Decodes the data to a String
  3. jsonPublisher - Converts the result to a Json object

Here's an example of using objectPublisher:

AnyRequest<[Todo]> {
    Url("https://jsonplaceholder.typicode.com/todos")
}
.objectPublisher
.sink(receiveCompletion: { ... }, receiveValue: { ... })

This removes the need to constantly use .map.decode to extract the desired Codable result.

To handle errors, you can use the receiveCompletion handler in sink:

Request {
    Url("https://jsonplaceholder.typicode.com/todos")
}
.sink(receiveCompletion: { res in
    switch res {
    case let .failure(err):
        // Handle `err`
    case .finished: break
    }
}, receiveValue: { ... })

How it Works

The body of the Request is built using the RequestBuilder @_functionBuilder.

It merges each RequestParam in the body into one CombinedParam object. This contains all the other params as children.

When you run .call(), the children are filtered to find the Url, and any other optional parameters that may have been included.

For more information, see RequestBuilder.swift and Request.swift

Request Groups

RequestGroup can be used to run multiple Requests simulataneously. You get a response when each Request completes (or fails)

RequestGroup {
    Request {
        Url("https://jsonplaceholder.typicode.com/todos")
    }
    Request {
        Url("https://jsonplaceholder.typicode.com/posts")
    }
    Request {
        Url("https://jsonplaceholder.typicode.com/todos/1")
    }
}
.onData { (index, data) in
    ...
}
.call()

Request Chains

RequestChain is used to run multiple Requests one at a time. When one completes, it passes its data on to the next Request, so you can use it to build the Request.

RequestChain.call can optionally accept a callback that gives you all the data of every Request when completed.

Note: You must use Request.chained to build your Request. This gives you access to the data and errors of previous Requests.

RequestChain {
    Request.chained { (data, errors) in
        Url("https://jsonplaceholder.typicode.com/todos")
    }
    Request.chained { (data, errors) in
        let json = Json(data[0]!)
        return Url("https://jsonplaceholder.typicode.com/todos/\(json?[0]["id"].int ?? 0)")
    }
}
.call { (data, errors) in
    ...
}

Repeated Calls

.update is used to run additional calls after the initial one. You can pass it either a number or a custom Publisher. You can also chain together multiple .updates. The two .updates in the following example are equivalent, so the end result is that the Request will be called once immediately and twice every 10 seconds thereafter.

Request {
    Url("https://jsonplaceholder.typicode.com/todo")
}
.update(every: 10)
.update(publisher: Timer.publish(every: 10, on: .main, in: .common).autoconnect())
.call()

If you want to use Request as a Publisher, use updatePublisher:

Request {
    Url("https://jsonplaceholder.typicode.com/todo")
}
.updatePublisher(every: 10)
.updatePublisher(publisher: ...)
.sink(receiveCompletion: { ... }, receiveValue: { ... })

Unlike update, updatePublisher does not send a value immediately, but will wait for the first value from the Publisher.

Json

swift-request includes support for Json. Json is used as the response type in the onJson callback on a Request object.

You can create Json by parsing a String or Data:

Json("{\"firstName\":\"Carson\"}")
Json("{\"firstName\":\"Carson\"}".data(using: .utf8))

You can subscript Json as you would expect:

myJson["firstName"].string // "Carson"
myComplexJson[0]["nestedJson"]["id"].int

It also supports dynamicMemberLookup, so you can subscript it like so:

myJson.firstName.string // "Carson"
myComplexJson[0].nestedJson.id.int

You can use .string, .int, .double, .bool, and .array to retrieve values in a desired type.

Note: These return non-optional values. If you want to check for nil, you can use .stringOptional, .intOptional, etc.

Contributing

See CONTRIBUTING

License

See LICENSE

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].