Swiftpack.co - RobotsAndPencils/AsyncHTTPNetworkService as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by RobotsAndPencils.
RobotsAndPencils/AsyncHTTPNetworkService 0.1.3
A Network layer for Swift using Concurrency
⭐️ 6
🕓 27 weeks ago
iOS macOS
.package(url: "https://github.com/RobotsAndPencils/AsyncHTTPNetworkService.git", from: "0.1.3")

AsyncHTTPNetworkService

AsyncHTTPNetworkService is a Swift native library network layer for using Concurrency (Async/Await).

The goal is to provide a simple network library so you don't have to think about it.

Example use

let networkService = AsyncHTTPNetworkService()
// see below on URLRequestBuilder + URLRequest extension for a easy example creating URL Requests
let urlRequest = URLRequest(...)
let person: Person = try await networkService.requestObject(urlRequest)
// person inherits from Codable

let searchResults: [Results] = try await networkService.requestObjects(urlRequest)

// networkService.requestVoid
// networkService.requestString

Compatibility

AsyncHTTPNetworkService is compatible to iOS 13+. If there are issues surrounding Aync/Await for < iOS 15, please let us know.

URLRequestBuilder + URLRequest extension

AsyncHTTPNetworkService includes URLRequest helpers for ease of creating requests and modifying requests

let baseURL = URLRequestBuilder(baseURL:  URL(string: "someendpoint"))

baseURL.get("user").queryItems(["tag": "check"])
baseURL.post("login").token("accessToken")
baseURL.post("withBody").body(json: codableObject)

Request Modifiers

AsyncNetworkService provides a basic protocol called NetworkRequestModifier that allows you to modify the network request before it sends.

2 options are included in AsyncNetworkService

APIKeyRequestModifier

Use this to pass up an api_key parameter inside a queryItem for all requests. A good example is when you're using a service that requires an api key.

This modifier could easily be updated to add an api_key to the request headers instead

BearerTokenRequestModifier

For each request, this will add in the passed in authenticationToken inside the Authorization header.

Example App

Included in this repo is a SwiftUI example app using AsyncHTTPNetworkService connecting to a Giphy api. Included is showing how using Codable to decode to objects easily.

The network calls to connect to Giphy is layed out in AsyncService.swift

Inside the GiphyService class is a list of Routes that are connected, and a function for each endpoint that is connected.

The init of the GiphyService includes an APIKeyRequestModifier that adds the a api_key to the header on each network request.

Maintainers

Matt Kiazyk

Contact

Made with ❤️ by Robots & Pencils

Twitter | GitHub

GitHub

link
Stars: 6
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

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