Swift Federal Data SDK

This class is used to ease access to a variety of federal APIs (not just DOL's), providing a simple, easy to use interface that is consistent for all APIs that are used. (Get the code) (Sample App)

Important: This SDK was built using pre-release technology. This SDK is in beta until it has been successfully tested with a production release version of Apple's Swift language. Because of this, if Apple makes any changes to the underlying language, tools, and/or compiler, this SDK may break until fixes are made. This SDK has been updated to support Swift 1.2, so it may produce errors in some versions of Xcode. For earlier versions of Swift, use release 1.0.0 of this SDK.

note: To use DOL's API you must register at https://devtools.dol.gov/developer and request an API key for each application that will access the API. Registration and API keys are free. A separate API key is not necessary for each application, but if you reuse your key across multiple apps, your apps' metrics will be combined and of little value.

API key requirements vary between federal agency's APIs

Creating An API Request Object

GovDataRequest(APIKey:APIHost:APIURL:)

Initializes an API request for a given endpoint, minus the "method" piece and arguments.
GovDataRequest(APIKey: String, APIHost: String, APIURL: String)

Parameters

Name Optional / Required Data Type Description
APIKey Optional String API Key or token, if required by the API
APIHost Required String The hostname of the API (e.g. http://api.dol.gov)
APIURL Optional String The part of the API endpoint URL between APIHost and the method (e.g. /V1)

Protocol

GovDataRequestProtocol

The protocol your object must conform to when using the GovDataRequest object
class className: GovDataRequestProtocol

Setting General Properties

timeOut

The timeOut interval to use when waiting for additional data. The default value is 60.0.
var timeOut: NSTimeInterval

Required Delegate Methods

didCompleteWithError(errorMessage:)

Returns to the delegate the error message from a failed API call (if any).
func didCompleteWithError(errorMessage: String)

Return Values

Name Data Type Description
errorMessage String Error Message returned from API call

didCompleteWithDictionary(results:)

Returns to the delegate the API response, parsed and presented as an NSDictionary object.
func didCompleteWithDictionary(results: NSDictionary)

Return Values

Name Data Type Description
results NSDictionary The parsed API response

didCompleteWithXML(results:)

Returns to the delegate the API response, parsed and presented as an NSDictionary object.
func didCompleteWithXML(results: XMLIndexer)

Return Values

Name Data Type Description
results XMLIndexer The parsed API response. How to access your data:
// will return "Test Title Header"
xml["root"]["header"]["title"].element?.text

// will return "Ralls, Kim"
xml["root"]["catalog"]["book"][1]["author"].element?.text

// will return "bk102"
xml["root"]["catalog"]["book"][1].element?.attributes["id"]

// will return "Computer, Fantasy, Fantasy"
", ".join(xml["root"]["catalog"]["book"].all.map { elem in elem["genre"].element!.text! })

// error handling
switch xml["root"]["what"]["header"]["foo"] {
case .Element(let elem):
  // everything is good, code away!
case .Error(let error):
  // error is an NSError instance that you can deal with
}

Sending the Request to the API

callAPIMethod(method:arguments:)

Send the request to the API, including arguments.
func callAPIMethod(method: String, arguments: Dictionary<String,String>)

Parameters

Name Optional / Required Data Type Description
method Required String The remaining portion of the URL endpoint, minus the arguments. Unlike the APIURL, this may be likely to change as you use different datasets from the same source.
arguments Required Dictionary<String,String> Key/value pairs of arguments to send to the API.