Skip to content

Commit

Permalink
Add github authenticated requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharkaboi committed Mar 24, 2024
1 parent 8acb76a commit bbd21b9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -11,4 +12,11 @@ internal interface GithubService {
@Path(GithubConstants.OWNER_PATH_ID) owner: String,
@Path(GithubConstants.REPO_PATH_ID) repo: String,
): Response<GithubResponse>

@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<GithubResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = DefaultStringVersionComparator
) : AppUpdateCheckerSource<String>() {
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit bbd21b9

Please sign in to comment.