feat: Deserialize directly to OffsetDateTime, not String

This commit is contained in:
Peter Vacho 2024-12-30 00:41:07 +01:00
parent e81d2b9a03
commit 415a8e60ff
Signed by: school
GPG key ID: 8CFC3837052871B4
3 changed files with 15 additions and 4 deletions

View file

@ -49,6 +49,7 @@ dependencies {
implementation("androidx.core:core-splashscreen:1.0.1")
implementation("com.squareup.retrofit2:retrofit:2.11.0")
implementation("com.squareup.retrofit2:converter-gson:2.11.0")
implementation("com.fatboyindustrial.gson-javatime-serialisers:gson-javatime-serialisers:1.1.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.1")
implementation("com.jakewharton.threetenabp:threetenabp:1.4.4")
}

View file

@ -2,6 +2,7 @@ package com.p_vacho.neat_calendar.api
import android.content.Context
import android.util.Log
import com.google.gson.GsonBuilder
import com.p_vacho.neat_calendar.api.services.AuthService
import com.p_vacho.neat_calendar.api.services.GeneralService
import okhttp3.OkHttpClient
@ -11,6 +12,8 @@ import com.p_vacho.neat_calendar.api.interceptors.AuthInterceptor
import okhttp3.ResponseBody
import retrofit2.HttpException
import java.io.IOException
import java.time.OffsetDateTime
import com.fatboyindustrial.gsonjavatime.OffsetDateTimeConverter
object RetrofitClient {
private const val DEFAULT_BASE_URL = "http://10.0.2.2:8000"
@ -62,10 +65,15 @@ object RetrofitClient {
okHttpClient.addInterceptor(ApiReachabilityInterceptor(appContext))
}
okHttpClient.addInterceptor(AuthInterceptor(appContext))
val gson = GsonBuilder()
.registerTypeAdapter(OffsetDateTime::class.java, OffsetDateTimeConverter())
.create()
return Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
}

View file

@ -1,5 +1,7 @@
package com.p_vacho.neat_calendar.api.models
import java.time.OffsetDateTime
data class LoginResponse(
val access_token: String,
val expires_in: Int,
@ -20,8 +22,8 @@ data class SessionResponse(
val id: String,
val session_type: String, // "refresh" or "access"
val parent_session_id: String?,
val expires_at: String, // ISO 8601
val created_at: String, // ISO 8601
val expires_at: OffsetDateTime,
val created_at: OffsetDateTime,
val revoked: Boolean,
)
@ -35,5 +37,5 @@ data class RegisterResponse(
val user_id: String,
val username: String,
val email: String,
val created_at: String, // ISO 8601
val created_at: OffsetDateTime,
)