All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.pocketbyte.locolaser.google.utils.GoogleSheetGlobalPool.kt Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
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
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy