Skip to content

Commit

Permalink
[SSDK-544] Replace supportSBS binary with expandable ApiType enum (#162)
Browse files Browse the repository at this point in the history
### Description

Fixes [SSDK-544](https://mapbox.atlassian.net/browse/SSDK-544)

- Change AbstractSearchEngine.init `supportSBS: Bool = false` parameter to `apiType: ApiType = .SBS`. 
- This changes the default API engine for discover/category and other API requests to SBS. 
- Add ApiType enum to represent non-Autofill and non-PlaceAutocomplete SearchEngine API types.
- This will be expanded to include the new [search-box](https://docs.mapbox.com/api/search/search-box/) API type

### Checklist
- [x] Update `CHANGELOG`

[SSDK-544]: https://mapbox.atlassian.net/browse/SSDK-544
  • Loading branch information
aokj4ck authored Feb 5, 2024
1 parent b779627 commit 742cf38
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 17 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Guide: https://keepachangelog.com/en/1.0.0/

<!-- Add changes for active work here -->

## 2.0.0
- [Core] Change AbstractSearchEngine.init `supportSBS: Bool = false` parameter to `apiType: ApiType = .SBS`. This changes the default API engine for discover/category and other API requests to SBS. Add ApiType enum to represent non-Autofill and non-PlaceAutocomplete SearchEngine API types.

## 2.0.0-rc.1

- [Discover] Fix charging station category canonical ID
- [SearchUI] Rename MapboxPanelController.Configuration to .PanelConfiguration. This disambiguates PanelConfiguration from the broader Configuration struct.
Expand Down
16 changes: 16 additions & 0 deletions MapboxSearch.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
043A3D4D2B30F38300DB681B /* CoreAddress+AddressComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 043A3D4C2B30F38300DB681B /* CoreAddress+AddressComponents.swift */; };
04C0848D2B4C82F3002F9C69 /* SdkInformation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04C0848C2B4C82F3002F9C69 /* SdkInformation.swift */; };
04C127552B62F6BC00884325 /* ApiType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04C127542B62F6BC00884325 /* ApiType.swift */; };
04C127582B62FFDB00884325 /* ApiType+Core.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04C127572B62FFDB00884325 /* ApiType+Core.swift */; };
04E5FF962B48828500DADC18 /* SearchAddressCountry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04E5FF952B48828500DADC18 /* SearchAddressCountry.swift */; };
04E5FF992B48829200DADC18 /* SearchAddressRegion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04E5FF982B48829200DADC18 /* SearchAddressRegion.swift */; };
140D1BDC286DB479001A51C2 /* SearchResultAccuracy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 140D1BDB286DB479001A51C2 /* SearchResultAccuracy.swift */; };
Expand Down Expand Up @@ -481,6 +483,8 @@
/* Begin PBXFileReference section */
043A3D4C2B30F38300DB681B /* CoreAddress+AddressComponents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoreAddress+AddressComponents.swift"; sourceTree = "<group>"; };
04C0848C2B4C82F3002F9C69 /* SdkInformation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SdkInformation.swift; sourceTree = "<group>"; };
04C127542B62F6BC00884325 /* ApiType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiType.swift; sourceTree = "<group>"; };
04C127572B62FFDB00884325 /* ApiType+Core.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ApiType+Core.swift"; sourceTree = "<group>"; };
04E5FF952B48828500DADC18 /* SearchAddressCountry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchAddressCountry.swift; sourceTree = "<group>"; };
04E5FF982B48829200DADC18 /* SearchAddressRegion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchAddressRegion.swift; sourceTree = "<group>"; };
140D1BDB286DB479001A51C2 /* SearchResultAccuracy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultAccuracy.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -897,6 +901,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
04C127562B62FFD000884325 /* Engine */ = {
isa = PBXGroup;
children = (
04C127572B62FFDB00884325 /* ApiType+Core.swift */,
);
path = Engine;
sourceTree = "<group>";
};
04E5FF972B48829200DADC18 /* Region */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1364,6 +1376,7 @@
isa = PBXGroup;
children = (
FEEDD2E32508DFE400DC0A98 /* AbstractSearchEngine.swift */,
04C127542B62F6BC00884325 /* ApiType.swift */,
FEEDD2D82508DFE400DC0A98 /* SearchEngine.swift */,
FEEDD2EB2508DFE400DC0A98 /* CategorySearchEngine.swift */,
04C0848C2B4C82F3002F9C69 /* SdkInformation.swift */,
Expand Down Expand Up @@ -1546,6 +1559,7 @@
FEEDD2C12508DFE400DC0A98 /* InternalAPI */ = {
isa = PBXGroup;
children = (
04C127562B62FFD000884325 /* Engine */,
148DE66E285777050085684D /* Common */,
F98BECBB2577B9150081D3BC /* Telemetry */,
FEEDD2C32508DFE400DC0A98 /* Array+Extensions.swift */,
Expand Down Expand Up @@ -2350,6 +2364,7 @@
F93DA6FF274E4B86005595B5 /* CoreSearchEngineStatics.swift in Sources */,
14F71865299FD4BD00D5BC2E /* PlaceAutocomplete+PlaceType.swift in Sources */,
F9F6378A25DD6A4E00C99904 /* FeedbackManager.swift in Sources */,
04C127582B62FFDB00884325 /* ApiType+Core.swift in Sources */,
148DE65D28574E6E0085684D /* AddressAutofill.swift in Sources */,
140E47A7298BCBDF00677E30 /* Discover+Query.swift in Sources */,
FEEDD3092508DFE400DC0A98 /* CodablePersistentService.swift in Sources */,
Expand Down Expand Up @@ -2443,6 +2458,7 @@
FEEDD3152508DFE400DC0A98 /* ServerSearchResult.swift in Sources */,
140E47A2298BC90E00677E30 /* Discover.swift in Sources */,
14FA65872953644400056E5B /* PlaceAutocomplete+Suggestion.swift in Sources */,
04C127552B62F6BC00884325 /* ApiType.swift in Sources */,
FE059C5A251A0AE6001F7701 /* SearchCategorySuggestionImpl.swift in Sources */,
FEEDD2F62508DFE400DC0A98 /* CoreSearchResultResponse.swift in Sources */,
FE260A6725C063880037B725 /* ReverseGeocodingOptions.swift in Sources */,
Expand Down
14 changes: 14 additions & 0 deletions Sources/MapboxSearch/InternalAPI/Engine/ApiType+Core.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright © 2024 Mapbox. All rights reserved.

import Foundation

extension ApiType {
func toCore() -> CoreSearchEngine.ApiType {
switch self {
case .geocoding:
return .geocoding
case .SBS:
return .SBS
}
}
}
18 changes: 9 additions & 9 deletions Sources/MapboxSearch/PublicAPI/Engine/AbstractSearchEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class AbstractSearchEngine: FeedbackManagerDelegate {

var locationProviderWrapper: WrapperLocationProvider?

/// SearchEngine supports the latest Single-Box Search APIs
public let supportSBS: Bool
/// Provide a specific API type
public let apiType: ApiType

/// Location provider for search results `proximity` argument
public let locationProvider: LocationProvider?
Expand Down Expand Up @@ -54,27 +54,27 @@ public class AbstractSearchEngine: FeedbackManagerDelegate {
/// - locationProvider: Provider configuration of LocationProvider that would grant location data by default
/// - serviceProvider: Internal `ServiceProvider` for sharing common dependencies like favoritesService or
/// eventsManager
/// - supportSBS: enable support the latest Single-Box Search APIs
/// - apiType: choose which API provider to use through this search engine
init(
accessToken: String? = nil,
serviceProvider: ServiceProviderProtocol & EngineProviderProtocol,
locationProvider: LocationProvider? = DefaultLocationProvider(),
defaultSearchOptions: SearchOptions = SearchOptions(),
supportSBS: Bool = false
apiType: ApiType = .SBS
) {
guard let accessToken = accessToken ?? serviceProvider.getStoredAccessToken() else {
fatalError(
"No access token was found. Please, provide it in init(accessToken:) or in Info.plist at '\(accessTokenPlistKey)' key"
)
}

self.supportSBS = supportSBS
self.locationProvider = locationProvider
self.locationProviderWrapper = WrapperLocationProvider(wrapping: locationProvider)
self.eventsManager = serviceProvider.eventsManager
self.feedbackManager = serviceProvider.feedbackManager
self.defaultSearchOptions = defaultSearchOptions
self.engineApi = supportSBS ? .SBS : .geocoding
self.apiType = apiType
self.engineApi = apiType.toCore()

self.userActivityReporter = .getOrCreate(
for: .init(
Expand Down Expand Up @@ -110,19 +110,19 @@ public class AbstractSearchEngine: FeedbackManagerDelegate {
/// for `nil` argument
/// - locationProvider: Provider configuration of LocationProvider that would grant location data by default
/// - defaultSearchOptions: Default options to use when `nil` was passed to the `search(…: options:)` call
/// - supportSBS: enable support the latest Single-Box Search APIs
/// - apiType: choose which API provider to use through this search engine
public convenience init(
accessToken: String? = nil,
locationProvider: LocationProvider? = DefaultLocationProvider(),
defaultSearchOptions: SearchOptions = SearchOptions(),
supportSBS: Bool = false
apiType: ApiType = .SBS
) {
self.init(
accessToken: accessToken,
serviceProvider: ServiceProvider.shared,
locationProvider: locationProvider,
defaultSearchOptions: defaultSearchOptions,
supportSBS: supportSBS
apiType: apiType
)
}

Expand Down
12 changes: 12 additions & 0 deletions Sources/MapboxSearch/PublicAPI/Engine/ApiType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright © 2024 Mapbox. All rights reserved.

import Foundation

/// Determine which Mapbox API to use for a SearchEngine instance
public enum ApiType {
/// The Mapbox Geocoding (a.k.a V5) API - https://docs.mapbox.com/api/search/geocoding/
case geocoding

/// The Mapbox Single Box Search (a.k.a Federation API) - https://docs.mapbox.com/api/search/search/
case SBS
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class Discover {
let searchEngine = CategorySearchEngine(
accessToken: accessToken,
locationProvider: locationProvider,
supportSBS: true
apiType: .SBS
)

let userActivityReporter = CoreUserActivityReporter.getOrCreate(
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapboxSearchUI/MapboxSearchController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ extension MapboxSearchController: SearchCategoriesRootViewDelegate {
}

func userSelectedCategory(_ category: SearchCategory) {
let categoryName = categorySearchEngine.supportSBS ? category.canonicalId : category.legacyName
let categoryName = categorySearchEngine.apiType == .geocoding ? category.legacyName : category.canonicalId

categorySearchEngine.search(categoryName: categoryName, options: categorySearchOptions) { results in
switch results {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CategorySearchEngineIntegrationTests: MockServerTestCase {
lazy var searchEngine = CategorySearchEngine(
accessToken: "access-token",
locationProvider: DefaultLocationProvider(),
supportSBS: true
apiType: .SBS
)

func testCategorySearch() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ final class PlaceAutocompleteIntegrationTests: MockServerTestCase {
wait(for: [expectation], timeout: 5)

let selectionExpectation = XCTestExpectation(description: "Expecting selection result")
placeAutocomplete.select(suggestion: suggestion!) { result in
let unwrappedSuggestion = try XCTUnwrap(suggestion)
placeAutocomplete.select(suggestion: unwrappedSuggestion) { result in
switch result {
case .success(let resolvedSuggestion):
XCTAssertEqual(resolvedSuggestion.name, "San Francisco")
Expand Down Expand Up @@ -92,7 +93,8 @@ final class PlaceAutocompleteIntegrationTests: MockServerTestCase {

try server.setResponse(.retrieveMinsk)
let selectionExpectation = XCTestExpectation(description: "Expecting selection result")
placeAutocomplete.select(suggestion: suggestion!) { result in
let unwrappedSuggestion = try XCTUnwrap(suggestion)
placeAutocomplete.select(suggestion: unwrappedSuggestion) { result in
switch result {
case .success(let resolvedSuggestion):
XCTAssertEqual(resolvedSuggestion.name, "Minsk")
Expand Down Expand Up @@ -225,7 +227,8 @@ final class PlaceAutocompleteIntegrationTests: MockServerTestCase {

try server.setResponse(.retrievePoi)
let selectionExpectation = XCTestExpectation(description: "Expecting selection result")
placeAutocomplete.select(suggestion: suggestion!) { result in
let unwrappedSuggestion = try XCTUnwrap(suggestion)
placeAutocomplete.select(suggestion: unwrappedSuggestion) { result in
switch result {
case .success(let resolvedSuggestion):
XCTAssertEqual(resolvedSuggestion.name, "Starbucks")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SearchEngineIntegrationTests: MockServerTestCase {
lazy var searchEngine = SearchEngine(
accessToken: "access-token",
locationProvider: DefaultLocationProvider(),
supportSBS: true
apiType: .SBS
)

override func setUpWithError() throws {
Expand Down

0 comments on commit 742cf38

Please sign in to comment.