I'm currently learning Swift and recreating an app as a project. I have this API that I'm trying to hit but it's just not working, sometimes. I put in breakpoints at the completion and sometimes I'll get a hit with response, but a majority of times I won't. Error returns nil btw.
APIService.swift
import Foundation
final class APIService {
init(){}
struct Constants{
static let apiURL = URL(string: "https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets/airbnb-listings/records?limit=100&refine=city%3A%22New%20York%22&refine=room_type%3A%22Entire%20home%2Fapt%22")
}
public func getListings(completion: @escaping (Result<[WindbnbListing], Error>) -> Void) {
guard let url = Constants.apiURL else {
return
}
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data, error == nil else {
if let error {
completion(.failure(error))
}
return
}
do{
let response = try JSONDecoder().decode(WindbnbListingsResponse.self, from: data)
completion(.success(response.results))
}catch{
let json = try? JSONSerialization.jsonObject(with: data)
print(String(describing: json))
completion(.failure(error))
}
}
task.resume()
}
}
WindbnbListingsResponse.swift
import Foundation
struct WindbnbListingsResponse: Codable {
let total_count: Int
let results: [WindbnbListing]
}
WindbnbListing.swift
import Foundation
struct WindbnbListing: Codable, Hashable, Identifiable {
let id: String?
let listing_url: String?
let name: String?
let summary: String?
let space: String?
let description: String?
let house_rules: String?
let thumbnail_url: String?
let medium_url: String?
let xl_picture_url: String?
let neighborhood: String?
let amenities: [String]?
let price: Int?
//Host info
let host_name: String
let host_since: String
let host_thumbnail_url: String
let host_picture_url: String
}
Heres a glimpse of console output when it somehow works...Breakpoints on completion(.success(response.results)) and completion(.failure(error))
Console output:
data Foundation.Data? 598105 bytes some _1 URLResponse? 0x0000600000266900 error Error? nil none completion () -> () 0x00000001023b047c Windbnbpartial apply forwarder for closure #1 (Swift.Result<Swift.Array<Windbnb.WindbnbListing>, Swift.Error>) -> () in Windbnb.WindbnbListingsViewViewModel.fetchListings() -> () at
data Foundation.Data 598105 bytes
response Windbnb.WindbnbListingsResponse
`
As you can see, I get a response working sometimes...could it be an endpoint issue where Im being rate limited? Doubtful because in postman I can keep making requests over and over and over with no hiccups
Answered by @workingdogsupportUkraine !