Skip to content

Commit

Permalink
tableproviders made configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Gyda Elisa Sæter authored and JegHeterGyda committed Jan 15, 2025
1 parent a6b736e commit 87191b1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 77 deletions.
34 changes: 12 additions & 22 deletions backend/src/main/kotlin/no/bekk/Application.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.bekk

import io.ktor.client.*
import no.bekk.plugins.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
Expand All @@ -22,38 +23,27 @@ private fun loadAppConfig(config: ApplicationConfig) {
}

tables = config.configList("tables").map { table ->
TableInstance(

when (table.propertyOrNull("type")?.getString()) {
"AIRTABLE" -> AirTableInstanceConfig(
id = table.propertyOrNull("id")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"id\""),
type = table.propertyOrNull("type")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"type\""),
accessToken = table.propertyOrNull("accessToken")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"accessToken\""),
baseId = table.propertyOrNull("baseId")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"baseId\""),
tableId = table.propertyOrNull("tableId")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"tableId\""),
viewId = table.propertyOrNull("viewId")?.getString(),
webhookId = table.propertyOrNull("webhookId")?.getString(),
webhookSecret = table.propertyOrNull("webhookSecret")?.getString(),
)
}

"YAML" -> YAMLInstanceConfig(
id = table.propertyOrNull("id")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"id\""),
endpoint = table.propertyOrNull("endpoint")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"baseId\""),
resourcePath = table.propertyOrNull("resourcePath")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"tableId\""),
)
else -> throw IllegalStateException("Illegal type \"type\"")

}


// sikkerhetskontroller = AirTableInstanceConfig(
// accessToken = config.propertyOrNull("airTable.sikkerhetskontroller.accessToken")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"airTable.sikkerhetskontroller.accessToken\""),
// baseId = config.propertyOrNull("airTable.sikkerhetskontroller.baseId")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"airTable.sikkerhetskontroller.baseId\""),
// tableId = config.propertyOrNull("airTable.sikkerhetskontroller.tableId")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"airTable.sikkerhetskontroller.tableId\""),
// viewId = config.propertyOrNull("airTable.sikkerhetskontroller.viewId")?.getString(),
// webhookId = config.propertyOrNull("airTable.sikkerhetskontroller.webhookId")?.getString(),
// webhookSecret = config.propertyOrNull("airTable.sikkerhetskontroller.webhookSecret")?.getString(),
//
// )
// driftskontinuitet = AirTableInstanceConfig(
// accessToken = config.propertyOrNull("airTable.driftskontinuitet.accessToken")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"airTable.driftskontinuitet.accessToken\""),
// baseId = config.propertyOrNull("airTable.driftskontinuitet.baseId")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"airTable.driftskontinuitet.baseId\""),
// tableId = config.propertyOrNull("airTable.driftskontinuitet.tableId")?.getString() ?: throw IllegalStateException("Unable to initialize app config \"airTable.driftskontinuitet.tableId\""),
// viewId = config.propertyOrNull("airTable.driftskontinuitet.viewId")?.getString(),
// webhookId = config.propertyOrNull("airTable.driftskontinuitet.webhookId")?.getString(),
// webhookSecret = config.propertyOrNull("airTable.driftskontinuitet.webhookSecret")?.getString(),
// )
}
}

// MicrosoftGraph config
Expand Down
29 changes: 15 additions & 14 deletions backend/src/main/kotlin/no/bekk/configuration/AppConfig.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package no.bekk.configuration

import io.ktor.client.*

object AppConfig {
lateinit var tables: TableConfig
lateinit var microsoftGraph: MicrosoftGraphConfig
Expand All @@ -12,34 +14,33 @@ object AppConfig {
object TableConfig {
lateinit var airTable: AirTableConfig
lateinit var tables: List<TableInstance>
//lateinit var sikkerhetskontroller: AirTableInstanceConfig
//lateinit var driftskontinuitet: AirTableInstanceConfig
}

object AirTableConfig {
lateinit var baseUrl: String
}


data class TableInstance (
val id: String,
val type: String,
val accessToken: String,
val baseId: String,
val tableId: String,
var viewId: String? = null,
var webhookId: String? = null,
var webhookSecret: String? = null,
)
interface TableInstance {
val id: String
}

data class AirTableInstanceConfig (
override val id: String,
val accessToken: String,
val baseId: String,
val tableId: String,
var viewId: String? = null,
var webhookId: String? = null,
var webhookSecret: String? = null,
)
) : TableInstance

data class YAMLInstanceConfig (
override val id: String,
val endpoint: String? = null,
val resourcePath: String? = null
) : TableInstance



object MicrosoftGraphConfig {
lateinit var baseUrl: String
Expand Down
54 changes: 13 additions & 41 deletions backend/src/main/kotlin/no/bekk/services/TableService.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package no.bekk.services

import io.ktor.client.*
import no.bekk.configuration.AirTableInstanceConfig
import no.bekk.configuration.AppConfig
import no.bekk.configuration.YAMLInstanceConfig
import no.bekk.providers.AirTableProvider
import no.bekk.providers.TableProvider
import no.bekk.providers.YamlProvider
import no.bekk.providers.clients.AirTableClient

object TableService {


private val providers: List<TableProvider> = AppConfig.tables.tables.map { table ->
if (table.type == "AIRTABLE") {
AirTableProvider(
when (table) {
is AirTableInstanceConfig -> AirTableProvider(
id = table.id,
airtableClient = AirTableClient(
table.accessToken
Expand All @@ -21,49 +25,17 @@ object TableService {
webhookId = table.webhookId,
webhookSecret = table.webhookSecret,
)
} else {
throw Exception("Table ${table.type} not found")
is YAMLInstanceConfig -> YamlProvider(
id = table.id,
endpoint = table.endpoint,
resourcePath = table.resourcePath,
)
else -> throw Exception("Valid tabletype not found")

}
}


// private val providers: List<TableProvider> = listOf<TableProvider>(
// AirTableProvider(
// id = "570e9285-3228-4396-b82b-e9752e23cd73",
// airtableClient = AirTableClient(
// AppConfig.tables.sikkerhetskontroller.accessToken
// ),
// baseId = AppConfig.tables.sikkerhetskontroller.baseId,
// tableId = AppConfig.tables.sikkerhetskontroller.tableId,
// viewId = AppConfig.tables.sikkerhetskontroller.viewId,
// webhookId = AppConfig.tables.sikkerhetskontroller.webhookId,
// webhookSecret = AppConfig.tables.sikkerhetskontroller.webhookSecret,
// ),
// AirTableProvider(
// id = "816cc808-9188-44a9-8f4b-5642fc2932c4",
// airtableClient = AirTableClient(
// AppConfig.tables.driftskontinuitet.accessToken
// ),
// baseId = AppConfig.tables.driftskontinuitet.baseId,
// tableId = AppConfig.tables.driftskontinuitet.tableId,
// viewId = AppConfig.tables.driftskontinuitet.viewId,
// webhookId = AppConfig.tables.driftskontinuitet.webhookId,
// webhookSecret = AppConfig.tables.driftskontinuitet.webhookSecret,
// ),
// AirTableProvider(
// id = "test-table",
// airtableClient = AirTableClient(
// System.getenv("AIRTABLE_TEST_TOKEN")
// ),
// baseId = "appJFzRzW8GmaKuz3",
// tableId = "tblbiZLh6qn3k7wdt",
// viewId = "",
// webhookId = "achpY1AcPW93B9ucr",
// webhookSecret = System.getenv("AIRTABLE_WEBHOOK_SECRET")
// ),
// )


fun getTableProvider(tableId: String): TableProvider {
return providers.find { it.id == tableId } ?: throw Exception("Table $tableId not found")
}
Expand Down

0 comments on commit 87191b1

Please sign in to comment.