Skip to content

Swift library for the US National Park Service application program interface (NPS API)

License

Notifications You must be signed in to change notification settings

MarcoEidinger/npsapi-swift

Repository files navigation

NatParkSwiftKit

Swift Package Manager Compatible Carthage compatible

Build Status codebeat badge codecov.io documentation

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.

The required API key can be requested for free from NPS Developer website

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 a default, the result set is limited to 50 records. Hence, in the 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 start 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)

About

Swift library for the US National Park Service application program interface (NPS API)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published