I'm learning iOS development and currently building an iOS app. Below I try to fetch data from Amadeus's Airport and City Search API. There's no data coming back from any keywords that match to any cities in the US or UK.
https://developers.amadeus.com/self-service/category/air/api-doc/airport-and-city-search
static func getAirports(for searchKeywordForCity: String, completion: @escaping (Result<[Airport], NetworkError>) -> Void) {
let endpoint = Endpoint(scheme: .https, host: baseURLForAmadeus, path: pathForAmadeus, queryItems: [
URLQueryItem(name: typeQueryKey, value: typeQueryValue),
URLQueryItem(name: searchQueryKey, value: searchKeywordForCity),
URLQueryItem(name: sortQueryKey, value: sortQueryValue),
URLQueryItem(name: viewQueryKey, value: viewQueryValue)
])
guard let finalURL = endpoint.url else { return completion(.failure(.invalidURL)) }
print(finalURL)
var request = URLRequest(url: finalURL)
request.httpMethod = "GET"
request.addValue("application/vnd.amadeus+json", forHTTPHeaderField: "content-type")
request.addValue("Authorization", forHTTPHeaderField: "Bearer \(apiKey)")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
completion(.failure(.thrownError(error)))
print("Error in \(#function) : \(error.localizedDescription) \n---\n \(error)")
} else {
guard let data = data else { return completion(.failure(.noData)) }
do {
let topLevelObject = try JSONDecoder().decode(TopLevelObject.self, from: data)
guard let airport = topLevelObject.data else { return completion(.failure(.noData)) }
completion(.success(airport))
} catch {
completion(.failure(.serverError(error)))
print("Error in \(#function) : \(error.localizedDescription) \n---\n \(error)")
}
}
}
task.resume()
}
// Data Model
struct TopLevelObject: Codable {
let data: [Airport]
}
struct Airport: Codable {
let name, detailedName: String
let id: String
let timeZoneOffset, iataCode: String
let geoCode: GeoCode
let address: Address
}
struct Address: Codable {
let cityName, cityCode, countryName, countryCode, regionCode: String
}
struct GeoCode: Codable {
let latitude, longitude: Double
}