Skip to content

Commit

Permalink
Read API endpoint from environment
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Aug 23, 2022
1 parent 5091cd1 commit d6ca593
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Added `Waypoint.allowsSnappingToStaticallyClosedRoad` property to allow snapping the waypoint’s location to a statically (long-term) closed part of a road. ([#721](https://github.com/mapbox/mapbox-directions-swift/pull/721))
* `RouteOptions(url:)` now returns `nil` if given a Mapbox Map Matching API request URL, and `MatchOptions(url:)` returns `nil` if given a Mapbox Directions API request URL. ([#728](https://github.com/mapbox/mapbox-directions-swift/pull/728))
* When the `MAPBOX_ACCESS_TOKEN` environment variable is unset, the `mapbox-directions-swift` command line tool exits with an error code instead of crashing. ([#728](https://github.com/mapbox/mapbox-directions-swift/pull/728))
* The `mapbox-directions-swift` command line tool now connects to the API endpoint in the `MAPBOX_HOST` environment variable, if specified. ([#728](https://github.com/mapbox/mapbox-directions-swift/pull/728))

## v2.6.0

Expand Down
2 changes: 2 additions & 0 deletions CommandLineTool.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ To run the `MapboxDirectionsCLI` within Xcode, select the `MapboxDirectionsCLI`

A [Mapbox access token](https://account.mapbox.com/access-tokens/) is required for some operations. Set the `MAPBOX_ACCESS_TOKEN` environment variable to your access token.

To connect to an API endpoint other than the default Mapbox API endpoint, set the `MAPBOX_HOST` environment variable to the base URL.

## Usage and Recipes

`mapbox-directions-swift` is a useful tool for mobile quality assurance. This tool can be used to verify a response to ensure proper Directions API integration, get a [GPX](https://wikipedia.org/wiki/GPS_Exchange_Format) trace that can be used in the Xcode Simulator, and convert a Directions API request to an Options object.
Expand Down
19 changes: 13 additions & 6 deletions Sources/MapboxDirectionsCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ struct ProcessingOptions: ParsableArguments {
}

struct Command: ParsableCommand {
static var accessToken: String {
static var credentials: Credentials {
get throws {
guard let accessToken = ProcessInfo.processInfo.environment["MAPBOX_ACCESS_TOKEN"] ??
UserDefaults.standard.string(forKey: "MBXAccessToken") else {
throw ValidationError("A Mapbox access token is required. Go to <https://account.mapbox.com/access-tokens/>, then set the MAPBOX_ACCESS_TOKEN environment variable to your access token.")
}
return accessToken

let hostURL: URL?
if let host = ProcessInfo.processInfo.environment["MAPBOX_HOST"] ??
UserDefaults.standard.string(forKey: "MGLMapboxAPIBaseURL") {
hostURL = URL(string: host)
} else {
hostURL = nil
}

return Credentials(accessToken: accessToken, host: hostURL)
}
}

Expand Down Expand Up @@ -64,8 +73,7 @@ extension Command {
}

mutating func run() throws {
let credentials = Credentials(accessToken: try accessToken)
try CodingOperation<MapMatchingResponse, MatchOptions>(options: options, credentials: credentials).execute()
try CodingOperation<MapMatchingResponse, MatchOptions>(options: options, credentials: try credentials).execute()
}
}
}
Expand All @@ -83,8 +91,7 @@ extension Command {
}

mutating func run() throws {
let credentials = Credentials(accessToken: try accessToken)
try CodingOperation<RouteResponse, RouteOptions>(options: options, credentials: credentials).execute()
try CodingOperation<RouteResponse, RouteOptions>(options: options, credentials: try credentials).execute()
}
}
}
Expand Down

0 comments on commit d6ca593

Please sign in to comment.