Skip to content

Commit

Permalink
feat(backend/pagination): Add support for pagination on starred repos
Browse files Browse the repository at this point in the history
  • Loading branch information
Grohden committed Aug 5, 2020
1 parent e36787b commit 6d0202f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
12 changes: 12 additions & 0 deletions backend/src/com/grohden/repotagger/api/ApiUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ fun ApplicationCall.requireIntParam(key: String): Int {
}
}

/**
* Returns a int param or null, if the param is not a valid
* it throws [BadRequest]
*/
fun ApplicationCall.intParamOrNull(key: String): Int? {
return try {
parameters[key]?.toInt()
} catch (error: NumberFormatException) {
throw BadRequest("Arg $key is invalid")
}
}

/**
* Requires a parameter to be non null and int, otherwise
* throws [BadRequest]
Expand Down
3 changes: 2 additions & 1 deletion backend/src/com/grohden/repotagger/api/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ fun Route.repository(
* return s a list of [SimpleRepository]
*/
get("/starred") {
val page = call.intParamOrNull("page")
val session = call.requireSession()
// FIXME: this needs a cache
val starred = githubClient.userStarred(session.token)
val starred = githubClient.userStarred(session.token, page)
val list = starred.map { githubRepo ->
SimpleRepository(
githubId = githubRepo.id,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/com/grohden/repotagger/github/api/GithubClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class GithubClient(private val client: HttpClient) {
*
* https://docs.github.com/en/rest/reference/activity#list-repositories-starred-by-the-authenticated-user
*/
suspend fun userStarred(token: String): GithubRepositories = client.get(
urlString = "$API_BASE/user/starred"
suspend fun userStarred(token: String, page: Int?): GithubRepositories = client.get(
urlString = "$API_BASE/user/starred?page=${page ?: 1}"
) {
withV3Accept()
withToken(token)
Expand Down

0 comments on commit 6d0202f

Please sign in to comment.