Latest 0.7.5
Homepage https://github.com/tadija/AENetwork
License MIT
Platforms ios 9.0
Authors

Swift 4.2
Platforms iOS | watchOS | tvOS | macOS
CocoaPods
Carthage
Swift Package Manager
License MIT

Swift minion for simple and lightweight networking

I made this for personal use, but feel free to use it or contribute.
For more examples check out Sources and Tests.

Usage

import AENetwork

/// - Note: Make `URL` with just a `String`
let anything: URL = "https://httpbin.org/anything"

/// - Note: Easily add parameters to `URL`
let url = anything.addingParameters(["foo" : "bar"])!

/// - Note: Factory methods on `URLRequest`
let request = URLRequest.post(url: url, headers: ["hello" : "world"], parameters: ["easy" : true])

/// - Note: Convenient sending of request
request.send { (result) in
    if let response = result.value {
        print("Status Code: (response.statusCode)n")
    }
}

/// - Note: Integrated `Reachability`
print("Connected to network: (Network.isOnline)")

/// - Note: Create custom `Network` instance when needed
let network = Network()

/// - Note: Monitor reachability state changes
network.reachability.startMonitoring()
network.reachability.stateDidChange = { state in
    print("Reachability state: (state.rawValue)n")
}
network.reachability.stopMonitoring()

/// - Note: Send request with `Fetcher` and use `ResponseResult` in completion
network.fetcher.send(request) { (result) in
    do {
        let response = try result.throwValue()
        print("Headers: (response.headers as? [String : Any])n")
    } catch {
        print(error)
    }
}

/// - Note: Simple creation of the entire backend layer
final class Backend: APIClient {
    let baseURL: URL = "https://httpbin.org"
}

/// - Note: `APIClient` will by default use shared `Network` instance for sending `APIRequest`,
/// but a custom `APIClient` implementation can do it via specific `Network` instance,
/// or even via any other way to resolve `APIRequest` and return `APIResponse` in the completion.

/// - Note: Type safe and scalable architecture of API requests
extension Backend {
    struct API {}
}
extension Backend.API {
    struct Anything: APIRequest {
        var method: URLRequest.Method { return .get }
        var path: String { return "anything" }
        var headers: [String : String]? { return ["X-Hello" : "X-World"] }
        var parameters: [String : Any]? { return ["easy" : true] }
    }
}

/// - Note: `Backend` example in action
let backend = Backend()
let apiRequest = Backend.API.Anything()

backend.send(apiRequest) { (result) in
    switch result {
    case .success(let response):
        print("Response: (response.dictionary?.description ?? "")n")
    case .failure(let error):
        print(error)
    }
}

Installation

License

This code is released under the MIT license. See LICENSE for details.

Latest podspec

{
    "name": "AENetwork",
    "summary": "Swift minion for simple and lightweight networking",
    "version": "0.7.5",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "source": {
        "git": "https://github.com/tadija/AENetwork.git",
        "tag": "0.7.5"
    },
    "source_files": "Sources/AENetwork/*.swift",
    "swift_version": "4.2",
    "platforms": {
        "ios": "9.0"
    },
    "homepage": "https://github.com/tadija/AENetwork",
    "authors": {
        "tadija": "[email protected]"
    },
    "social_media_url": "http://twitter.com/tadija"
}

Pin It on Pinterest

Share This