Skip to content

Commit

Permalink
Configure data layer (#8)
Browse files Browse the repository at this point in the history
* Setup retrofit and refactor on data layer due to architecture changes

* Sample app module is broken. Removed temporally

* Solve comments

* Solve comments

---------

Co-authored-by: Hector Narvaez <hector.narvaez@CSABOGMB042.local>
  • Loading branch information
HectorNarvaez and Hector Narvaez committed May 16, 2023
1 parent 48eabf3 commit ed4c933
Show file tree
Hide file tree
Showing 26 changed files with 236 additions and 177 deletions.
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ rootProject.name = "omh-storage"
include(":storage-api")
include(":storage-api-drive-gms")
include(":storage-api-drive-nongms")
// include(":storage-sample")
// Sample app is currently broken. Removed temporally
//include(":storage-sample")
Original file line number Diff line number Diff line change
@@ -1,26 +1,75 @@
package com.omh.android.storage.api.drive.nongms.data

import com.omh.android.storage.api.drive.nongms.BuildConfig
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory

internal object GoogleRetrofitImpl {
internal class GoogleRetrofitImpl(private val bearerToken: String) {

private val okHttpClient = OkHttpClient.Builder()
.addInterceptor(
HttpLoggingInterceptor().apply {
if (BuildConfig.DEBUG) setLevel(HttpLoggingInterceptor.Level.BODY)
companion object {
private const val HEADER_AUTHORIZATION_NAME = "Authorization"

private var instance: GoogleRetrofitImpl? = null

internal fun getInstance(bearerToken: String): GoogleRetrofitImpl {
if (instance == null) {
instance = GoogleRetrofitImpl(bearerToken)
}

return instance!!
}
}

private var apiService: GoogleStorageApiService? = null

fun getGoogleStorageApiService(): GoogleStorageApiService {
if (apiService != null) {
return apiService!!
}

val retrofit = Retrofit.Builder()
.client(createOkHttpClient())
.addConverterFactory(createConverterFactory())
.baseUrl(BuildConfig.G_STORAGE_URL)
.build()

apiService = retrofit.create(GoogleStorageApiService::class.java)
return apiService!!
}

private fun createOkHttpClient(): OkHttpClient {
val loggingInterceptor = setupLoggingInterceptor()

return OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor { chain ->
val request = setupRequestInterceptor(chain)
chain.proceed(request)
}
.build()
}

private fun setupLoggingInterceptor() = HttpLoggingInterceptor().apply {
setLevel(
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor.Level.BODY
} else {
HttpLoggingInterceptor.Level.NONE
}
)
.build()
}

private val retrofitClient = Retrofit.Builder()
.client(okHttpClient)
.baseUrl(BuildConfig.G_STORAGE_URL)
.addConverterFactory(JacksonConverterFactory.create())
private fun setupRequestInterceptor(chain: Interceptor.Chain) = chain
.request()
.newBuilder()
.addHeader(
HEADER_AUTHORIZATION_NAME,
bearerToken
)
.build()

val googleStorageREST: GoogleStorageREST = retrofitClient.create(GoogleStorageREST::class.java)
private fun createConverterFactory() = JacksonConverterFactory.create()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.omh.android.storage.api.drive.nongms.data

import com.omh.android.storage.api.drive.nongms.data.source.response.FileListRemoteResponse
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

internal interface GoogleStorageApiService {

companion object {
private const val GET_ALL_FILES_AND_FOLDERS_LIST_PARTICLE = "files"

private const val QUERY_Q = "q"
private const val QUERY_FIELDS = "fields"

private const val Q_VALUE = "'root' in parents and trashed = false"
private const val FIELDS_VALUE = "files(id, name, mimeType, modifiedTime)"
}

@GET(GET_ALL_FILES_AND_FOLDERS_LIST_PARTICLE)
fun getRootFilesList(
@Query(QUERY_Q) query: String = Q_VALUE,
@Query(QUERY_FIELDS) fields: String = FIELDS_VALUE
): Call<FileListRemoteResponse>
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.omh.android.storage.api.drive.nongms.data.source

import com.omh.android.storage.api.data.source.remote.FileRemoteDataSource
import com.omh.android.storage.api.domain.model.File
import com.omh.android.storage.api.drive.nongms.data.GoogleRetrofitImpl
import com.omh.android.storage.api.drive.nongms.data.source.mapper.toFileList

internal class FileRemoteDataSource : FileRemoteDataSource {

override fun getRootFilesList(): List<File> {
val response = GoogleRetrofitImpl
.getInstance("")
.getGoogleStorageApiService()
.getRootFilesList()
.execute()

return if (response.isSuccessful) {
response.body()?.toFileList().orEmpty()
} else {
emptyList()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.omh.android.storage.api.drive.nongms.data.source.mapper

import com.omh.android.storage.api.domain.model.File
import com.omh.android.storage.api.drive.nongms.data.source.response.FileListRemoteResponse
import com.omh.android.storage.api.drive.nongms.data.source.response.FileRemoteResponse

@SuppressWarnings("ComplexCondition")
internal fun FileRemoteResponse.toFile(): File? {
if (mimeType == null || id == null || name == null || modifiedTime == null) {
return null
}

return File(
mimeType,
id,
name,
modifiedTime
)
}

internal fun FileListRemoteResponse.toFileList(): List<File> =
files?.mapNotNull { remoteFileModel -> remoteFileModel?.toFile() }.orEmpty()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.omh.android.storage.api.drive.nongms.data.source.response

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty

@JsonIgnoreProperties(ignoreUnknown = true)
internal data class FileListRemoteResponse(
@JsonProperty("files") val files: List<FileRemoteResponse?>?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.omh.android.storage.api.drive.nongms.data.source.response

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty

@JsonIgnoreProperties(ignoreUnknown = true)
internal data class FileRemoteResponse(
@JsonProperty("mimeType")val mimeType: String?,
@JsonProperty("id")val id: String?,
@JsonProperty("name")val name: String?,
@JsonProperty("modifiedTime")val modifiedTime: String?
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.omh.android.storage.api.data.mapper

import com.omh.android.storage.api.domain.model.FileType

internal object FileTypeMapper {

private val MAP_FILE_TYPES: Map<String, FileType> =
FileType.values().associateBy { fileType -> fileType.mimeType }

fun getFileTypeWithMime(mimeType: String): FileType {
return if (MAP_FILE_TYPES.containsKey(mimeType)) {
MAP_FILE_TYPES[mimeType] ?: FileType.OTHER
} else {
FileType.OTHER
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.omh.android.storage.api.data.repository

import com.omh.android.storage.api.data.datasource.network.FilesFoldersNetworkDataSource
import com.omh.android.storage.api.data.mapper.fromFilesFoldersRemoteListToDomain
import com.omh.android.storage.api.data.source.remote.FileRemoteDataSource
import com.omh.android.storage.api.domain.abstraction.FilesFoldersRepository

class FilesFoldersDataRepository(
private val networkDataSource: FilesFoldersNetworkDataSource
private val networkDataSource: FileRemoteDataSource
) : FilesFoldersRepository {
override fun getAll() = fromFilesFoldersRemoteListToDomain(
networkDataSource.getAll()
)

override fun getRootFilesList() = networkDataSource.getRootFilesList()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.omh.android.storage.api.data.datasource.network
package com.omh.android.storage.api.data.source.remote

interface FileNetworkDataSource {
fun create()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.omh.android.storage.api.data.source.remote

import com.omh.android.storage.api.domain.model.File

interface FileRemoteDataSource {

fun getRootFilesList(): List<File>
}
Loading

0 comments on commit ed4c933

Please sign in to comment.