Swiftpack.co - Package - richardpiazza/SessionPlus

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

Usage

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

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!

Github

link
Stars: 2

Dependencies

Used By

Total: 0

Releases

SessionPlus 1.0.0 - 2020-08-23 11:16:11

1.0 release of SessionPlus 🎉.