Programs often communicate over HTTP. The de facto standard notation for payloads in this communication is JSON. The Patron Swift package provides a simple interface to send and receive data to and from HTTP servers. It’s purpose is to reduce redundant client code whilst keeping things simple.
The Patron object represents a remote HTTP JSON service endpoint.
Protocols
JSONService
The JSONService protocol defines a JSON service.
Structures
PatronError
Patron
The Patron class is a convenient way to represent a remote HTTP JSON service endpoint. A Patron object provides access to a single service on a specific host via GET and POST HTTP methods. It assumes that payloads in both directions are JSON.
As Patron serializes JSON payloads on the calling thread, it’s not a good idea to use it from the main thread, instead I recommend, you run it from within an Operation, or a closure dispatched to another queue. Usually there is more work required anyways, serialization obviously, which should be offloaded from the main thread.
json The payload to send as the body of this request.
cb The callback receiving the JSON result as its first parameter, followed by response, and error. All callback parameters may be nil.
Returns
An executing URLSessionTask.
Throws
PatronError.InvalidJSON, if the potential json payload is
not serializable to JSON by NSJSONSerialization.
Getting Information
var host: String { get }
The hostname of the remote service.
var status: (Int, TimeInterval)? { get }
The last URLSession or JSONSerialization error code, and the timestamp at which it occured in Unix time, seconds since 00:00:00 UTC on 1 January 1970. The next successful request resets status to nil.
Test
For testing, we run a little Node.js Server – find it in Tests/Server.
$ make test
Install
📦 Add https://github.com/michaelnisi/patron to your package manifest.
Patron
Programs often communicate over HTTP. The de facto standard notation for payloads in this communication is JSON. The Patron Swift package provides a simple interface to send and receive data to and from HTTP servers. It’s purpose is to reduce redundant client code whilst keeping things simple.
Symbols
Classes
The
Patronobject represents a remote HTTP JSON service endpoint.Protocols
The
JSONServiceprotocol defines a JSON service.Structures
Patron
The
Patronclass is a convenient way to represent a remote HTTP JSON service endpoint. APatronobject provides access to a single service on a specific host viaGETandPOSTHTTP methods. It assumes that payloads in both directions are JSON.As
Patronserializes JSON payloads on the calling thread, it’s not a good idea to use it from the main thread, instead I recommend, you run it from within anOperation, or a closure dispatched to another queue. Usually there is more work required anyways, serialization obviously, which should be offloaded from the main thread.Creating a Client
Creates a client for the service at the specified
URL.Parameters
URLThe URL of the service.sessionThe session to use for HTTP requests.logThe log to use, the shared disabled log by default.Returns
Returns the newly initialized
Patronclient.Issuing GET Requests
Issues a
GETrequest to the remote API.Parameters
pathThe URL path including first slash, for example"/user".cbThe callback receiving the JSON result as its first parameter, followed by response, and error. All callback parameters may benil.Returns
An executing
URLSessionTask.Example
Searching for Swift Repos on GitHub and printing their names.
Find this example in the Playground included in this repo.
Query String
If you don‘t feel like stringing together the path yourself, you can pass URL query items.
Issues a
GETrequest with query string to the remote API.Parameters
pathThe URL path including first slash, for example"/user".queryAn array of URL query items from Foundation.cbThe callback receiving the JSON result as its first parameter, followed by response, and error. All callback parameters may benil.Returns
An executing
URLSessionTask.Additional Parameters
For more control, there‘s an alternative method with
allowsCellularAccessandcachePolicyparameters.allowsCellularAccesstrueif the request is allowed to use cellular radios.cachePolicyThe cache policy of the request.Issuing POST Requests
Issues a
POSTrequest to the remote API.Parameters
pathThe URL path.jsonThe payload to send as the body of this request.cbThe callback receiving the JSON result as its first parameter, followed by response, and error. All callback parameters may benil.Returns
An executing
URLSessionTask.Throws
PatronError.InvalidJSON, if the potentialjsonpayload is not serializable to JSON byNSJSONSerialization.Getting Information
The hostname of the remote service.
The last
URLSessionorJSONSerializationerror code, and the timestamp at which it occured in Unix time, seconds since00:00:00 UTC on 1 January 1970. The next successful request resetsstatustonil.Test
For testing, we run a little Node.js Server – find it in
Tests/Server.Install
📦 Add
https://github.com/michaelnisi/patronto your package manifest.License
MIT