nerdishbynature / octokit.swift

A Swift API Client for GitHub and GitHub Enterprise
MIT License
488 stars 126 forks source link

Shouldn't we handle paging? #188

Open Nikoloutsos opened 4 days ago

Nikoloutsos commented 4 days ago

Hello 👋

Recently was in need to fetch some data from GitHub API and I decided to use this library instead of creating my own API calls manually. Then I realized that there is no easy way to get all contents by handling paging with running the following example.

let issues = try await Octokit(config).issues(owner: "freeCodeCamp", repository: "freeCodeCamp")
// returns 100 issues while it should return 188.

I think it would be nice enhancement for this library to give the option to the developer to choose if he wants to get all content.

What do you say?

Nikoloutsos commented 4 days ago

Just saw we give the ability to add page on query params. But I think the developer experience would be much better if we do the following:

Usually I am doing kinda something like this to get all contents.

// Do while loop until a page returns no issues.
var issues = [Issue]()
var starting_page = 1
while(true) {
let issuesFromPage =  try await Octokit(config).issues(owner: "freeCodeCamp", repository: "freeCodeCamp", page: starting_page)
issues.append(issuesFromPage)
starting_page += 1
if issuesFromPage.isEmpty { break }
}

instead this could be encapsulated under an enumeration:

issues.append(try await Octokit(config).issues(owner: "freeCodeCamp", repository: "freeCodeCamp", paging_strategy: .allContent))

// or if you want only the first page
issues.append(try await Octokit(config).issues(owner: "freeCodeCamp", repository: "freeCodeCamp", paging_strategy: .page(number: 1, max_items:100)))

The enum would look like this.

enum PagingStrategy {
    case .allContent
    case .page(number: Int, max_items: Int)
}