Latest2.0.1
Homepagehttps://github.com/hooliooo/Astral
LicenseMIT
Platformsios 9.3, osx 10.11, tvos 11.0, watchos 4.0, requires ARC
FrameworksFoundation
Authors

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.

CI Status
Version
License
Platform

Requirements

Astral requires iOS 9.3 or higher and Swift 3.x

Installation

CocoaPods

  1. Add the following to your Podfile:
pod 'Astral'
  1. Integrate your dependencies using frameworks: add use_frameworks! to your Podfile.
  2. 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.Field.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.Field.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.Field.accept, value: Header.Value.mediaType(MediaType.applicationJSON))
    ]
}
let queue: DispatchQueue = DispatchQueue(label: "NetworkQueue", qos: DispatchQoS.utility, attributes: [DispatchQueue.Attributes.concurrent])

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

Julio Alorro

License

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

Latest podspec

{
    "name": "Astral",
    "module_name": "Astral",
    "version": "2.0.1",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "summary": "An HTTP networking library that uses protocols and Futures",
    "homepage": "https://github.com/hooliooo/Astral",
    "authors": {
        "Julio Alorro": "[email protected]"
    },
    "source": {
        "git": "https://github.com/hooliooo/Astral.git",
        "tag": "2.0.1"
    },
    "platforms": {
        "ios": "9.3",
        "osx": "10.11",
        "tvos": "11.0",
        "watchos": "4.0"
    },
    "source_files": "Sources/*.swift",
    "requires_arc": true,
    "frameworks": "Foundation",
    "swift_version": "4.1"
}

Pin It on Pinterest

Share This