ru.pocketbyte.locolaser.google.utils.GoogleSheetGlobalPool.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resource-googlesheet Show documentation
Show all versions of resource-googlesheet Show documentation
Implementation of source for LocoLaser tool to work with Google Sheets.
package ru.pocketbyte.locolaser.google.utils
import com.google.api.services.sheets.v4.Sheets
import java.io.File
import java.io.IOException
object GoogleSheetGlobalPool {
private val sheetsMap = mutableMapOf()
private const val APPLICATION_NAME = "LocoLaser"
fun getService(sheetId: String, credentialFile: File?): Sheets {
val sheetKey = "$sheetId/${credentialFile?.canonicalPath ?: "null"}"
return sheetsMap[sheetKey] ?: run {
val credentials = if (credentialFile != null)
try {
OAuth2Helper.credentialFromFile(credentialFile)
} catch (e: IOException) {
throw RuntimeException(e)
}
else
try {
OAuth2Helper.getCredential(sheetId)
} catch (e: IOException) {
throw RuntimeException(e)
}
val service = Sheets.Builder(
OAuth2Helper.HTTP_TRANSPORT,
OAuth2Helper.JSON_FACTORY,
credentials
).setApplicationName(APPLICATION_NAME).build()
sheetsMap[sheetKey] = service
service
}
}
}