From bbd21b9ef5f23209f19c950f35d0082bf240d9e9 Mon Sep 17 00:00:00 2001 From: Sharkaboi Date: Sun, 24 Mar 2024 13:17:43 +0530 Subject: [PATCH] Add github authenticated requests --- .../sources/github/GithubService.kt | 8 ++++++++ .../sources/github/GithubTagSource.kt | 11 ++++++++++- .../sharkaboi/appupdatechecker/GithubTest.kt | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/sharkaboi/appupdatechecker/sources/github/GithubService.kt b/library/src/main/java/com/sharkaboi/appupdatechecker/sources/github/GithubService.kt index 54289c0..2f67a81 100644 --- a/library/src/main/java/com/sharkaboi/appupdatechecker/sources/github/GithubService.kt +++ b/library/src/main/java/com/sharkaboi/appupdatechecker/sources/github/GithubService.kt @@ -2,6 +2,7 @@ package com.sharkaboi.appupdatechecker.sources.github import retrofit2.Response import retrofit2.http.GET +import retrofit2.http.Header import retrofit2.http.Path internal interface GithubService { @@ -11,4 +12,11 @@ internal interface GithubService { @Path(GithubConstants.OWNER_PATH_ID) owner: String, @Path(GithubConstants.REPO_PATH_ID) repo: String, ): Response + + @GET(GithubConstants.PATH) + suspend fun getLatestReleaseWithToken( + @Path(GithubConstants.OWNER_PATH_ID) owner: String, + @Path(GithubConstants.REPO_PATH_ID) repo: String, + @Header("Authorization") authHeader: String, + ): Response } diff --git a/library/src/main/java/com/sharkaboi/appupdatechecker/sources/github/GithubTagSource.kt b/library/src/main/java/com/sharkaboi/appupdatechecker/sources/github/GithubTagSource.kt index e6acbef..7fd7cc5 100644 --- a/library/src/main/java/com/sharkaboi/appupdatechecker/sources/github/GithubTagSource.kt +++ b/library/src/main/java/com/sharkaboi/appupdatechecker/sources/github/GithubTagSource.kt @@ -16,6 +16,7 @@ import retrofit2.converter.moshi.MoshiConverterFactory data class GithubTagSource( val ownerUsername: String, val repoName: String, + val bearerToken: String? = null, override val currentVersion: String, override var versionComparator: VersionComparator = DefaultStringVersionComparator ) : AppUpdateCheckerSource() { @@ -36,7 +37,15 @@ data class GithubTagSource( } try { - val response = service.getLatestRelease(owner = ownerUsername, repo = repoName) + val response = if (!bearerToken.isNullOrBlank()) { + service.getLatestReleaseWithToken( + owner = ownerUsername, + repo = repoName, + authHeader = "Bearer $bearerToken" + ) + } else { + service.getLatestRelease(owner = ownerUsername, repo = repoName) + } if (response.code() == 404) { throw PackageNotFoundException("Project not found in github with username $ownerUsername and repo $repoName") diff --git a/library/src/test/java/com/github/sharkaboi/appupdatechecker/GithubTest.kt b/library/src/test/java/com/github/sharkaboi/appupdatechecker/GithubTest.kt index 3f68a5b..67537ed 100644 --- a/library/src/test/java/com/github/sharkaboi/appupdatechecker/GithubTest.kt +++ b/library/src/test/java/com/github/sharkaboi/appupdatechecker/GithubTest.kt @@ -4,6 +4,7 @@ import com.sharkaboi.appupdatechecker.AppUpdateChecker import com.sharkaboi.appupdatechecker.models.InvalidRepositoryNameException import com.sharkaboi.appupdatechecker.models.InvalidUserNameException import com.sharkaboi.appupdatechecker.models.PackageNotFoundException +import com.sharkaboi.appupdatechecker.models.RemoteError import com.sharkaboi.appupdatechecker.models.UpdateResult import com.sharkaboi.appupdatechecker.sources.github.GithubTagSource import kotlinx.coroutines.runBlocking @@ -126,4 +127,22 @@ class GithubTest { println(exception) assertTrue(exception is InvalidRepositoryNameException) } + + @Test + fun `Checker with invalid auth token returns error`() = runBlocking { + val exception = runCatching { + val versionNameChecker = AppUpdateChecker( + source = GithubTagSource( + ownerUsername = ownerUsername, + repoName = repoName, + currentVersion = "v0.0.0", + bearerToken = "asdhaskdhakjhd" + ) + ) + val result = versionNameChecker.checkUpdate() + println(result) + }.exceptionOrNull() + println(exception) + assertTrue(exception is RemoteError) + } }