ResonanceKit is a very lightweight framework, compared to something like AlamoFire, for async JSON REST requests utilizing the Codable protocols to
get the model objects from the server. It is pure Swift. It utilizes Coroutines to allow awaiting on main dispatch queue or any other queue you like.
In addition to handing the decoding and encoding requests, it uses Await symantics so that you can make multiple requests as if they are sequential
before returning the final result. This greatly simplifies your code and avoids callback hell. Eventually coroutine support will be built into, likely,
Swift 6, and will be the way to make concurrent network requests in the future.
Built-in mock server for testing
It provides a injectable mock session that simulates talking to the real server, by putting your requests in a folder structure that mimics the folder
structure on the real server and returning the same JSON for those requests you can run tests quickly and reliably.
Other minor features:
An extension to UIImageView to allow downloading of images with an in memory LRU Cache.
A convineient way of printing out the request so you can easily paste it into curl for debugging requests.
How to use
Simply import using Swift Package Manager https://github.com/possen/ResonanceKit.git then import ResonanceKit and import SwiftCoroutines in files
that need to make network requests.
The call must be wrapped in the call to DispatchQueue.main.startCoroutine() at some point in your request call stack, this can be high up in your
NetworkController just so long as it is in the callstack of the request. As mentioned earlier, you don’t have to put it on the main queue, but
it won’t block because it utilises CoRoutines. Which are similar to coooperative multithreading.
Example Person Model Object:
struct People: Decodable {
var count: Int
let next: URL?
var results: [Person]
}
struct Person: Decodable, CustomStringConvertible, Hashable {
var description: String {
return name
}
let name: String
let birthYear: String
let eyeColor: String
let height: String
let mass: String
let skinColor: String
let homeworld: URL
let films: [URL]
let species: [URL]
let starships: [URL]
let vehicles: [URL]
let created: Date
let edited: Date
let url: URL
}
To use Testing
Each file in that folder structure is a JSON
file which has the following structure which is an array so you can have multiple responses in the same file:
A request section to validate the matching of the request:
A status section to simulate the different response codes
A method section to match the type of request
A response section which is the JSON response as if it were coming back from real server
Example Mock Response put in folder Mocks/People.json:
The dependencies are meant to keep it as lightweight as possible and as system support becomes available, I will try to eliminate these.
SwiftCoroutines - for coroutine support really nice package for doing await and coroutine support.
SwiftyBeaver - for logging requests. Uses my fork of it to allow bundling as a Built for Distribution flag which does not work due to a module namespace issue,
Would use new logging announced at WWDC 2020 but that would limit the use to prerelease software.
ResonanceKit
ResonanceKit is a very lightweight framework, compared to something like AlamoFire, for async JSON REST requests utilizing the
Codable
protocols to get the model objects from the server. It is pure Swift. It utilizes Coroutines to allow awaiting on main dispatch queue or any other queue you like.In addition to handing the decoding and encoding requests, it uses Await symantics so that you can make multiple requests as if they are sequential before returning the final result. This greatly simplifies your code and avoids callback hell. Eventually coroutine support will be built into, likely, Swift 6, and will be the way to make concurrent network requests in the future.
Built-in mock server for testing
It provides a injectable mock session that simulates talking to the real server, by putting your requests in a folder structure that mimics the folder structure on the real server and returning the same
JSON
for those requests you can run tests quickly and reliably.Other minor features:
UIImageView
to allow downloading of images with an in memoryLRU
Cache.curl
for debugging requests.How to use
Simply import using Swift Package Manager
https://github.com/possen/ResonanceKit.git
thenimport ResonanceKit
andimport SwiftCoroutines
in files that need to make network requests.Create a session with your base URL:
Setup your decoding strategies:
Similar properties exist for encoding an object to send to the request body.
From there just create a request object with the decodable type of the object you need. Then perform your network request where
Person
isDecodable
:The call must be wrapped in the call to
DispatchQueue.main.startCoroutine()
at some point in your request call stack, this can be high up in yourNetworkController
just so long as it is in the callstack of the request. As mentioned earlier, you don’t have to put it on the main queue, but it won’t block because it utilises CoRoutines. Which are similar to coooperative multithreading.Example Person Model Object:
To use Testing
Each file in that folder structure is a JSON file which has the following structure which is an array so you can have multiple responses in the same file:
request
section to validate the matching of the request:status
section to simulate the different response codesmethod
section to match the type of requestresponse
section which is the JSON response as if it were coming back from real serverExample Mock Response put in folder Mocks/People.json:
Dependencies
The dependencies are meant to keep it as lightweight as possible and as system support becomes available, I will try to eliminate these.
Built for Distribution
flag which does not work due to a module namespace issue, Would use new logging announced at WWDC 2020 but that would limit the use to prerelease software.