SessionPlus

2.1.0

A swift request & response framework for JSON apis.
richardpiazza/SessionPlus

What's New

SessionPlus 2.1.0

2022-05-11T12:48:15Z

Modifications to better support using absolute URLs.

The primary intent of the framework is to interact with JSON apis, but another very-common need is the ability to download random data or images from URLs outside of a primary API. This is what the deprecated Downloader was focused on.

The ability to interact with non-base URLs has been brought forward and integrated into the overall solution. Request now has an Address enum which determines the type of addressing that will be used. The URLSessionClient has been renamed to BaseURLSessionClient to better indicate its usage, and a AbsoluteURLSessionClient implementation is now available.

SessionPlus

A swift request & response framework for JSON apis.

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

Installation

SessionPlus is distributed using the Swift Package Manager. You can add it using Xcode or by listing it as a dependency in your Package.swift manifest:

let package = Package(
  ...
  dependencies: [
    .package(url: "https://github.com/richardpiazza/SessionPlus.git", .upToNextMajor(from: "2.0.0")
  ],
  ...
  targets: [
    .target(
      name: "MyPackage",
      dependnecies: [
        "SessionPlus"
      ]
    )
  ]
)

Usage

SessionPlus offers a default implementation (URLSessionClient) that allows for requesting data from a JSON api. For example:

let url = URL(string: "https://api.agify.io")!
let client = BaseURLSessionClient(baseURL: url)
let request = Get(queryItems: [URLQueryItem(name: "name", value: "bob")])
let response = try await client.request(request)

Decoding

The Client protocol also offers extensions for automatically decoding responses to any Decodable type.

struct ApiResult: Decodable {
  let name: String
  let age: Int
  let count: Int
}


let response = try await client.request(request) as ApiResult
...
let response: ApiResult = try await client.request(request)

Flexibility

The Client protocol declares up to three forms requests based on platform abilities:

// async/await for swift 5.5+
func performRequest(_ request: Request) async throws -> Response
// completion handler for backwards compatibility
func performRequest(_ request: Request, completion: @escaping (Result<Response, Error>) -> Void)
// Combine publisher that emits with a response
func performRequest(_ request: Request) -> AnyPublisher<Response, Error>

Contribution

Contributions to SessionPlus are welcomed and encouraged! See the Contribution Guide for more information.

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Sep 01 2022 00:36:56 GMT-0500 (GMT-05:00)