Skip to content

Commit

Permalink
Implemented profile pictures to roles
Browse files Browse the repository at this point in the history
  • Loading branch information
developerfromjokela committed Sep 19, 2022
1 parent d842fc6 commit 9f9f209
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ publishing {
from(components["java"])
groupId = "org.openwilma"
artifactId = "kotlin"
version = "0.9.4-BETA"
version = "0.9.5-BETA"
}
}
}
Expand All @@ -30,6 +30,7 @@ dependencies {
implementation("org.json:json:20220320")
implementation("org.jsoup:jsoup:1.15.3")
implementation("com.helger:ph-css:6.5.0")
implementation("commons-io:commons-io:2.11.0")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.0")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.9.0")
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/openwilma/kotlin/classes/user/WilmaRole.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ data class WilmaRole(
@field:SerializedName("primusId") var primusId: Int,
@field:SerializedName("slug") var slug: String?,
@field:SerializedName("formKey") var formKey: String?,
@field:SerializedName("schools") var schools: List<WilmaSchool>
@field:SerializedName("schools") var schools: List<WilmaSchool>,
var profilePicture: String? = null
)
45 changes: 43 additions & 2 deletions src/main/java/org/openwilma/kotlin/methods/Profile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.openwilma.kotlin.methods

import com.google.gson.reflect.TypeToken
import okhttp3.Response
import org.apache.commons.io.IOUtils
import org.openwilma.kotlin.classes.WilmaSession
import org.openwilma.kotlin.classes.errors.Error
import org.openwilma.kotlin.classes.responses.WilmaAPIResponse
Expand All @@ -11,10 +12,12 @@ import org.openwilma.kotlin.clients.WilmaHttpClient
import org.openwilma.kotlin.enums.UserType
import org.openwilma.kotlin.parsers.WilmaJSONParser
import org.openwilma.kotlin.utils.URLUtils
import java.util.*
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine


public suspend fun getUserAccount(wilmaSession: WilmaSession): WilmaAPIResponse<WilmaAccountInfo> {
return suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
Expand All @@ -34,7 +37,7 @@ public suspend fun getUserAccount(wilmaSession: WilmaSession): WilmaAPIResponse<


public suspend fun getRoles(wilmaSession: WilmaSession): WilmaAPIResponse<List<WilmaRole>> {
return suspendCoroutine {
val roles: WilmaAPIResponse<List<WilmaRole>> = suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "api/v1/accounts/me/roles", requireRole = false), object : WilmaHttpClient.HttpClientInterface {
override fun onResponse(response: String, status: Int) {
Expand All @@ -48,14 +51,50 @@ public suspend fun getRoles(wilmaSession: WilmaSession): WilmaAPIResponse<List<W
}
})
}
roles.payload?.let {it.forEach {role -> role.profilePicture = getRolePFP(wilmaSession, role) }}
return roles
}

public suspend fun getActiveUserFormKey(wilmaSession: WilmaSession, roleRequired: Boolean = false): String? {
return getActiveRole(wilmaSession, roleRequired)?.formKey
}

public suspend fun getActiveRole(wilmaSession: WilmaSession, roleRequired: Boolean = false): WilmaRole? {
private suspend fun getRolePFP(wilmaSession: WilmaSession, role: WilmaRole, noException: Boolean =false): String {
return suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRawRequest(URLUtils.buildUrlWithRole(wilmaSession, role, "profiles/photo"), object : WilmaHttpClient.HttpClientInterface {
override fun onResponse(response: String, status: Int) {}

override fun onRawResponse(response: Response) {
if (response.code == 200) {
val inputStream = response.body?.byteStream()
try {
it.resume(Base64.getEncoder().encodeToString(IOUtils.toByteArray((inputStream))))
} catch (e: Exception) {
if (noException) {
it.resume("")
return
}
it.resumeWithException(e)
}
return
}
it.resume("")
}

override fun onFailed(error: Error) {
if (noException) {
it.resume("")
return
}
it.resumeWithException(error)
}
})
}
}

public suspend fun getActiveRole(wilmaSession: WilmaSession, roleRequired: Boolean = false): WilmaRole? {
val role: WilmaRole? = suspendCoroutine {
val httpClient = WilmaHttpClient(wilmaSession)
httpClient.getRequest(URLUtils.buildUrl(wilmaSession, "api/v1/accounts/me/roles", requireRole = roleRequired), object : WilmaHttpClient.HttpClientInterface {
override fun onResponse(response: String, status: Int) {
Expand All @@ -79,4 +118,6 @@ public suspend fun getActiveRole(wilmaSession: WilmaSession, roleRequired: Boole
}
})
}
role?.let { it.profilePicture = getRolePFP(wilmaSession, it, true) }
return role
}
11 changes: 11 additions & 0 deletions src/main/java/org/openwilma/kotlin/utils/URLUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.openwilma.kotlin.utils

import org.openwilma.kotlin.classes.WilmaServer
import org.openwilma.kotlin.classes.WilmaSession
import org.openwilma.kotlin.classes.user.WilmaRole
import org.openwilma.kotlin.config.Config
import java.net.URI

Expand All @@ -23,6 +24,16 @@ object URLUtils {
return serverUrl
}

fun buildUrlWithRole(wilmaSession: WilmaSession, role: WilmaRole?, path: String): String {
var serverUrl = wilmaSession.wilmaServer.serverURL
var slug = role?.slug ?: ""
if (slug.isNotEmpty() && slug[0] == '/') {
slug = slug.drop(1)+"/"
}
serverUrl = if (serverUrl.endsWith("/")) "$serverUrl$slug$path" else "$serverUrl/$slug$path"
return serverUrl
}

fun buildUrlWithJsonFormat(wilmaServer: WilmaServer, path: String): String {
var serverUrl = wilmaServer.serverURL
serverUrl = if (serverUrl.endsWith("/")) serverUrl + path else "$serverUrl/$path"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.text.SimpleDateFormat

class ParseUserAccountJSON {

private val role = WilmaRole(name = "Heikki Hypoteesi", type = UserType.TEACHER, primusId = 118,
private val role = WilmaRole(name = "Heikki Hypoteesi", type = UserType.TEACHER, primusId = 118,
formKey = "teacher:118:df15a7dec0f0e75190e4cb1ba343d271", slug = "/!01118", schools = listOf(
WilmaSchool(name = "Esimerkkilän peruskoulu (0-9)", id = 12, features = listOf("school_common"))
))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openwilma.kotlin.methods

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.openwilma.kotlin.OpenWilma
Expand Down Expand Up @@ -35,7 +36,7 @@ internal class AuthenticationKtTest {
fun getRoles(): Unit = runBlocking {
val wilmaSession = OpenWilma.signInToWilma(wilmaServer, "ope", "ope")
val roles = OpenWilma.roles(wilmaSession)
println(roles)
println(GsonBuilder().setPrettyPrinting().create().toJson(roles))
assert(roles.payload?.isNotEmpty() ?: false)
}
}

0 comments on commit 9f9f209

Please sign in to comment.