-
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
Conversation
this is for issue #121 |
any clue why bitrise fails ? It compiles on Xcode 9.1 / swift 3.2 |
It looks like requests are still being made with a Mapbox access token (or it's expecting one to be there):
|
Ha yes sorry I need to check the unit tests ! Thanks. |
MapboxDirections/MBDirections.swift
Outdated
baseURLComponents.scheme = "https" | ||
baseURLComponents.host = mapboxHost | ||
} | ||
else |
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.
Could you fix the indentation here and also put curly braces on the same line as the else? } else {
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 use tabs for indentation in my Xcode... is that a problem ?
I moved the brackets correctly.
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.
👍 , tabs should be fine. Just trying to keep the library consistent.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I did not changed this unparameterizedURL unwrap... It is the original code.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Could we actually pull this out into a public variable profilePrefix
?
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.
Do I have to do it ?
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 yes, sometimes this is not equal to mapbox
.
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.
mapbox
still needs to be pulled out into a public property.
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.
This change should be unnecessary, because you can initialize a ProfileIdentifier
to hold any arbitrary identifier using ProfileIdentifier(rawValue:)
. If this is inconvenient, an application can always extend ProfileIdentifier
with additional constants as desired.
] | ||
|
||
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 comment
The 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 mapboxHost
. Perhaps we can make mapboxHost
public so we can override this when using other mapbox hostnames.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
The access_token
parameter is unnecessary when talking to an OSRM endpoint, but it should already be easy enough to set the access token to something bogus, as the unit tests do. If the OSRM endpoint is erroring out because of that parameter, I guess we could’ve made Credentials(accessToken:)
take an empty string as a signal to not add the access_token
parameter to the URL. 🤷♂️
@bsudekum Hi, how can we move forward with this pull request ? Do I have to change something ? |
MapboxDirections/MBDirections.swift
Outdated
baseURLComponents.scheme = "https" | ||
baseURLComponents.host = mapboxHost | ||
} | ||
else { |
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.
Can you put the previous curly brace and the else
on the same line please?
baseURLComponents.scheme = "https" | ||
baseURLComponents.host = host ?? "api.mapbox.com" | ||
var baseURLComponents: URLComponents | ||
if host == nil { |
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:
if let host = host {
var urlString = host
if urlString.hasPrefix("http") == false {
urlString = "http://\(urlString)"
}
assert(URL(string: urlString) != nil, "A valid custom host must be provided (ex: my_host.com or full http://my_host.com:8080")
if let url = URL(string: urlString), let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
baseURLComponents = urlComponents
}
} else {
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
}
Also cc @1ec5 |
] | ||
} | ||
|
||
let path: String |
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.
shouldn't this be a var
?
What's the status of this? |
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 always felt bad about never getting around to reviewing this PR after having entertained the notion in #121. That was about when navigation went from being a side hobby project to something real inside Mapbox. Just to set the record straight about that issue, the original idea was that this library could add some facilities to process raw OSRM responses as easily as Mapbox Directions API responses. As far as I knew, there wasn’t a particular need to support the OSRM request format differently than Mapbox Directions API requests. Later on, the more flexible JSON deserialization in #382 and #669 took away some of the hurdles to parsing workalike responses too.
This PR seems to be focused on the request format, but it isn’t clear to me that requesting routes was ever blocked in the first place. Rather, over the years, Mapbox request and response formats have both accumulated new bits, some of which the navigation SDK has come to depend on. The most prominent part is banner and voice instructions, although the navigation SDK can kind of degrade gracefully without them.
Unfortunately, I’m not sure there’s much to be gained by leaving this PR open, since the library has been refactored so many times since.
] | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
The access_token
parameter is unnecessary when talking to an OSRM endpoint, but it should already be easy enough to set the access token to something bogus, as the unit tests do. If the OSRM endpoint is erroring out because of that parameter, I guess we could’ve made Credentials(accessToken:)
take an empty string as a signal to not add the access_token
parameter to the URL. 🤷♂️
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
This change should be unnecessary, because you can initialize a ProfileIdentifier
to hold any arbitrary identifier using ProfileIdentifier(rawValue:)
. If this is inconvenient, an application can always extend ProfileIdentifier
with additional constants as desired.
I just updated the directions init to really support any custom host. This allows us to use any OSRM server.