haskell-github / github

The github API for Haskell
https://hackage.haskell.org/package/github
BSD 3-Clause "New" or "Revised" License
411 stars 190 forks source link

Support pagination #503

Open thomasjm opened 8 months ago

thomasjm commented 8 months ago

This PR is an attempt to support GitHub's pagination API.

At present, this package allows you to construct a request with FetchCount, which has constructors FetchAtLeast Word and FetchAll. Both of these cause the library to repeatedly call the GitHub endpoint as many times as necessary to collect the desired number of items.

This obscures the actual pagination API from the user. What if I want to fetch a specific page? And what if I want to access the Link header information returned by the GitHub API, to determine the total number of pages, next/last page links, etc.?

This PR adds a new constructor to FetchCount called FetchPage PageParams. PageParams allows you to specify the page and/or perPage as integers, and you will get exactly the items you're asking for.

This PR also exposes a function parsePageLinks, which you can use to extract the page links from an HTTP response. You can use it like this:

mgr <- newManager tlsManagerSettings
ret <- executeRequestWithMgrAndRes mgr auth $
  issuesForRepoR owner repo mempty (GitHub.FetchPage (GitHub.PageParams (Just 2) (Just 1)))
case ret of
  Left e -> expectationFailure . show $ e
  Right res -> do
    let issues = responseBody res
    let pageLinks = GitHub.parsePageLinks res
    putStrLn ("pageLinks: " ++ show pageLinks)

I'd like to expose this in a nicer way at the higher levels of the API, like the github function. Maybe by providing a githubPaged version that returns the response value and also a PageLinks object. But this is serviceable enough for now. More API design suggestions are welcome!