Skip to content

Commit

Permalink
chore: replace gson with jackson
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph5610 committed Apr 1, 2024
1 parent 2b58079 commit c7c3964
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 38 deletions.
14 changes: 9 additions & 5 deletions api-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
version = "0.10"
version = "0.11"

val retrofitVersion = "2.10.0"
val jacksonVersion = "2.17.0"

dependencies {
api(platform("com.squareup.okhttp3:okhttp-bom:4.12.0"))
api("com.google.code.gson:gson:2.10.1") // patch for CVE-2022-25647
api("com.squareup.okhttp3:logging-interceptor")
api("com.squareup.okhttp3:okhttp-dnsoverhttps")
api("com.squareup.retrofit2:retrofit:2.10.0")
api("com.squareup.retrofit2:adapter-rxjava3:2.10.0")
api("com.squareup.retrofit2:converter-gson:2.10.0")
api("com.squareup.retrofit2:retrofit:$retrofitVersion")
api("com.squareup.retrofit2:adapter-rxjava3:$retrofitVersion")
api("com.squareup.retrofit2:converter-jackson:$retrofitVersion")
api("com.fasterxml.jackson.core:jackson-core:$jacksonVersion")
api("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

package online.hudacek.fxradio.apiclient

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import online.hudacek.fxradio.apiclient.http.provider.AbstractClientProvider
import online.hudacek.fxradio.apiclient.http.provider.CachingClientProvider
import retrofit2.Retrofit
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.jackson.JacksonConverterFactory

/**
* Provides Retrofit instance for [baseUrl]
Expand All @@ -38,7 +39,7 @@ class ServiceProvider(
val retrofit: Retrofit
get() = Retrofit.Builder()
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(JacksonConverterFactory.create(jacksonObjectMapper))
.baseUrl(baseUrl)
.client(clientProvider.client)
.build()
Expand All @@ -49,4 +50,12 @@ class ServiceProvider(
inline fun <reified T : ApiDefinition> create(): T = retrofit.create(T::class.java)

fun close() = clientProvider.close()

companion object {

/**
* instance of kotlin jackson object mapper
*/
private val jacksonObjectMapper = jacksonObjectMapper()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package online.hudacek.fxradio.apiclient.musicbrainz.model

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class Artist(
val id: String,
val name: String,
val disambiguation: String
val disambiguation: String?
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package online.hudacek.fxradio.apiclient.musicbrainz.model

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class ArtistCredit(
val name: String,
val artist: Artist
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package online.hudacek.fxradio.apiclient.musicbrainz.model

import com.google.gson.annotations.SerializedName
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty

@JsonIgnoreProperties(ignoreUnknown = true)
data class Release(
val id: String,
val score: Int,
val title: String,
val status: String,
@SerializedName("artist-credit") val artistCredit: List<ArtistCredit>
val status: String?,
@JsonProperty("artist-credit") val artistCredit: List<ArtistCredit>
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package online.hudacek.fxradio.apiclient.musicbrainz.model

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class SearchResult(
val created: String,
val count: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

package online.hudacek.fxradio.apiclient.radiobrowser.model

import com.google.gson.annotations.SerializedName
import com.fasterxml.jackson.annotation.JsonProperty

data class AddStationRequest(
val name: String = "",
val url: String = "",
val homepage: String = "",
val favicon: String = "",
@SerializedName("countrycode") val countryCode: String = "",
@JsonProperty("countrycode") val countryCode: String = "",
val country: String = "",
val language: String = "",
val tags: String = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@

package online.hudacek.fxradio.apiclient.radiobrowser.model

import com.google.gson.annotations.SerializedName
import com.fasterxml.jackson.annotation.JsonProperty

data class AdvancedSearchRequest(
val name: String? = null,
val tag: String? = null,
val tagExact: Boolean? = null,
val order: String? = null,
val limit: Int? = null,
@SerializedName("hidebroken") val hideBroken: Boolean = true,
@JsonProperty("hidebroken") val hideBroken: Boolean = true,
val reverse: Boolean? = null,
@SerializedName("has_extended_info") val hasExtendedInfo: Boolean? = null,
@JsonProperty("has_extended_info") val hasExtendedInfo: Boolean? = null,
)

Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

package online.hudacek.fxradio.apiclient.radiobrowser.model

import com.google.gson.annotations.SerializedName
import com.fasterxml.jackson.annotation.JsonProperty
import online.hudacek.fxradio.apiclient.ApiUtils.COUNTRY_IGNORE_LIST

data class Country(
val name: String,
@SerializedName("iso_3166_1") val iso3166: String,
@SerializedName("stationcount") val stationCount: Int
@JsonProperty("iso_3166_1") val iso3166: String,
@JsonProperty("stationcount") val stationCount: Int
) {

// Don't use stationCount when comparing this data class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

package online.hudacek.fxradio.apiclient.radiobrowser.model

import com.google.gson.annotations.SerializedName
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import online.hudacek.fxradio.apiclient.ApiUtils.COUNTRY_IGNORE_LIST
import java.io.Serializable

Expand All @@ -29,26 +30,27 @@ private const val INVALID_UUID = "0"
/**
* Station data class
*/
@JsonIgnoreProperties(ignoreUnknown = true)
data class Station(
@SerializedName("stationuuid") val uuid: String,
@JsonProperty("stationuuid") val uuid: String,
val name: String,
@SerializedName("url_resolved") val urlResolved: String,
@JsonProperty("url_resolved") val urlResolved: String,
val homepage: String,
val favicon: String?,
val tags: String = "",
val country: String = "",
@SerializedName("countrycode") val countryCode: String = "",
@JsonProperty("countrycode") val countryCode: String = "",
val state: String = "",
val language: String = "",
val codec: String = "",
val bitrate: Int = 0,
val votes: Int = 0,
@SerializedName("geo_lat") val geoLat: Double = 0.0,
@SerializedName("geo_long") val geoLong: Double = 0.0,
@SerializedName("clicktrend") val clickTrend: Int = 0,
@SerializedName("clickcount") val clickCount: Int = 0,
@SerializedName("languagecodes") val languageCodes: String = "",
@SerializedName("has_extended_info") val hasExtendedInfo: Boolean = false
@JsonProperty("geo_lat") val geoLat: Double = 0.0,
@JsonProperty("geo_long") val geoLong: Double = 0.0,
@JsonProperty("clicktrend") val clickTrend: Int = 0,
@JsonProperty("clickcount") val clickCount: Int = 0,
@JsonProperty("languagecodes") val languageCodes: String = "",
@JsonProperty("has_extended_info") val hasExtendedInfo: Boolean = false
) : Serializable {

fun isValid() = uuid != INVALID_UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@

package online.hudacek.fxradio.apiclient.radiobrowser.model

import com.google.gson.annotations.SerializedName
import com.fasterxml.jackson.annotation.JsonProperty

data class StatsResponse(
@SerializedName("supported_version") val supportedVersion: String = "",
@SerializedName("software_version") val softwareVersion: String = "",
@JsonProperty("supported_version") val supportedVersion: String = "",
@JsonProperty("software_version") val softwareVersion: String = "",
val status: String = "",
val stations: String = "",
@SerializedName("stations_broken") val stationsBroken: String = "",
@JsonProperty("stations_broken") val stationsBroken: String = "",
val tags: String = "",
@SerializedName("clicks_last_hour") val clicksLastHour: Int = 0,
@SerializedName("clicks_last_day") val clicksLastDay: Int = 0,
@JsonProperty("clicks_last_hour") val clicksLastHour: Int = 0,
@JsonProperty("clicks_last_day") val clicksLastDay: Int = 0,
val languages: Int = 0,
val countries: Int = 0
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package online.hudacek.fxradio.apiclient.radiobrowser.model

import com.google.gson.annotations.SerializedName
import com.fasterxml.jackson.annotation.JsonProperty

data class Tag(val name: String, @SerializedName("stationcount") val stationCount: Int)
data class Tag(val name: String, @JsonProperty("stationcount") val stationCount: Int)
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ class GetCoverArtUseCase : BaseUseCase<String, Maybe<Response>>() {
private val musicBrainzApi: MusicBrainzApi by lazy { MusicBrainzApiProvider.provide() }

override fun execute(input: String): Maybe<Response> = musicBrainzApi.getReleases(input)
// Take only the most probable candidate for cover art
.flatMapMaybe { maybeOfNullable(it.releases.firstOrNull { r -> r.score >= SCORE_THRESHOLD }) }
.map {
// Take only the most probable candidate for cover art
val coverUrl = Config.API.COVER_ART_URL + it.id + ART_PATH
ReleaseWithCoverArt(coverUrl, it)
}
.doOnSuccess { logger.debug { "Requesting CoverArt: ${it.coverArtUrl}" } }
.doOnError { logger.error(it) { "Failed to retrieve CoverArt!" } }
.flatMapSingle { Single.fromCallable { HttpClient.request(it.coverArtUrl) } }
.compose(applySchedulersMaybe())
.onErrorComplete()
Expand Down

0 comments on commit c7c3964

Please sign in to comment.