NatParkSwiftKit
Swift library for the US National Park Service application program interface (NPS API). The API provides information about parks / monuments / historical sites throughout the US.
Required API key can be requested for free from NPS Developer website
Disclaimer
Project is in inital development state and API incompatible changes can occur at any time in the master branch. As soon as the 1st release is done (~ Feb 2020) then API compatibility becomes target for future development activities.
Installation
Swift Package Manager
If you encounter any problem or have a question on adding package to an Xcode project, I suggest the Adding Package Dependencies to Your App guide article from Apple.
Carthage
Add the following to your Cartfile.
github "MarcoEidinger/npsapi-swift" "master"
Usage
Example to fetch information for a single park
import NatParkSwiftKit
let api = DataService(apiKey: "your-secret-API-key")
let cancellablePipeline = api.fetchParks()
.replaceError(with: nil)
.sink { (park) in
guard let park = park else { return }
print("Park \(park.parkCode) is a \(park.designation)")
}
Parks and other entities of the National Park Service Data API can be fetched in bulks. The result type is a tuple containing 1) the data and 2) total count (of items matching your query)
import NatParkSwiftKit
let api = DataService(apiKey: "your-secret-API-key")
let cancellablePipeline = api.fetchParks()
.sink(receiveCompletion: { _ in
print("Park request completed (either failed or was successful)")
}, receiveValue: { (results) in
let (parks, allParksCount) = results
parks.forEach {
print("Park \($0.parkCode) is a \($0.designation)")
}
}
)
As default the result set is limited to 50 records. Hence, in previous example the following is true
// parks.count == 50
// allParksCount >= 497
The limit can be decreased or increased by setting limit in RequestOptions
Below is a more complex search
import NatParkSwiftKit
let api = DataService(apiKey: "your-secret-API-key")
let publisher = api.fetchParks(by: nil, in: [.california], RequestOptions.init(limit: 5, searchQuery: "Yosemite National Park", fields: [.images, .entranceFees, .entrancePasses]))
let subscription = publisher
.sink(receiveCompletion:
{ (completion) in
switch completion {
case .finished:
print("Finished successfully")
case .failure(let error):
print(error)
}
}
) { (results) in
let (parks, _) = results
print(parks.count) // 1
}
Analog to the HTTP API it is possible to use pagination by specifying star in conjunction with limit of RequestOptions
.
However, I discourage to use it as the NPS server implementation seems to be unreliable
Complete client-side API documentation is available here
Supported Types
- Parks
- Alerts
- NewsReleases
- VisitorCenters
- Places (a.k.a Assets)