0

I want to get the first 20 data for the first page from the url below. I want to get the next 20 data on the second page. I don't want to get all the data in the first time. I just want to get the first 20 data.

URL:

https://rss.itunes.apple.com/api/v1/us/books/top-free/all/%20100/non-explicit.json

JSON:

class BookProvider {

    // MARK: - Properties
    private let baseUrl = "https://rss.itunes.apple.com/api/v1"

    // MARK: - Life Cycle
    init() { }

    // MARK: Functions
    func loadTopCharts() -> Future<[Book], Error> {
        let urlString = "us/books/top-free/all/100/non-explicit.json"

        if let data = getData(from: urlString) {
            if let songs = jsonDictionary(from: data)?["results"] {
                do {
                    let data = try JSONSerialization.data(withJSONObject: songs, options: .fragmentsAllowed)
                    let bookDecoder = try JSONDecoder().decode([Book].self, from: data)

                    return Future { promixe in
                        promixe(.success(bookDecoder))
                    }
                }catch {
                    debugPrint(error.localizedDescription)
                }
            }
        }
        return Future{ _ in }
    }

    private func getData(from urlString: String) -> Data? {
        guard let url = URL(string: baseUrl + "/" + urlString) else {
            fatalError("Couldn't load URL from provided string.")
        }

        do {
            return try Data(contentsOf: url)
        } catch {
            debugPrint("Couldn't load data from URL:\n\(error)")
            return nil
        }
    }

    private func jsonDictionary(from data: Data) -> [String : Any]? {
        do {
            let dictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
            return (dictionary as? [String : Any])?["feed"] as? [String : Any]
        }catch {
            debugPrint(error.localizedDescription)
            return nil
        }
    }
}
Ufuk Köşker
  • 1,288
  • 8
  • 29
  • 2
    What is your question? – dalton_c Aug 11 '21 at 20:36
  • I don't want to get all the data in the first time. I just want to get the first 20 data. How Can I do this ? – Ufuk Köşker Aug 11 '21 at 20:45
  • Looks like your URL is wrong. Should be https://rss.itunes.apple.com/api/v1/us/books/top-free/all/20/non-explicit.json though I'm not sure how to get the next page. – dalton_c Aug 11 '21 at 20:46
  • 1
    @daltonclaybrook I think he has to manually break it down. There is a limit of 20 calls per minute so it is much better to make a single call and thats probably what they are enforcing. – Leo Dabus Aug 11 '21 at 21:02
  • I don't want to get all the data in the same url. I want to get 20 data per page – Ufuk Köşker Aug 11 '21 at 21:04
  • @UfukKöşker if it is indeed true that Apple has a strict rate-limit on this API like Leo is suggesting, then you're probably better off fetching the full list, then breaking it up into chunks of 20 on the client side. – dalton_c Aug 11 '21 at 21:28
  • "I want to get the first 20 data" That's up to your server. Talk to your server boy or girl. – El Tomato Aug 12 '21 at 00:23

0 Answers0