Skip to content

Commit

Permalink
Merge pull request #1093 from paug/more-robust-to-sessionize-errors
Browse files Browse the repository at this point in the history
Be more robust to sessionize errors
  • Loading branch information
martinbonnin authored Apr 24, 2024
2 parents 16c4713 + 895c5e3 commit 5190a21
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RootMutation {
"bookmarks require authentication"
}

if (Sessionize.data.get().sessions.none { it.id == sessionId }) {
if (Sessionize.data().sessions.none { it.id == sessionId }) {
throw Error("Cannot add bookmark for inexisting session '$sessionId'")
}

Expand All @@ -49,7 +49,7 @@ class RootMutation {
}

private fun Set<String>.toBookmarkConnection(): BookmarkConnection {
return BookmarkConnection(Sessionize.data.get().sessions.filter { this.contains(it.id) })
return BookmarkConnection(Sessionize.data().sessions.filter { this.contains(it.id) })
}

fun removeBookmark(executionContext: ExecutionContext, sessionId: String): BookmarkConnection {
Expand Down Expand Up @@ -100,15 +100,15 @@ class RootMutation {
@GraphQLQueryRoot
class RootQuery {
fun rooms(): List<Room> {
return Sessionize.data.get().rooms
return Sessionize.data().rooms
}

fun sessions(
@GraphQLDefault("10") first: Int,
@GraphQLDefault("null") after: String?,
@GraphQLDefault("{field: STARTS_AT, direction: ASCENDING}") orderBy: SessionOrderBy
): SessionConnection {
var sessions = Sessionize.data.get().sessions
var sessions = Sessionize.data().sessions

when (orderBy.direction) {
OrderByDirection.ASCENDING -> {
Expand Down Expand Up @@ -156,40 +156,40 @@ class RootQuery {

@Deprecated("Use speakersPage instead")
fun speakers(): List<Speaker> {
return Sessionize.data.get().speakers
return Sessionize.data().speakers
}

fun speakersPage(
@GraphQLDefault("10") first: Int,
@GraphQLDefault("null") after: String?,
): SpeakerConnection {
return Sessionize.data.get().speakers.splice(first, after) { nodes, pageInfo ->
return Sessionize.data().speakers.splice(first, after) { nodes, pageInfo ->
SpeakerConnection(nodes, pageInfo)
}
}

fun speaker(id: String): Speaker {
return Sessionize.data.get().speakers.first { it.id == id }
return Sessionize.data().speakers.first { it.id == id }
}

fun venue(id: String): Venue {
return Sessionize.data.get().venues.first { it.id == id}
return Sessionize.data().venues.first { it.id == id}
}

fun venues(): List<Venue> {
return Sessionize.data.get().venues
return Sessionize.data().venues
}

fun partnerGroups(): List<PartnerGroup> {
return Sessionize.data.get().partnerGroups
return Sessionize.data().partnerGroups
}

fun session(id: String): Session {
return Sessionize.data.get().sessions.first { it.id == id }
return Sessionize.data().sessions.first { it.id == id }
}

fun config(): Conference {
return Sessionize.data.get().conference
return Sessionize.data().conference
}

fun bookmarkConnection(executionContext: ExecutionContext): BookmarkConnection {
Expand All @@ -208,11 +208,11 @@ class RootQuery {
Entity.newBuilder(entity)
}

return BookmarkConnection(Sessionize.data.get().sessions.filter { entity.names.contains(it.id) })
return BookmarkConnection(Sessionize.data().sessions.filter { entity.names.contains(it.id) })
}

fun conferences(@GraphQLDefault("null") orderBy: ConferenceOrderBy?): List<Conference> {
return listOf(Sessionize.data.get().conference)
return listOf(Sessionize.data().conference)
}
}

Expand Down Expand Up @@ -320,21 +320,21 @@ data class Session(
val links: List<Link>
): Node {
fun speakers(): List<Speaker> {
return Sessionize.data.get().speakers.filter { speakerIds.contains(it.id) }
return Sessionize.data().speakers.filter { speakerIds.contains(it.id) }
}

fun room(): Room? {
val roomId = roomIds.firstOrNull()
if (roomId == null) {
return null
}
return Sessionize.data.get().rooms.firstOrNull {
return Sessionize.data().rooms.firstOrNull {
it.id == roomId
}
}

fun rooms(): List<Room> {
return Sessionize.data.get().rooms.filter {
return Sessionize.data().rooms.filter {
roomIds.contains(it.id)
}
}
Expand All @@ -359,7 +359,7 @@ data class Speaker(
private val sessionIds: List<String>,
): Node {
fun sessions(): List<Session> {
return Sessionize.data.get().sessions.filter {
return Sessionize.data().sessions.filter {
sessionIds.contains(it.id)
}
}
Expand Down
26 changes: 21 additions & 5 deletions service/src/commonMain/kotlin/androidmakers/service/sessionize.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package androidmakers.service

import androidmakers.service.graphql.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
Expand Down Expand Up @@ -71,10 +72,20 @@ suspend fun getUrl(url: String): String {
suspend fun getJsonUrl(url: String) = Json.parseToJsonElement(getUrl(url)).toAny()

object Sessionize {
val data = AtomicReference<SessionizeData>()
private val data = AtomicReference<SessionizeData>()

suspend fun importAndroidMakers2024() {
val data = getData(
internal fun data(): SessionizeData {
val d = data.get()
if (d == null) {
runBlocking {
importAndroidMakers2024()
}
}
return data.get()
}

private suspend fun getData(): SessionizeData {
return getData(
url = "https://sessionize.com/api/v2/ok1n6jgj/view/All",
gridSmartUrl = "https://sessionize.com/api/v2/ok1n6jgj/view/GridSmart",
config = Conference(
Expand Down Expand Up @@ -233,8 +244,13 @@ object Sessionize {
)
)
)

this.data.set(data)
}
suspend fun importAndroidMakers2024() {
try {
this.data.set(getData())
} catch (e: Exception) {
e.printStackTrace()
}
}


Expand Down

0 comments on commit 5190a21

Please sign in to comment.