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.