Lightweight async/await networking library with interceptor support - usable from iOS 13+.

Overview

Lightweight async/await networking library with interceptor support.

🚀 Getting started

AsyncNetwork's session acts as a wrapper to URLSession by adding support for interceptors. Interceptors can be used to easily extract common functionality for the preparation of URLRequests and the handling or retrying of the respective responses. This library allows you to write custom interceptors, but also comes with a couple of handy interceptors for authentication, status code validation, adding custom headers, logging and more!

Let's see how we can make use of that.

let session = Session(
    session: .shared,
    interceptors: [
        .setHeaders { headers in
            headers["Accept-Language"] = Locale.current.languageCode
            headers["Accept-Type"] = "application/json"
            headers["Content-Type"] = "application/json"
        },
        .validateStatus(),
        .responseLogger(),
    ],
    maximumRetryCount: 1,
)

In the code example above, a Session is created on the basis of an underlying URLSession and an array of interceptors. Interceptors can be thought of as layers, as they are called in order during request preparation and in reverse order for handling the response.

The first interceptor sets a set of headers, so that each request contains the correct "Accept-Language", "Accept-Type" and "Content-Type" header fields. In this example, we are overriding existing headers, which we might not always want to do, so you might want to have a look at .addHeaders instead.

The second interceptor validates that the status code of the response is between 200 and 299. If not, it throws an error and makes the whole request fail based on that. Successive interceptors (in this case, the one above it, which is not doing anything for response handling anyways), will not be called due to the error being thrown.

The third and last interceptor logs the response of every request response to the console. You can modify subsystem, category and logType as well here, if you want to customize that output. For even more control, you might want to look into the more general .logger interceptor as well.

🔩 Installation

AsyncNetwork is currently only available via Swift Package Manager. See this tutorial by Apple on how to add a package dependency to your Xcode project.

✍️ Author

Paul Kraft

📄 License

AsyncNetwork is available under the MIT license. See the LICENSE file for more info.

You might also like...
AsyncLocationKit - Async/await CoreLocation With Swift

AsyncLocationKit Wrapper for Apple CoreLocation framework with new Concurency Mo

Edit images and video with async / await in Swift, powered by Metal.
Edit images and video with async / await in Swift, powered by Metal.

AsyncGraphics The core value type in AsyncGraphics is a Graphic. It's like an image, tho it can be used with various async methods. Documentation Swif

A Swift lib for network with async/await

SmileNetwork A Swift network utility with async/await applied UseAge enum MockEndpoint { case weather(cityId: String) } extension MockEndpoint: S

Test app for the InStat company. The project uses MVVM architecture, interface created with SwiftUI, network requests with async/await

FootStats Test app for the InStat company. The Project uses MVVM architecture, interface created with SwiftUI, network requests with async/await using

A demonstration for bridging between Combine and your new async functions

CombineAsyncually This is a DEMONSTRATION of how you can bridge the new async / await functionality in Swift 5.5 with Combine. There is NO WARRANTY. T

⚡️ Fast async task based Swift framework with focus on type safety, concurrency and multi threading
⚡️ Fast async task based Swift framework with focus on type safety, concurrency and multi threading

Our apps constantly do work. The faster you react to user input and produce an output, the more likely is that the user will continue to use your appl

Async and concurrent versions of Swift’s forEach, map, flatMap, and compactMap APIs.

CollectionConcurrencyKit Welcome to CollectionConcurrencyKit, a lightweight Swift package that adds asynchronous and concurrent versions of the standa

Swift TableView pagination with async API request.
Swift TableView pagination with async API request.

SwiftTableViewPagination Swift TableView pagination with async API request. Output UML Create puml file. $ cd SwiftTableViewPagination/scripts/swiftum

PixabayImageSearchSample - SwiftUI + LazyVGrid + Pixabay API + Codable + sync/async Sample
PixabayImageSearchSample - SwiftUI + LazyVGrid + Pixabay API + Codable + sync/async Sample

PixabayImageSearchSample SwiftUI + LazyVGrid + Pixabay API + Codable + sync/asyn

Owner
Paul
Paul
Hydra ⚡️ Lightweight full-featured Promises, Async & Await Library in Swift

Lightweight full-featured Promises, Async & Await Library in Swift What's this? Hydra is full-featured lightweight library which allows you to write b

Daniele Margutti 2k Dec 24, 2022
Hydra: Lightweight full-featured Promises, Async-Await Library in Swift

Async Functions for ECMAScript The introduction of Promises and Generators in EC

Ecma TC39 1.6k Oct 17, 2022
A simple network layer for use in small iOS projects with async/await support

SimpleNetwork Intro SimpleNetwork is simple network layer for use in small projects. Swift Package Manager Note: Instructions below are for using Swif

Alexandre Garrefa 1 Nov 30, 2021
A lightweight swift network layer with Combine, Async-Await, and a traditional completion block.

CombineNetwork A simple light-weight network library to make network requesting simpler. It supports newer techonology such as async/await as well as

Dushant Singh 4 Jan 3, 2022
iOS 13-compatible backports of commonly used async/await-based system APIs that are only available from iOS 15 by default.

AsyncCompatibilityKit Welcome to AsyncCompatibilityKit, a lightweight Swift package that adds iOS 13-compatible backports of commonly used async/await

John Sundell 367 Jan 5, 2023
Example project showing how to use async/await with iOS 13

ios13AsyncAwait Example project showing how to use async/await with iOS 13 Article This source code is a part of an article published at This Dev Brai

Michał Tynior 7 Oct 2, 2022
Using async / await on iOS 13: everything you need to know

Using async / await on iOS 13: everything you need to know! ?? Content This repository contains the code sample I've used during December 14th's lives

Vincent Pradeilles 11 Feb 1, 2022
⏳ Collection of Swift 5.5 async/await utility functions.

⏳ FunAsync Collection of Swift 5.5 async/await utility functions. Throw <-> Result conversion asyncThrowsToAsyncResult asyncResultToAsyncThrows More C

Yasuhiro Inami 23 Oct 14, 2022
🎭 Swift async/await & Actor-powered effectful state-management framework.

?? Actomaton ??‍?? Actor + ?? Automaton = ?? Actomaton Actomaton is Swift async/await & Actor-powered effectful state-management framework inspired by

Yasuhiro Inami 199 Dec 20, 2022
AsyncTaskKit - contains some additions to async/await Task

AsyncTaskKit This repo contains some additions to async/await Task. In general i

Michał Zaborowski 0 Jan 2, 2022