From 17520e81a4637789535202bdffedf1a958bcf8f0 Mon Sep 17 00:00:00 2001 From: Ronald Mannak Date: Thu, 18 Dec 2025 11:47:00 -0800 Subject: [PATCH 1/2] Simplifiy pagination --- README.md | 13 ++++++++----- .../HuggingFace/Hub/HubClient+Pagination.swift | 17 +++++++++++++++++ Sources/HuggingFace/Shared/HTTPClient.swift | 14 +++++++++++++- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 Sources/HuggingFace/Hub/HubClient+Pagination.swift diff --git a/README.md b/README.md index e8d3dd1..135844b 100644 --- a/README.md +++ b/README.md @@ -600,13 +600,16 @@ _ = try await client.upsertSpaceVariable( The API automatically handles pagination using `Link` headers: ```swift -let page1 = try await client.listModels(limit: 100) -print("Page 1: \(page1.items.count) models") +var page = try await client.listModels(limit: 100) +print("Page 1: \(page.items.count) models") -// Get next page if available -if let nextURL = page1.nextURL { - // Make a request to nextURL to get the next page +while page.nextURL != nil { + guard let next = try await client.nextPage(of: page) else { break } + page = next + + print("Page: \(page.items.count) models") } + ``` #### Error Handling diff --git a/Sources/HuggingFace/Hub/HubClient+Pagination.swift b/Sources/HuggingFace/Hub/HubClient+Pagination.swift new file mode 100644 index 0000000..9a1bc54 --- /dev/null +++ b/Sources/HuggingFace/Hub/HubClient+Pagination.swift @@ -0,0 +1,17 @@ +// +// File 2.swift +// swift-huggingface +// +// Created by Ronald Mannak on 12/17/25. +// + +import Foundation + +extension HubClient { + public func nextPage( + of page: PaginatedResponse + ) async throws -> PaginatedResponse? { + guard let next = page.nextURL else { return nil } + return try await httpClient.fetchPaginated(.get, url: next) + } +} diff --git a/Sources/HuggingFace/Shared/HTTPClient.swift b/Sources/HuggingFace/Shared/HTTPClient.swift index 2a3ca5e..63e8667 100644 --- a/Sources/HuggingFace/Shared/HTTPClient.swift +++ b/Sources/HuggingFace/Shared/HTTPClient.swift @@ -113,7 +113,19 @@ final class HTTPClient: @unchecked Sendable { params: [String: Value]? = nil, headers: [String: String]? = nil ) async throws -> PaginatedResponse { - let request = try await createRequest(method, path, params: params, headers: headers) + guard let url = URL(string: path, relativeTo: host) else { + throw HTTPClientError.unexpectedError("Invalid URL") + } + return try await fetchPaginated(method, url: url, params: params, headers: headers) + } + + func fetchPaginated( + _ method: HTTPMethod, + url: URL, + params: [String: Value]? = nil, + headers: [String: String]? = nil + ) async throws -> PaginatedResponse { + let request = try await createRequest(method, url: url, params: params, headers: headers) let (data, response) = try await session.data(for: request) let httpResponse = try validateResponse(response, data: data) From e82f01774a18b87a62b233dfa09b94261b2cd799 Mon Sep 17 00:00:00 2001 From: Ronald Mannak Date: Thu, 18 Dec 2025 12:00:19 -0800 Subject: [PATCH 2/2] Remove newline --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 135844b..f6dae69 100644 --- a/README.md +++ b/README.md @@ -609,7 +609,6 @@ while page.nextURL != nil { print("Page: \(page.items.count) models") } - ``` #### Error Handling