Small, easy to use Swift framework for requesting resources from a server and optionally parsing them. Documentation: https://propellerlabs.github.io/PropellerNetwork/
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
PropellerNetwork.xcodeproj
Source
Tests
docs
.gitignore
.travis.yml
LICENSE
Package.swift
README.md

README.md

Travis Platform Swift Carthage Swift Package Manager MIT License

PropellerNetwork

Networking layer for Propeller iOS projects

Installation

Swift Package Manager

dependencies: [
    .Package(url: "https://github.com/propellerlabs/PropellerNetwork.git", majorVersion: 1)
]

Carthage

github "propellerlabs/PropellerNetwork"

Usage

Create a WebServiceConfiguration

A WebServiceConfiguration is passed into a Resource to let the WebService know how to configure the URLRequest. You can use a WebServiceConfiguration on multiple Resource objects.

Example

struct NetworkConfiguration {
    static let `default` = WebServiceConfiguration(basePath: "https://httpbin.org",
                                                   additionalHeaders: nil,
                                                   credential: nil)
}

Create a resource

A resource encapsulates the expected return type, web service configuration, URL path, HTTP method, parameters, headers, encoding and parsing to handle the specific network request.

init(configuration: WebServiceConfiguration,
     urlPath: String,
     method: PropellerNetwork.HTTPMethod = .get,
     parameters: Parameters? = nil, 
     headers: [String : String]? = nil, 
     encoding: ParameterEncoding = JSONEncoder.default, 
     parsing: ((JSONObject) -> A?)? = nil)

Example

struct User {
    let name: String    
}

let getUserResource = Resource<User>(configuration: NetworkConfiguration.default,
                                     urlPath: "/get",
                                     parsing: { json in
                                        guard let name = json["name"] as? String else {
                                            return nil
                                        }
                                        return User(name: name)
                                    })

Request a resource

After setting up your resource, request it!

WebService.request<A>(_ resource: Resource<A>, completion: @escaping (A?, Error?) -> Void)

Example

WebService.request(getUserResource) { object, error in
    print(object)
    print(error)
}

##Thanks

Special thanks to Chris Eidhof and Florian Kugler for their Swift Talk web episode on Networking as the inspiration for this project.