Astral
Astral is a minimal HTTP Networking library that aims to simplify an application's networking layer by abstracting the steps needed to create a network request into multiple objects.
It aims to shy away from the typical network layer singleton by encapsulating each part of network request as an object.
Inspired by Soroush Khanlou's blog post on Protocol Oriented Programming.
Requirements
Astral requires iOS 9.3 or higher and Swift 3.x
Installation
CocoaPods
- Add the following to your Podfile:
pod 'Astral'
- Integrate your dependencies using frameworks: add
use_frameworks!
to your Podfile. - Run
pod install
.
Carthage
Add the following to your Cartfile:
github "hooliooo/Astral"
Swift Package Manager
Add the following dependency in your Package.swift file:
.package(url: "https://github.com/hooliooo/Astral.git", from: "0.9.0")
A Simple Example
Here's an example using the Pokemon API and the implementations of RequestBuilder and RequestDispatcher provided by Astral.
Feel free to build and customize your own implementations. Simply adopt the appropriate protocols.
struct PokeAPIConfiguration: RequestConfiguration {
let scheme: URLScheme = URLScheme.http
let host: String = "pokeapi.co"
let basePathComponents: [String] = [
"api",
"v2"
]
let baseHeaders: Set<Header> = [
Header(key: Header.Key.contentType, value: Header.Value.mediaType(MediaType.applicationJSON))
]
}
struct PokemonRequest: Request {
let id: Int
let configuration: RequestConfiguration = PokeAPIConfiguration()
let method: HTTPMethod = HTTPMethod.get
let pathComponents: [String] = [
"pokemon",
"\(self.id)"
]
let parameters: Parameters = .none
let headers: Set<Header> = []
}
let queue: DispatchQueue = DispatchQueue(label: "pokeapi", qos: DispatchQoS.utility, attributes: [DispatchQueue.Attributes.concurrent])
let request: Request = PokemonRequest(id: 1)
let dispatcher: RequestDispatcher = BaseRequestDispatcher(queue: queue)
dispatcher.response(
of: request,
onSuccess: { [weak self] (response: Response) -> Void in
// let responseData: Data = response.data
// Do something with data
// or
// let dictionary: [String: Any] = response.json.dictValue
// Do something with dictionary
},
onFailure: { (error: NetworkingError) -> Void in
// Handle the error
},
onComplete: { () -> Void in
// Handle the completion of the network request
// such as clean up of the UI
}
)
In-depth Example
Let's say you have a get request that requires url path components. An example of that request could be:
struct YourAPIConfiguration: RequestConfiguration {
let scheme: URLScheme = URLScheme.https
let host: String = "yourhost.com"
let basePathComponents: [String] = [
"api",
"v1"
]
let baseHeaders: Set<Header> = [
Header(key: Header.Key.contentType, value: Header.Value.mediaType(MediaType.applicationJSON))
]
}
struct YourRequest: Request {
let configuration: RequestConfiguration = YourAPIConfiguration()
let method: HTTPMethod = HTTPMethod.get
let pathComponents: [String] = [
"your",
"path",
"components"
]
let parameters: Parameters = .dict([
"yourKey": "yourValue,
"anotherKey": "anotherValue"
])
let headers: Set<Header> = [
Header(key: Header.Key.accept, value: Header.Value.mediaType(MediaType.applicationJSON))
]
}
let request: Request = YourRequest()
let dispatcher: RequestDispatcher = BaseRequestDispatcher(isDebugMode: true)
dispatcher.urlRequest(of: request).url
With these lines of code, your hypothetical request would create the following URL:
https://yourhost.com/api/v1/your/path/components?yourKey=yourValue&anotherKey=anotherValue
The response would also have the following headers included:
Content-Type = application/json
Accept = application/json
In the case of when the RequestConfiguration AND the Request have each have a Header with identical keys, the Request WILL OVERWRITE the RequestConfiguration's Header.
Author
License
Astral is available under the MIT license. See the LICENSE file for more info.