mapbox / mapbox-directions-swift

Traffic-aware directions and map matching in Swift on iOS, macOS, tvOS, watchOS, and Linux
https://www.mapbox.com/navigation/
ISC License
183 stars 88 forks source link

Accept request URL in lieu of configuration file #728

Closed 1ec5 closed 2 years ago

1ec5 commented 2 years ago

Overhauled the command line tool’s configuration option. Instead of the --config option that takes a JSON configuration file path, the tool now takes a single positional argument that can either be a JSON configuration file path or a Directions API or Map Matching API request URL. When you pass in a request URL, the URL is round-tripped through a DirectionsOptions object back to a URL, exercising the entire typical code path (and then some).

But if you do decide to pass in a JSON configuration file instead of a URL, the command line tool now respects the MAPBOX_HOST environment variable alongside the MAPBOX_ACCESS_TOKEN environment variable. An access token is scoped to a single API endpoint, so this configuration option makes it possible to run the tool against a proxy or staging server for testing purposes.

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. Previously, you could mix and match URLs to convert between RouteOptions and MatchOptions. Now, you’ll need to use JSONEncoder and JSONDecoder as well:

guard let url = URL(string: "https://api.mapbox.com/directions/v5/mapbox/driving-traffic/…"),
    let routeOptions = RouteOptions(url: url),
    let encodedOptions = try? JSONEncoder().encode(self) else { return }
try JSONDecoder().decode(MatchOptions.self, from: encodedOptions)

564 would unblock a more convenient conversion workflow.

Some tangential changes along the way:

Fixes #692 and fixes #724. Supersedes #580. Depends on #727.

/cc @mapbox/navigation-ios @purew