Swiftpack.co - velos/Endpoints as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
velos/Endpoints
A simple iOS library for defining Endpoints
.package(url: "https://github.com/velos/Endpoints.git", from: "0.1.4")

Endpoints

CI Documentation

Endpoints is a small library for creating statically and strongly-typed definitions of endpoint with paths, methods, inputs and outputs.

Purpose

The purpose of Endpoints is to, in a type-safe way, define how to create a URLRequest from typed properties and, additionally, define how a response for the request should be handled. The library not only includes the ability to create these requests in a type-safe way, but also includes helpers to perform the requests using URLSession. Endpoints does not try to wrap the URL loading system to provide features on top of it like Alamofire. Instead, Endpoints focuses on defining requests and converting those requests into URLRequest objects to be plugged into vanilla URLSessions. However, this library could be used in conjunction with Alamofire if desired.

Getting Started

The basic process for defining an Endpoint starts with defining a value conforming to RequestType. With the RequestType protocol, you are encapsulating all the properties that are needed for making a request and the types for parsing the response. Within the RequestType, the endpoint static var serves as an immutable definition of the server's endpoint and how the variable pieces of the RequestType should fit together when making the full request.

To get started, first create a type (struct or class) conforming to RequestType. There are only two required elements to conform: defining the Response and creating the Endpoint.

Requests and Endpoints do not contain base URLs so that these requests can be used on different environments. Environments are defined as conforming to the EnvironmentType and implement a baseURL as well as an optional requestProcessor which has a final hook before URLRequest creation to modify the URLRequest to attach authentication or signatures.

To find out more about the pieces of the RequestType, check out Defining a ResponseType on the wiki.

Examples

The most basic example of defining an Endpoint is creating a simple GET request. This means defining a type that conforms to RequestType such as:

struct MyRequest: RequestType {
    static let endpoint: Endpoint<MyRequest> = Endpoint(
        method: .get,
        path: "path/to/resource"
    )

    struct Response: Decodable {
        let resourceId: String
        let resourceName: String
    }
}

This includes a Response associated type (can be typealiased to a more complex existing type) which defines how the response will come back from the request.

Then usage can employ the URLSession extensions:

Usage

URLSession.shared.endpointPublisher(in: .production, with: MyRequest())
    .sink { completion in
        guard case .failure(let error) = completion else { return }
        // handle error
    } receiveValue: { (response: MyRequest.Response) in
        // handle MyRequest.Response
    }
    .store(in: &cancellables)

To browse more complex examples, make sure to check out the Examples wiki page.

GitHub

link
Stars: 0
Last commit: 4 days 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.

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

Release Notes

0.1.3: tvOS Support
4 weeks ago
  • Adds tvOS Support

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