From 0f648aa12da5f74e791ac5739e979d7f1dc5fc64 Mon Sep 17 00:00:00 2001 From: Romain Quidet Date: Fri, 13 Oct 2017 17:36:45 +0200 Subject: [PATCH 1/5] [OSRM] accept other hosts than mapbox (OSRM server) --- MapboxDirections/MBDirections.swift | 44 +++++++++++++++++++-------- MapboxDirections/MBRouteOptions.m | 8 ++--- MapboxDirections/MBRouteOptions.swift | 2 +- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/MapboxDirections/MBDirections.swift b/MapboxDirections/MBDirections.swift index 701b076dc..b8aaa0d1d 100644 --- a/MapboxDirections/MBDirections.swift +++ b/MapboxDirections/MBDirections.swift @@ -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,16 @@ 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 . In Info.plist, set the MGLMapboxAccessToken key to your access token, or use the Directions(accessToken:host:) initializer.") - - self.accessToken = accessToken! + if host == nil { + let accessToken = accessToken ?? defaultAccessToken + assert(accessToken != nil && !accessToken!.isEmpty, "A Mapbox access token is required. Go to . 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" + baseURLComponents.host = host ?? mapboxHost self.apiEndpoint = baseURLComponents.url! } @@ -229,11 +234,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 + var params = options.params + if isMapboxHost { + params += [ + URLQueryItem(name: "access_token", value: accessToken), + ] + } + + let path: String + 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)! var components = URLComponents(url: unparameterizedURL, resolvingAgainstBaseURL: true)! components.queryItems = params return components.url! diff --git a/MapboxDirections/MBRouteOptions.m b/MapboxDirections/MBRouteOptions.m index 51cf33965..02100aa66 100644 --- a/MapboxDirections/MBRouteOptions.m +++ b/MapboxDirections/MBRouteOptions.m @@ -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"; diff --git a/MapboxDirections/MBRouteOptions.swift b/MapboxDirections/MBRouteOptions.swift index e5ee31813..9c7217d9b 100644 --- a/MapboxDirections/MBRouteOptions.swift +++ b/MapboxDirections/MBRouteOptions.swift @@ -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" } /** From 9bb15513b7d68095a411f846aee35a106f76e9d7 Mon Sep 17 00:00:00 2001 From: Romain Quidet Date: Fri, 27 Oct 2017 16:32:45 +0200 Subject: [PATCH 2/5] better custom host string handling --- MapboxDirections/MBDirections.swift | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/MapboxDirections/MBDirections.swift b/MapboxDirections/MBDirections.swift index b8aaa0d1d..1f9f5f3c7 100644 --- a/MapboxDirections/MBDirections.swift +++ b/MapboxDirections/MBDirections.swift @@ -131,16 +131,27 @@ 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?) { + var baseURLComponents: URLComponents if host == nil { let accessToken = accessToken ?? defaultAccessToken assert(accessToken != nil && !accessToken!.isEmpty, "A Mapbox access token is required. Go to . 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 } - - var baseURLComponents = URLComponents() - baseURLComponents.scheme = "https" - baseURLComponents.host = 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! } From 2f0d06bbf9b8a497e4802f89f7440e4b98168d4c Mon Sep 17 00:00:00 2001 From: Romain Quidet Date: Fri, 3 Nov 2017 17:18:30 +0100 Subject: [PATCH 3/5] correct mapbox v4 api support --- MapboxDirections/MBRouteOptions.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MapboxDirections/MBRouteOptions.swift b/MapboxDirections/MBRouteOptions.swift index f564b202a..fb457159e 100644 --- a/MapboxDirections/MBRouteOptions.swift +++ b/MapboxDirections/MBRouteOptions.swift @@ -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] { From 12e24233e802a19e42791cfdab037a0bc112ef8c Mon Sep 17 00:00:00 2001 From: Romain Quidet Date: Fri, 3 Nov 2017 17:29:59 +0100 Subject: [PATCH 4/5] review: correct braket place --- MapboxDirections/MBDirections.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/MapboxDirections/MBDirections.swift b/MapboxDirections/MBDirections.swift index 1f9f5f3c7..be3fe3c94 100644 --- a/MapboxDirections/MBDirections.swift +++ b/MapboxDirections/MBDirections.swift @@ -141,8 +141,7 @@ open class Directions: NSObject { baseURLComponents.scheme = "https" baseURLComponents.host = mapboxHost } - else - { + else { var urlString = host! if urlString.hasPrefix("http") == false { urlString = "http://\(urlString)" From d2cce68548e431d48838dedc3e326fda5feec0a8 Mon Sep 17 00:00:00 2001 From: Romain Quidet Date: Fri, 10 Nov 2017 17:33:33 +0100 Subject: [PATCH 5/5] review: reposition the else --- MapboxDirections/MBDirections.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MapboxDirections/MBDirections.swift b/MapboxDirections/MBDirections.swift index be3fe3c94..23137a22e 100644 --- a/MapboxDirections/MBDirections.swift +++ b/MapboxDirections/MBDirections.swift @@ -141,7 +141,7 @@ open class Directions: NSObject { baseURLComponents.scheme = "https" baseURLComponents.host = mapboxHost } - else { + else { var urlString = host! if urlString.hasPrefix("http") == false { urlString = "http://\(urlString)"