Swiftpack.co -  ralfebert/SweetURLRequest as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
ralfebert/SweetURLRequest
SweetURLRequest simplifies common cases for creating an URLRequest and handling HTTPURLResponse.
.package(url: "https://github.com/ralfebert/SweetURLRequest.git", from: "0.1.14")

SweetURLRequest

SweetURLRequest simplifies common cases for creating an URLRequest and handling HTTPURLResponse.

Extensions for URLRequest:

⚡ Constants for HTTP methods
⚡ Properties to set common HTTP headers
⚡ URL/Form/JSON encoded parameters

Extensions for HTTPURLResponse:

⚡ Check for a 2xx success status code
⚡ Handle HTTP status codes via switch/case
⚡ Error enum for HTTP status codes

Examples

Example project

MetMuseumEndpoints: A Swift package for the The Metropolitan Museum of Art Collection API

Constants for HTTP methods

URLRequest(
    method: .post,
    url: URL(string: "http://www.example.com")!
)

Properties to set common HTTP headers

var request = URLRequest(url: URL(string: "http://www.example.com")!)
request.headers.accept = .json
request.headers.contentType = .xml
request.headers.authorization = "Bearer xyz"

URL/Form/JSON encoded parameters

Parameters will be URL-encoded for GET/HEAD/DELETE and sent as application/x-www-form-urlencoded body for other methods by default:

URLRequest(
    method: .get,
    url: URL(string: "http://www.example.com")!,
    parameters: ["param1" : "äöü", "param2" : "foo bar"]
)

You can also pass a JSON body, this will use JSONEncoder to serialize the given data and set an appropriate Content-Type header:

try URLRequest(
    method: .post,
    url: URL(string: "http://www.example.com")!,
    jsonBody: Person(name: "Bob")
)

Check for a success status code in the 2xx range

func expectSuccess(response: URLResponse) throws {
    let status = (response as! HTTPURLResponse).status
    guard status.responseType == .success else { throw status }
}

Handle HTTP status codes via switch/case, throw a status code as Error

func validate(response: URLResponse) throws {
    let status = (response as! HTTPURLResponse).status

    guard status.responseType == .success else {
        switch status {
        case .unauthorized:
            print("Unauthorized")
        default:
            print("Non-success status: \(status)")
        }
        throw status
    }
}

GitHub

link
Stars: 3
Last commit: Yesterday

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.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

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