Swiftpack.co -  richardpiazza/SessionPlus as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
richardpiazza/SessionPlus
A collection of extensions & wrappers around URLSession.
.package(url: "https://github.com/richardpiazza/SessionPlus.git", from: "1.1.0")

SessionPlus

A collection of extensions & wrappers around URLSession.

Twitter: @richardpiazza

This package has been designed to work across multiple swift environments by utilizing conditional checks. It has been tested on Apple platforms (macOS, iOS, tvOS, watchOS), as well as Linux (Ubuntu).

Quick Start

Checkout the WebAPI class.

open class WebAPI: HTTPClient, HTTPCodable, HTTPInjectable {
    
    public var baseURL: URL
    public var session: URLSession
    public var authorization: HTTP.Authorization?
    public var jsonEncoder: JSONEncoder = JSONEncoder()
    public var jsonDecoder: JSONDecoder = JSONDecoder()
    public var injectedResponses: [InjectedPath : InjectedResponse] = [:]
    …
    public init(baseURL: URL, session: URLSession? = nil, delegate: URLSessionDelegate? = nil) {
        …
    }
}

WebAPI provides a basic implementation for an out-of-the-box HTTP/REST/JSON client.

Components

HTTPClient

public protocol HTTPClient {
    var baseURL: URL { get }
    var session: URLSession { get set }
    var authorization: HTTP.Authorization? { get set }
    func request(method: HTTP.RequestMethod, path: String, queryItems: [URLQueryItem]?, data: Data?) throws -> URLRequest
    func task(request: URLRequest, completion: @escaping HTTP.DataTaskCompletion) throws -> URLSessionDataTask
    func execute(request: URLRequest, completion: @escaping HTTP.DataTaskCompletion)
}

URLSession is task-driven. The SessionPlus api is designed with this in mind; allowing you to construct your request and then either creating a data task for you to references and execute, or automatically executing the request.

Example conformances for request(method:path:queryItems:data:), task(request:, completion), & execut(request:completion:) are provided in an extension, so the minimum required conformance to HTTPClient is baseURL, session, and authorization.

Convenience methods for the common HTTP request methods get, put, post, delete, and patch, are all provided.

HTTPCodable

public protocol HTTPCodable {
    var jsonEncoder: JSONEncoder { get set }
    var jsonDecoder: JSONDecoder { get set }
}

The HTTPCodable protocol is used to extend an HTTPClient implementation with support for encoding and decoding of JSON bodies.

HTTPInjectable

public protocol HTTPInjectable {
    var injectedResponses: [InjectedPath : InjectedResponse] { get set }
}

The HTTPInjectable protocol is used to extend an HTTPClient implementation by overriding the default execute(request:completion:) implementation to allow for the definition and usage of predefined responses. This makes for simple testing!

Installation

SessionPlus is distributed using the Swift Package Manager. To install it into a project, add it as a dependency within your Package.swift manifest:

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/richardpiazza/SessionPlus.git", .upToNextMinor(from: "1.1.0")
    ],
    ...
)

Then import the SessionPlus packages wherever you'd like to use it:

import SessionPlus

GitHub

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

Release Notes

SessionPlus 1.1.0
4 weeks ago

Replaced Header, MIMEType, and RequestMethod enums with structs for greater flexibility. Opened encoding/decoding methods to public for implementing in additional convenience methods.

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