-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add Detail API lookup by Mapbox ID #298
base: main
Are you sure you want to change the base?
Changes from all commits
7fb6620
3013d8d
317d904
e170bb3
503837f
098f545
d2907b8
e9cad76
50d8e4b
6033200
9912d94
006564e
8ac31d0
e2ce12e
aa5aff7
d713245
19893e9
20b23ad
cd365f4
89591ae
b98a9d7
ab87824
5ab837c
0b3a116
239de56
f481ab4
6d8ff50
5299cce
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 |
---|---|---|
|
@@ -209,6 +209,8 @@ public class SearchEngine: AbstractSearchEngine { | |
offlineMode == .disabled ? engine.reverseGeocoding : engine.reverseGeocodingOffline | ||
} | ||
|
||
/// Concrete implementation for a typical ``select(suggestions:)`` invocation. Performs the work of a Mapbox API | ||
/// retrieve/ endpoint request. | ||
func retrieve( | ||
suggestion: SearchSuggestion, | ||
retrieveOptions: RetrieveOptions? | ||
|
@@ -336,6 +338,37 @@ public class SearchEngine: AbstractSearchEngine { | |
} | ||
} | ||
|
||
/// Process a single response from ``retrieve(mapboxID:options:)`` | ||
/// - Parameters: | ||
/// - coreResponse: The API response | ||
/// - mapboxID: The ID used to create the original request. | ||
private func processDetailsResponse(_ coreResponse: CoreSearchResponseProtocol?, mapboxID: String) { | ||
assert(offlineMode == .disabled) | ||
guard let response = preProcessResponse(coreResponse) else { | ||
return | ||
} | ||
|
||
switch response.process() { | ||
case .success(let responseResult): | ||
// Response Info is not supported for retrieving by Details API at this time. | ||
responseInfo = nil | ||
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 |
||
|
||
guard let result = responseResult.results.first else { | ||
let errorMessage = "Could not retrieve details result." | ||
delegate?.searchErrorHappened( | ||
searchError: .internalSearchRequestError(message: errorMessage), | ||
searchEngine: self | ||
) | ||
return | ||
} | ||
delegate?.resultResolved(result: result, searchEngine: self) | ||
|
||
case .failure(let searchError): | ||
eventsManager.reportError(searchError) | ||
delegate?.searchErrorHappened(searchError: searchError, searchEngine: self) | ||
} | ||
} | ||
|
||
private func resolveServerRetrieveResponse(_ coreResponse: CoreSearchResponseProtocol?) -> SearchResult? { | ||
guard let response = preProcessRetrieveResponse(coreResponse) else { | ||
return nil | ||
|
@@ -576,6 +609,27 @@ extension SearchEngine { | |
} | ||
} | ||
|
||
// MARK: - Public Details API | ||
|
||
extension SearchEngine { | ||
/// Retrieve a POI by its Mapbox ID. | ||
/// When you already have a Mapbox ID, you can retrieve the associated POI data directly rather than using | ||
/// ``search(query:options:)`` and ``select(suggestions:)`` | ||
/// For example, to refresh the data for a _cached POI_ , simply query ``retrieve(mapboxID:options:)``, instead of | ||
/// using new `search` and `select` invocations. | ||
/// - Parameters: | ||
/// - mapboxID: The Mapbox ID for a known POI. Mapbox IDs will be returned in Search responses and may be cached. | ||
/// - detailsOptions: Options to configure this query. May be nil. | ||
public func retrieve(mapboxID: String, options detailsOptions: DetailsOptions = DetailsOptions()) { | ||
assert(offlineMode == .disabled) | ||
|
||
let detailsOptions = detailsOptions | ||
engine.retrieveDetails(for: mapboxID, options: detailsOptions.toCore()) { [weak self] serverResponse in | ||
self?.processDetailsResponse(serverResponse, mapboxID: mapboxID) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - IndexableDataResolver | ||
|
||
extension SearchEngine: IndexableDataResolver { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright © 2024 Mapbox. All rights reserved. | ||
|
||
import Foundation | ||
|
||
/// Options to configure a request for ``SearchEngine/retrieve(mapboxID:options:)`` | ||
public struct DetailsOptions: Sendable { | ||
/// Besides the basic metadata attributes, developers can request additional | ||
/// attributes by setting attribute_sets parameter with attribute set values, | ||
/// for example &attribute_sets=basic,photos,visit. | ||
/// The requested metadata will be provided in metadata object in the response. | ||
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 you please add the DocC reference to the mentioned |
||
public var attributeSets: [AttributeSet] | ||
|
||
/// The ISO language code to be returned. If not provided, the default is English. | ||
public var language: String? | ||
|
||
/// The two digit ISO country code (such as 'JP') to request a worldview for the location data, if applicable data | ||
/// is available. | ||
/// This parameters will only be applicable for Boundaries and Places feature types. | ||
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 you please elaborate on what are |
||
public var worldview: String? | ||
|
||
public init(attributeSets: [AttributeSet]? = nil, language: String? = nil, worldview: String? = nil) { | ||
// Always make sure that the basic attribute set is present | ||
var attributeSets: Set<AttributeSet> = Set(attributeSets ?? [.basic]) | ||
attributeSets.insert(.basic) | ||
self.attributeSets = Array(attributeSets) | ||
self.language = language | ||
self.worldview = worldview | ||
} | ||
|
||
func toCore() -> CoreDetailsOptions { | ||
CoreDetailsOptions( | ||
attributeSets: attributeSets.map { NSNumber(value: $0.coreValue.rawValue) }, | ||
language: language, | ||
worldview: worldview | ||
) | ||
} | ||
} |
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.
Is it needed here to return the request id to match the rest of the implementations?