-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Osrm support #198
base: main
Are you sure you want to change the base?
Osrm support #198
Changes from all commits
0f648aa
f9ce0de
9bb1551
f176ef0
2f0d06b
12e2423
d2cce68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,9 @@ public let MBDirectionsErrorDomain = "MBDirectionsErrorDomain" | |
/// The Mapbox access token specified in the main application bundle’s Info.plist. | ||
let defaultAccessToken = Bundle.main.object(forInfoDictionaryKey: "MGLMapboxAccessToken") as? String | ||
|
||
/// The Mapbox public host | ||
internal let mapboxHost = "api.mapbox.com" | ||
|
||
/// The user agent string for any HTTP requests performed directly within this library. | ||
let userAgent: String = { | ||
var components: [String] = [] | ||
|
@@ -114,12 +117,12 @@ open class Directions: NSObject { | |
*/ | ||
@objc(sharedDirections) | ||
open static let shared = Directions(accessToken: nil) | ||
|
||
/// The API endpoint to request the directions from. | ||
internal var apiEndpoint: URL | ||
|
||
/// The Mapbox access token to associate the request with. | ||
internal let accessToken: String | ||
internal var accessToken: String? | ||
|
||
/** | ||
Initializes a newly created directions object with an optional access token and host. | ||
|
@@ -128,14 +131,26 @@ open class Directions: NSObject { | |
- parameter host: An optional hostname to the server API. The [Mapbox Directions API](https://www.mapbox.com/api-documentation/?language=Swift#directions) endpoint is used by default. | ||
*/ | ||
public init(accessToken: String?, host: String?) { | ||
let accessToken = accessToken ?? defaultAccessToken | ||
assert(accessToken != nil && !accessToken!.isEmpty, "A Mapbox access token is required. Go to <https://www.mapbox.com/studio/account/tokens/>. In Info.plist, set the MGLMapboxAccessToken key to your access token, or use the Directions(accessToken:host:) initializer.") | ||
|
||
self.accessToken = accessToken! | ||
|
||
var baseURLComponents = URLComponents() | ||
baseURLComponents.scheme = "https" | ||
baseURLComponents.host = host ?? "api.mapbox.com" | ||
var baseURLComponents: URLComponents | ||
if host == nil { | ||
let accessToken = accessToken ?? defaultAccessToken | ||
assert(accessToken != nil && !accessToken!.isEmpty, "A Mapbox access token is required. Go to <https://www.mapbox.com/studio/account/tokens/>. In Info.plist, set the MGLMapboxAccessToken key to your access token, or use the Directions(accessToken:host:) initializer.") | ||
|
||
self.accessToken = accessToken | ||
baseURLComponents = URLComponents() | ||
baseURLComponents.scheme = "https" | ||
baseURLComponents.host = mapboxHost | ||
} | ||
else { | ||
var urlString = host! | ||
if urlString.hasPrefix("http") == false { | ||
urlString = "http://\(urlString)" | ||
} | ||
let url = URL(string: urlString) | ||
assert(url != nil, "A valid custom host must be provided (ex: my_host.com or full http://my_host.com:8080") | ||
baseURLComponents = URLComponents(url: url!, resolvingAgainstBaseURL: false)! | ||
} | ||
|
||
self.apiEndpoint = baseURLComponents.url! | ||
} | ||
|
||
|
@@ -229,11 +244,26 @@ open class Directions: NSObject { | |
*/ | ||
@objc(URLForCalculatingDirectionsWithOptions:) | ||
open func url(forCalculating options: RouteOptions) -> URL { | ||
let params = options.params + [ | ||
URLQueryItem(name: "access_token", value: accessToken), | ||
] | ||
|
||
let unparameterizedURL = URL(string: options.path, relativeTo: apiEndpoint)! | ||
let isMapboxHost = apiEndpoint.host == mapboxHost | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is tricky. We use access tokens on other hosts that are not equal to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you wish. The goal here is to know when we are using mapbox host. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
var params = options.params | ||
if isMapboxHost { | ||
params += [ | ||
URLQueryItem(name: "access_token", value: accessToken), | ||
] | ||
} | ||
|
||
let path: String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this be a |
||
if isMapboxHost { | ||
path = options.path | ||
} | ||
else | ||
{ | ||
assert(!options.queries.isEmpty, "No query") | ||
let queryComponent = options.queries.joined(separator: ";") | ||
path = "route/v1/\(options.profileIdentifier.rawValue)/\(queryComponent)" | ||
} | ||
|
||
let unparameterizedURL = URL(string: path, relativeTo: apiEndpoint)! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be properly unwrapped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not changed this unparameterizedURL unwrap... It is the original code. |
||
var components = URLComponents(url: unparameterizedURL, resolvingAgainstBaseURL: true)! | ||
components.queryItems = params | ||
return components.url! | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#import "MBRouteOptions.h" | ||
|
||
|
||
MBDirectionsProfileIdentifier const MBDirectionsProfileIdentifierAutomobile = @"mapbox/driving"; | ||
MBDirectionsProfileIdentifier const MBDirectionsProfileIdentifierAutomobileAvoidingTraffic = @"mapbox/driving-traffic"; | ||
MBDirectionsProfileIdentifier const MBDirectionsProfileIdentifierCycling = @"mapbox/cycling"; | ||
MBDirectionsProfileIdentifier const MBDirectionsProfileIdentifierWalking = @"mapbox/walking"; | ||
MBDirectionsProfileIdentifier const MBDirectionsProfileIdentifierAutomobile = @"driving"; | ||
MBDirectionsProfileIdentifier const MBDirectionsProfileIdentifierAutomobileAvoidingTraffic = @"driving-traffic"; | ||
MBDirectionsProfileIdentifier const MBDirectionsProfileIdentifierCycling = @"cycling"; | ||
MBDirectionsProfileIdentifier const MBDirectionsProfileIdentifierWalking = @"walking"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -335,7 +335,7 @@ open class RouteOptions: NSObject, NSSecureCoding { | |
assert(!queries.isEmpty, "No query") | ||
|
||
let queryComponent = queries.joined(separator: ";") | ||
return "directions/v5/\(profileIdentifier.rawValue)/\(queryComponent).json" | ||
return "directions/v5/mapbox/\(profileIdentifier.rawValue)/\(queryComponent).json" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we actually pull this out into a public variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I have to do it ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RomainQuidet yes, sometimes this is not equal to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change should be unnecessary, because you can initialize a |
||
} | ||
|
||
/** | ||
|
@@ -531,7 +531,7 @@ open class RouteOptionsV4: RouteOptions { | |
|
||
let profileIdentifier = self.profileIdentifier.rawValue.replacingOccurrences(of: "/", with: ".") | ||
let queryComponent = queries.joined(separator: ";") | ||
return "v4/directions/\(profileIdentifier)/\(queryComponent).json" | ||
return "v4/directions/mapbox.\(profileIdentifier)/\(queryComponent).json" | ||
} | ||
|
||
override var params: [URLQueryItem] { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be cleaner to do a guard let here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I quite disagree. It's a pure if else. There's nothing to guard against.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're guarding against assuming
host
is not nil and also this assert. IMO, asserting is great time to throw in a guard.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RomainQuidet upon reviewing the code, I understand your intent. Probably it would be more clear if you safely unwrapped the
host
variable like so: