Skip to content

Commit

Permalink
Vallum: add method to fetch all surveys (#206)
Browse files Browse the repository at this point in the history
Intended for project Energiepolders Hoeksche Waard because it uses
surveys from multiple projects.
  • Loading branch information
Erikvv authored Jan 14, 2025
1 parent 01b0141 commit 8a41817
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions vallum/src/main/kotlin/Vallum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,40 @@ constructor(
private val baseUrl: String = "https://ztor.zero.zenmo.com",
private val tokenUrl: String = "https://keycloak.zenmo.com/realms/zenmo/protocol/openid-connect/token",
) {
private val client = HttpClient(CIO) {
install(ContentNegotiation) {
json(Json {
ignoreUnknownKeys = true
})
}
}

fun getHessenpoortSurveys(): List<Survey> =
getSurveysByProject("Hessenpoort")

@JvmOverloads
fun getSurveysByProject(project: String, includeInSimulation: Boolean? = true): List<Survey> {
val client = HttpClient(CIO) {
install(ContentNegotiation) {
json(Json {
ignoreUnknownKeys = true
})
fun getSurveysByProject(project: String, includeInSimulation: Boolean? = true): List<Survey> = runBlocking {
val accessToken = getAccessToken(client)
client.get(baseUrl.trimEnd('/') + "/company-surveys") {
parameter("project", project)
parameter("includeInSimulation", includeInSimulation)
headers {
append("Authorization", "Bearer $accessToken")
}
}
}.body<List<Survey>>()
}

return runBlocking {
val accessToken = getAccessToken(client)
client.get(baseUrl.trimEnd('/') + "/company-surveys") {
parameter("project", project)
parameter("includeInSimulation", includeInSimulation)
headers {
append("Authorization", "Bearer $accessToken")
}
}.body<List<Survey>>()
}
/**
* Get all surveys the account has access to and are configured to include in the simulation
*/
fun getAllEnabledSurveys(): List<Survey> = runBlocking {
val accessToken = getAccessToken(client)
client.get(baseUrl.trimEnd('/') + "/company-surveys") {
parameter("includeInSimulation", true)
headers {
append("Authorization", "Bearer $accessToken")
}
}.body<List<Survey>>()
}

private suspend fun getAccessToken(client: HttpClient): String {
Expand Down

0 comments on commit 8a41817

Please sign in to comment.