Skip to content

Commit

Permalink
Fix ErrorResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Dec 29, 2024
1 parent 74acf1b commit 18a0d37
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
11 changes: 7 additions & 4 deletions Sources/AppleMapsKit/AppleMapsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct AppleMapsClient: Sendable {

private let decoder = JSONDecoder()

/// Initializes a new `AppleMapsClient` instance.
/// Initializes a new ``AppleMapsClient`` instance.
///
/// > Note: The Maps access token is valid for 30 minutes.
///
Expand Down Expand Up @@ -119,7 +119,7 @@ public struct AppleMapsClient: Sendable {
///
/// - Returns: Returns a ``MapRegion`` that describes a region that encloses the results, and an array of ``SearchResponse`` objects that describes the results of the search.
public func search(
for place: String,
for place: String?,
excludePoiCategories: [PoiCategory]? = nil,
includePoiCategories: [PoiCategory]? = nil,
limitToCountries: [String]? = nil,
Expand All @@ -136,7 +136,10 @@ public struct AppleMapsClient: Sendable {
) async throws -> SearchResponse {
var url = URL(string: "\(Self.apiServer)/v1/search")!

var queries: [URLQueryItem] = [URLQueryItem(name: "q", value: place)]
var queries: [URLQueryItem] = []
if let place {
queries.append(URLQueryItem(name: "q", value: place))
}
if let excludePoiCategories {
queries.append(
URLQueryItem(name: "excludePoiCategories", value: excludePoiCategories.map { $0.rawValue }.joined(separator: ","))
Expand Down Expand Up @@ -573,7 +576,7 @@ extension AppleMapsClient {
if response.status == .ok {
return try await response.body.collect(upTo: 1024 * 1024)
} else {
throw try await self.decoder.decode(ErrorResponse.self, from: response.body.collect(upTo: 1024 * 1024))
throw try await self.decoder.decode(ErrorResponseJSON.self, from: response.body.collect(upTo: 1024 * 1024)).error
}
}
}
2 changes: 1 addition & 1 deletion Sources/AppleMapsKit/AppleMapsKitError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct AppleMapsKitError: Error, Sendable, Equatable {
private init(_ base: Base) {
self.base = base
}

/// No places were found for the given query.
public static let noPlacesFound = Self(.noPlacesFound)
/// The search result type is invalid.
Expand Down
2 changes: 1 addition & 1 deletion Sources/AppleMapsKit/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension AuthClient {
if response.status == .ok {
return try await JSONDecoder().decode(TokenResponse.self, from: response.body.collect(upTo: 1024 * 1024))
} else {
throw try await JSONDecoder().decode(ErrorResponse.self, from: response.body.collect(upTo: 1024 * 1024))
throw try await JSONDecoder().decode(ErrorResponseJSON.self, from: response.body.collect(upTo: 1024 * 1024)).error
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions Sources/AppleMapsKit/DTOs/ErrorResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ extension ErrorResponse: CustomStringConvertible {
public var description: String {
var result = #"AppleMapsError(message: \#(self.message ?? "nil")"#

if let details {
result.append(", details: \(details)")
if let details, !details.isEmpty {
result.append(", details: [")
result.append(details.joined(separator: ", "))
result.append("]")
}

result.append(")")

return result
}
}

struct ErrorResponseJSON: Codable {
let error: ErrorResponse
}
20 changes: 14 additions & 6 deletions Tests/AppleMapsKitTests/AppleMapsKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Testing
struct AppleMapsKitTests {
var client: AppleMapsClient
// TODO: Replace with `false` when you have valid credentials.
var credentialsAreInvalid = true
let credentialsAreInvalid = true

init() async throws {
// TODO: Replace the following values with valid ones.
Expand Down Expand Up @@ -100,16 +100,24 @@ struct AppleMapsKitTests {
}

@Test("Search with Page Token") func searchWithPageToken() async throws {
await withKnownIssue {
try await withKnownIssue {
let searchResponse = try await client.search(
for: "eiffel tower",
resultTypeFilter: [.pointOfInterest, .physicalFeature, .poi, .address],
lang: "en-US",
enablePagination: true,
pageToken: "test"
enablePagination: true
)
let results = try #require(searchResponse.results)
#expect(!results.isEmpty)

let nextPageToken = try #require(searchResponse.paginationInfo?.nextPageToken)

let nextSearchResponse = try await client.search(
for: nil,
pageToken: nextPageToken
)
let nextResults = try #require(nextSearchResponse.results)
#expect(!nextResults.isEmpty)
} when: {
credentialsAreInvalid
}
}

Expand Down

0 comments on commit 18a0d37

Please sign in to comment.