Skip to content

Commit

Permalink
👕 refactor moshi util #32
Browse files Browse the repository at this point in the history
  • Loading branch information
theapache64 committed Nov 27, 2024
1 parent aa1eb68 commit f3c9dd8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
kotlin.code.style=official
# Versions
kotlin_version=1.7.21
retrosheet_version=2.0.1
retrosheet_version=2.0.2
retrofit_version=2.9.0
moshi_version=1.14.0
coroutines_version=1.6.4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.github.theapache64.retrosheet.data.SheetErrorJsonAdapter
import com.github.theapache64.retrosheet.utils.CsvConverter
import com.github.theapache64.retrosheet.utils.JsonValidator
import com.github.theapache64.retrosheet.utils.KeyValueUtils
import com.github.theapache64.retrosheet.utils.MoshiUtils
import com.github.theapache64.retrosheet.utils.SheetUtils
import com.squareup.moshi.Moshi
import java.lang.reflect.Method
import java.lang.reflect.ParameterizedType
import java.net.HttpURLConnection
Expand All @@ -32,9 +32,18 @@ class RetrosheetInterceptor
private constructor(
val isLoggingEnabled: Boolean = false,
private val sheets: Map<String, Map<String, String>>,
val forms: Map<String, String>
val forms: Map<String, String>,
internal val moshi: Moshi
) : Interceptor {

private val sheetErrorJsonAdapter by lazy {
SheetErrorJsonAdapter(moshi)
}

private val apiErrorJsonAdapter by lazy {
ApiErrorJsonAdapter(moshi)
}

companion object {
private val TAG = RetrosheetInterceptor::class.java.simpleName
private const val URL_START = "https://docs.google.com/spreadsheets/d"
Expand All @@ -48,14 +57,6 @@ private constructor(
"https://docs\\.google\\.com/spreadsheets/d/(?<docId>.+)/(?<params>.+)".toRegex()
}

private val sheetErrorJsonAdapter by lazy {
SheetErrorJsonAdapter(MoshiUtils.moshi)
}

private val apiErrorJsonAdapter by lazy {
ApiErrorJsonAdapter(MoshiUtils.moshi)
}

private fun isReturnTypeList(request: Request): Boolean {
val method = request.tag(Invocation::class.java)?.method() ?: return false
// Trying to find return type using reflection
Expand Down Expand Up @@ -115,12 +116,14 @@ private constructor(
private val sheets = mutableMapOf<String, Map<String, String>>()
private val forms = mutableMapOf<String, String>()
private var isLoggingEnabled: Boolean = false
private var moshi = Moshi.Builder().build()

fun build(): RetrosheetInterceptor {
return RetrosheetInterceptor(
isLoggingEnabled,
sheets,
forms
forms,
moshi
)
}

Expand All @@ -129,6 +132,11 @@ private constructor(
return this
}

fun setMoshi(moshi: Moshi): Builder {
this.moshi = moshi
return this
}

@Suppress("MemberVisibilityCanBePrivate")
fun addSheet(sheetName: String, columnMap: Map<String, String>): Builder {
ColumnNameVerifier(columnMap.keys).verify()
Expand Down Expand Up @@ -184,7 +192,7 @@ private constructor(
val responseBuilder = response.newBuilder()

// Checking if it's a JSON response. If yes, it's an error else, it's the CSV.
val isSpreadsheetError = JsonValidator.isValidJsonObject(responseBody)
val isSpreadsheetError = JsonValidator.isValidJsonObject(responseBody, moshi)
if (isSpreadsheetError) {
// It's the spreadsheet error. let's parse it.

Expand Down Expand Up @@ -222,7 +230,7 @@ private constructor(
responseBody = KeyValueUtils.transform(responseBody)
}

val csvJson = CsvConverter.convertCsvToJson(responseBody, isReturnTypeList(request))
val csvJson = CsvConverter.convertCsvToJson(responseBody, isReturnTypeList(request), moshi)
if (csvJson != null) {
jsonRoot = csvJson
if (isLoggingEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.github.theapache64.retrosheet.core

import com.github.theapache64.retrosheet.RetrosheetInterceptor
import com.github.theapache64.retrosheet.annotations.Write
import com.github.theapache64.retrosheet.utils.MoshiUtils
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import java.io.IOException
import java.net.HttpURLConnection
Expand All @@ -20,28 +20,31 @@ import retrofit2.Invocation
class GoogleFormHelper(
private val chain: Interceptor.Chain,
private val request: Request,
private val retrosheetInterceptor: RetrosheetInterceptor
private val retrosheetInterceptor: RetrosheetInterceptor,
) {


private val stringMapAdapter by lazy {
val mapType = Types.newParameterizedType(Map::class.java, String::class.java, String::class.java)
retrosheetInterceptor.moshi.adapter<Map<String, String>>(mapType)
}

private val listAdapter by lazy {
val type = Types.newParameterizedType(List::class.java, Object::class.java)
retrosheetInterceptor.moshi.adapter<List<Any>>(type)
}

private val anyAdapter by lazy {
retrosheetInterceptor.moshi.adapter(Any::class.java)
}

companion object {

private const val FORM_DATA_SPLIT_1 = "FB_PUBLIC_LOAD_DATA_"
private const val FORM_DATA_SPLIT_2 = "</script>"

const val SOLUTION_UPDATE = "Please update retrosheet to latest version."

private val stringMapAdapter by lazy {
val mapType = Types.newParameterizedType(Map::class.java, String::class.java, String::class.java)
MoshiUtils.moshi.adapter<Map<String, String>>(mapType)
}

private val listAdapter by lazy {
val type = Types.newParameterizedType(List::class.java, Object::class.java)
MoshiUtils.moshi.adapter<List<Any>>(type)
}

private val anyAdapter by lazy {
MoshiUtils.moshi.adapter(Any::class.java)
}

fun isGoogleFormSubmit(request: Request): Boolean {
val isForm = (request.tag(Invocation::class.java)?.method()?.getAnnotation(Write::class.java) != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.theapache64.retrosheet.utils

import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import de.siegmar.fastcsv.reader.NamedCsvReader

Expand All @@ -9,7 +10,8 @@ import de.siegmar.fastcsv.reader.NamedCsvReader
object CsvConverter {
fun convertCsvToJson(
csvData: String,
isReturnTypeList: Boolean
isReturnTypeList: Boolean,
moshi: Moshi
): String? {
val items = mutableListOf<Map<String, Any?>>()

Expand Down Expand Up @@ -54,13 +56,13 @@ object CsvConverter {
return when {
isReturnTypeList -> {
val type = Types.newParameterizedType(List::class.java, Map::class.java)
val adapter = MoshiUtils.moshi.adapter<List<Map<String, Any?>>>(type)
val adapter = moshi.adapter<List<Map<String, Any?>>>(type)
adapter.toJson(items)
}

items.isNotEmpty() -> {
val type = Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java)
val adapter = MoshiUtils.moshi.adapter<Map<String, Any?>>(type)
val adapter = moshi.adapter<Map<String, Any?>>(type)
adapter.toJson(items.first())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.theapache64.retrosheet.utils

import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi

/**
* Created by theapache64 : Jul 25 Sat,2020 @ 22:00
Expand All @@ -9,9 +10,9 @@ import com.squareup.moshi.JsonClass
class Empty

object JsonValidator {
fun isValidJsonObject(input: String): Boolean {
fun isValidJsonObject(input: String, moshi: Moshi): Boolean {
return try {
EmptyJsonAdapter(MoshiUtils.moshi).apply { fromJson(input) }
EmptyJsonAdapter(moshi).apply { fromJson(input) }
true
} catch (e: Exception) {
false
Expand Down

This file was deleted.

0 comments on commit f3c9dd8

Please sign in to comment.