Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define GMS api service #36

Merged
merged 6 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.omh.android.storage.api.drive.gms

import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential
import com.omh.android.auth.api.OmhAuthClient
import com.omh.android.auth.api.models.OmhAuthStatusCodes
import com.omh.android.storage.api.OmhStorageClient
import com.omh.android.storage.api.domain.model.OmhStorageException
import com.omh.android.storage.api.domain.repository.OmhFileRepository
import com.omh.android.storage.api.drive.gms.data.GoogleDriveApiProvider
import com.omh.android.storage.api.drive.gms.data.GoogleDriveApiService
import com.omh.android.storage.api.drive.gms.data.repository.GmsFileRepositoryImpl
import com.omh.android.storage.api.drive.gms.data.source.GmsFileRemoteDataSourceImpl
import kotlin.jvm.Throws

internal class OmhGmsStorageClientImpl private constructor(
authClient: OmhAuthClient
Expand All @@ -20,7 +23,14 @@ internal class OmhGmsStorageClientImpl private constructor(

@Throws(OmhStorageException::class)
override fun getRepository(): OmhFileRepository {
val dataSource = GmsFileRemoteDataSourceImpl()
val credentials = authClient.getCredentials() as? GoogleAccountCredential
?: throw OmhStorageException.InvalidCredentialsException(OmhAuthStatusCodes.SIGN_IN_FAILED)

val apiProvider = GoogleDriveApiProvider.getInstance(credentials)

val apiService = GoogleDriveApiService(apiProvider)

val dataSource = GmsFileRemoteDataSourceImpl(apiService)

return GmsFileRepositoryImpl(dataSource)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.omh.android.storage.api.drive.gms.data

import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.http.javanet.NetHttpTransport
import com.google.api.client.json.JsonFactory
import com.google.api.client.json.gson.GsonFactory
import com.google.api.services.drive.Drive
import com.omh.android.storage.api.drive.gms.BuildConfig

internal class GoogleDriveApiProvider private constructor(private val credential: GoogleAccountCredential) {

companion object {
private var instance: GoogleDriveApiProvider? = null

internal fun getInstance(omhCredentials: GoogleAccountCredential): GoogleDriveApiProvider {
if (instance == null) {
instance = GoogleDriveApiProvider(omhCredentials)
}

return instance!!
}
}

private val applicationName = BuildConfig.LIBRARY_PACKAGE_NAME
HectorNarvaez marked this conversation as resolved.
Show resolved Hide resolved
private val jsonFactory: JsonFactory = GsonFactory.getDefaultInstance()
private val httpTransport: NetHttpTransport = GoogleNetHttpTransport.newTrustedTransport()

val googleDriveApiService: Drive by lazy { initDriveService() }

private fun initDriveService(): Drive = Drive
.Builder(
httpTransport,
jsonFactory,
credential
).setApplicationName(applicationName)
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.omh.android.storage.api.drive.gms.data

import com.google.api.services.drive.Drive

internal class GoogleDriveApiService(private val apiProvider: GoogleDriveApiProvider) {

fun getFilesList(): Drive.Files.List = apiProvider
.googleDriveApiService
.files()
.list()
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.omh.android.storage.api.drive.gms.data.source

import com.google.api.services.drive.model.File
import com.google.api.services.drive.model.FileList
import com.omh.android.storage.api.data.source.OmhFileRemoteDataSource
import com.omh.android.storage.api.domain.model.OmhFile
import com.omh.android.storage.api.drive.gms.data.GoogleDriveApiService
import com.omh.android.storage.api.drive.gms.data.source.mapper.toOmhFile

internal class GmsFileRemoteDataSourceImpl : OmhFileRemoteDataSource {
internal class GmsFileRemoteDataSourceImpl(private val apiService: GoogleDriveApiService) :
OmhFileRemoteDataSource {

override fun getFilesList(parentId: String): List<OmhFile> {
return emptyList()
val googleJsonFileList: FileList = apiService.getFilesList().execute()
val googleFileList: List<File> = googleJsonFileList.files.toList()
return googleFileList.mapNotNull { googleFile -> googleFile.toOmhFile() }
}

override fun createFile(name: String, mimeType: String, parentId: String?): OmhFile? {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.omh.android.storage.api.drive.gms.data.source.mapper

import com.google.api.services.drive.model.File
import com.omh.android.storage.api.domain.model.OmhFile

@SuppressWarnings("ComplexCondition")
fun File.toOmhFile(): OmhFile? {
val formattedModifiedTime = modifiedTime.toStringRfc3339()

if (mimeType == null || id == null || name == null || formattedModifiedTime == null) {
return null
}

val parentId = if (parents.isNullOrEmpty()) {
""
} else {
parents[0]
}

return OmhFile(
mimeType,
id,
name,
formattedModifiedTime,
parentId
)
}