Skip to content

Commit

Permalink
Allow changing credentials, bump version: 2.0.0-SNAPSHOT-5
Browse files Browse the repository at this point in the history
  • Loading branch information
thennothinghappened committed Dec 1, 2023
1 parent dc154b5 commit c26011e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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,
Expand All @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
*/
Expand Down
7 changes: 7 additions & 0 deletions shared/src/commonTest/kotlin/org/orca/kotlass/CommonTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c26011e

Please sign in to comment.