From c26011e1335efcef8f0c49eeb4327fbbae742faa Mon Sep 17 00:00:00 2001 From: thennothinghappened <35243139+thennothinghappened@users.noreply.github.com> Date: Fri, 1 Dec 2023 19:36:58 +1100 Subject: [PATCH] Allow changing credentials, bump version: `2.0.0-SNAPSHOT-5` --- shared/build.gradle.kts | 2 +- .../orca/kotlass/client/CompassApiClient.kt | 50 ++++++++++++------- .../kotlass/client/requests/IAuthClient.kt | 8 ++- .../kotlin/org/orca/kotlass/CommonTest.kt | 7 +++ 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 9d8904e..1389a68 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -7,7 +7,7 @@ plugins { /* required for maven publication */ group = "org.orca.kotlass" -version = "2.0.0-SNAPSHOT-4" +version = "2.0.0-SNAPSHOT-5" /** * GitHub User ID for publishing to GitHub Packages. diff --git a/shared/src/commonMain/kotlin/org/orca/kotlass/client/CompassApiClient.kt b/shared/src/commonMain/kotlin/org/orca/kotlass/client/CompassApiClient.kt index 396163d..b0aaec3 100644 --- a/shared/src/commonMain/kotlin/org/orca/kotlass/client/CompassApiClient.kt +++ b/shared/src/commonMain/kotlin/org/orca/kotlass/client/CompassApiClient.kt @@ -8,6 +8,8 @@ import io.ktor.client.request.* import io.ktor.http.* import io.ktor.serialization.* import io.ktor.serialization.kotlinx.json.* +import kotlinx.coroutines.cancel +import kotlinx.coroutines.isActive import kotlinx.datetime.LocalDate import kotlinx.serialization.json.Json import org.orca.kotlass.client.requests.* @@ -31,7 +33,7 @@ import org.orca.kotlass.data.user.UserDetails /** * Client for talking to the Compass API! */ -class CompassApiClient(private val credentials: CompassUserCredentials) : +class CompassApiClient(private var credentials: CompassUserCredentials) : ICalendarEventsClient, IActivitiesClient, IGradingSchemesClient, @@ -40,28 +42,40 @@ class CompassApiClient(private val credentials: CompassUserCredentials) : IUsersClient, IAuthClient { - private val client = HttpClient { - defaultRequest { - - url { - host = credentials.domain - protocol = URLProtocol.HTTPS + companion object { + private fun createHttpClient(credentials: CompassUserCredentials): HttpClient { + return HttpClient { + defaultRequest { + + url { + host = credentials.domain + protocol = URLProtocol.HTTPS + } + + header(HttpHeaders.Cookie, credentials.cookie) + contentType(ContentType.Application.Json) + } + + expectSuccess = true + + install(ContentNegotiation) { + json(Json { + prettyPrint = true + isLenient = true + ignoreUnknownKeys = true + }) + } } - - header(HttpHeaders.Cookie, credentials.cookie) - contentType(ContentType.Application.Json) } + } - expectSuccess = true + private var client = createHttpClient(credentials) - install(ContentNegotiation) { - json(Json { - prettyPrint = true - isLenient = true - ignoreUnknownKeys = true + override fun setCredentials(credentials: CompassUserCredentials) { + this.credentials = credentials + client.cancel("Reloading HTTP client: credentials changed!") - }) - } + client = createHttpClient(credentials) } /** diff --git a/shared/src/commonMain/kotlin/org/orca/kotlass/client/requests/IAuthClient.kt b/shared/src/commonMain/kotlin/org/orca/kotlass/client/requests/IAuthClient.kt index 1707d35..7466248 100644 --- a/shared/src/commonMain/kotlin/org/orca/kotlass/client/requests/IAuthClient.kt +++ b/shared/src/commonMain/kotlin/org/orca/kotlass/client/requests/IAuthClient.kt @@ -7,8 +7,14 @@ import org.orca.kotlass.client.CompassUserCredentials * Client for checking authentication status with Compass. */ interface IAuthClient { + + /** + * Set the new credentials for this client. + */ + fun setCredentials(credentials: CompassUserCredentials) + /** - * Return whether we are successfully authenticated with Compass. + * Return whether provided credentials are successfully authenticated with Compass. * * NOTE: this cannot check if the provided [CompassUserCredentials.userId] is valid! */ diff --git a/shared/src/commonTest/kotlin/org/orca/kotlass/CommonTest.kt b/shared/src/commonTest/kotlin/org/orca/kotlass/CommonTest.kt index fab1c43..6f45a53 100644 --- a/shared/src/commonTest/kotlin/org/orca/kotlass/CommonTest.kt +++ b/shared/src/commonTest/kotlin/org/orca/kotlass/CommonTest.kt @@ -40,6 +40,13 @@ class CommonTest { assertSuccess(res) } + @Test + fun `test reloading credentials`(): Unit = runBlocking { + `test authentication`() + client.setCredentials(COMPASS_PRIVATE_TEST_DATA.credentials) + `test authentication`() + } + @Test fun `test getting grading schemes`(): Unit = runBlocking { val res = client.getGradingSchemesForLearningTasks()