Swiftpack.co - richardpiazza/SessionPlus as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by richardpiazza.
richardpiazza/SessionPlus 1.2.0
A collection of extensions & wrappers around URLSession.
⭐️ 1
🕓 3 days ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/richardpiazza/SessionPlus.git", from: "1.2.0")


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).


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.2.0")

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

import SessionPlus

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.



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), & execute(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.


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.


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!


Stars: 1
Last commit: 19 hours ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

SessionPlus 1.2.0
3 days ago

Swift 5.5 Concurrency Support. This release adds support for async/await support for Apple systems.

The FoundationNetworking module that provides URLSession on non-Apple platforms does not yet support async request method.

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