NatParkSwiftKit

1.0.0

Swift library for the US National Park Service application program interface (NPS API)
MarcoEidinger/npsapi-swift

What's New

Yellowstone

2020-02-15T22:43:06Z

Initial release with support for

  • Parks
  • Alerts
  • NewsReleases
  • VisitorCenters
  • Places (a.k.a Assets)

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)

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Oct 19 2024 16:32:58 GMT-0900 (Hawaii-Aleutian Daylight Time)