Skip to content
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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft

Osrm support #198

wants to merge 7 commits into from

Conversation

RomainQuidet
Copy link

I just updated the directions init to really support any custom host. This allows us to use any OSRM server.

@RomainQuidet
Copy link
Author

this is for issue #121

@RomainQuidet
Copy link
Author

any clue why bitrise fails ? It compiles on Xcode 9.1 / swift 3.2

@bsudekum
Copy link

bsudekum commented Nov 3, 2017

It looks like requests are still being made with a Mapbox access token (or it's expecting one to be there):

Selected tests
Test Suite MapboxDirectionsTests.xctest started
V4Tests
    ✗ testPolyline, XCTAssertNil failed: "Error Domain=MBDirectionsErrorDomain Code=-1 "Not Authorized - Invalid Token" UserInfo={NSLocalizedFailureReason=Not Authorized - Invalid Token}" - Error: Error Domain=MBDirectionsErrorDomain Code=-1 "Not Authorized - Invalid Token" UserInfo={NSLocalizedFailureReason=Not Authorized - Invalid Token}
    ✗ testPolyline, XCTAssertNotNil failed -
❌  fatal error: unexpectedly found nil while unwrapping an Optional value

@RomainQuidet
Copy link
Author

Ha yes sorry I need to check the unit tests ! Thanks.

baseURLComponents.scheme = "https"
baseURLComponents.host = mapboxHost
}
else
Copy link

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 {

Copy link
Author

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.

Copy link

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)!
Copy link

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.

Copy link
Author

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"
Copy link

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?

Copy link
Author

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 ?

Copy link

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.

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.

Copy link
Contributor

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
Copy link

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.

Copy link
Author

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.

Copy link
Contributor

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. 🤷‍♂️

@RomainQuidet
Copy link
Author

@bsudekum Hi, how can we move forward with this pull request ? Do I have to change something ?

baseURLComponents.scheme = "https"
baseURLComponents.host = mapboxHost
}
else {

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 {

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.

Copy link
Author

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.

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.

Copy link
Contributor

@vincethecoder vincethecoder Feb 16, 2018

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
}

@bsudekum
Copy link

Also cc @1ec5

]
}

let path: String
Copy link
Contributor

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?

@datwelk
Copy link

datwelk commented Dec 11, 2018

What's the status of this?

@1ec5 1ec5 changed the base branch from master to main October 6, 2020 17:21
@S2Ler S2Ler marked this pull request as draft January 17, 2023 06:04
Copy link
Contributor

@1ec5 1ec5 left a 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
Copy link
Contributor

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"
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants