Skip to content

Commit

Permalink
Refactor igdb-api to use Dagger Hilt
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Rybitskyi committed Sep 8, 2020
1 parent a3ed96a commit 35240b1
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 86 deletions.
3 changes: 3 additions & 0 deletions igdb-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ dependencies {
implementation(deps.square.moshi)
kapt(deps.square.moshiCodeGenerator)

implementation(deps.google.daggerHilt)
kapt(deps.google.daggerHiltCompiler)

testImplementation(deps.testing.jUnit)
androidTestImplementation(deps.testing.jUnitExt)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
package com.paulrybitskyi.gamedge.igdb.api.datastore

import com.github.michaelbull.result.mapEither
import com.paulrybitskyi.gamedge.data.datastores.GamesRemoteDataStore
import com.paulrybitskyi.gamedge.data.datastores.GamesServerDataStore
import com.paulrybitskyi.gamedge.data.utils.DataCompany
import com.paulrybitskyi.gamedge.data.utils.DataGame
import com.paulrybitskyi.gamedge.data.utils.DataStoreResult
import com.paulrybitskyi.gamedge.igdb.api.IgdbApi
import com.paulrybitskyi.gamedge.igdb.api.utils.ApiGame
import com.paulrybitskyi.gamedge.igdb.api.utils.ApiResult

internal class GamesServerDataStore(
internal class GamesServerDataStoreImpl(
private val igdbApi: IgdbApi,
private val entityMapper: EntityMapper
) : GamesRemoteDataStore {
) : GamesServerDataStore {


override suspend fun searchGames(searchQuery: String, offset: Int, limit: Int): DataStoreResult<List<DataGame>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
* limitations under the License.
*/

package com.paulrybitskyi.gamedge.igdb.api
package com.paulrybitskyi.gamedge.igdb.api.di

import com.paulrybitskyi.gamedge.commons.data.querying.QueryTimestampProvider
import com.paulrybitskyi.gamedge.commons.data.querying.QueryTimestampProviderFactory
import com.paulrybitskyi.gamedge.igdb.api.*
import com.paulrybitskyi.gamedge.igdb.api.Constants
import com.paulrybitskyi.gamedge.igdb.api.IgdbApi
import com.paulrybitskyi.gamedge.igdb.api.IgdbApiImpl
import com.paulrybitskyi.gamedge.igdb.api.adapters.AgeRatingCategoryAdapter
import com.paulrybitskyi.gamedge.igdb.api.adapters.AgeRatingTypeAdapter
import com.paulrybitskyi.gamedge.igdb.api.adapters.WebsiteCategoryAdapter
Expand All @@ -28,66 +31,90 @@ import com.paulrybitskyi.gamedge.igdb.api.utils.AuthorizationInterceptor
import com.paulrybitskyi.gamedge.igdb.api.utils.calladapter.ApiResultCallAdapterFactory
import com.paulrybitskyi.gamedge.igdb.apicalypse.querybuilder.ApicalypseQueryBuilderFactory
import com.paulrybitskyi.gamedge.igdb.apicalypse.serialization.ApicalypseSerializer
import com.paulrybitskyi.gamedge.igdb.apicalypse.serialization.ApicalypseSerializerFactory
import com.squareup.moshi.Moshi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import javax.inject.Provider
import javax.inject.Singleton

internal object IgdbApiFactory {
@InstallIn(ApplicationComponent::class)
@Module
internal object ApiModule {


fun createIgdbApi(): IgdbApi {
@Singleton
@Provides
fun provideIgdbApi(
igdbApiService: IgdbApiService,
igdbApiQueryBuilder: IgdbApiQueryBuilder
): IgdbApi {
return IgdbApiImpl(
igdbApiService = createIgdbApiService(),
igdbApiQueryBuilder = createIgdbApiQueryBuilder()
igdbApiService = igdbApiService,
igdbApiQueryBuilder = igdbApiQueryBuilder
)
}


private fun createIgdbApiService(): IgdbApiService {
return createRetrofit().create(IgdbApiService::class.java)
@Provides
fun provideIgdbApiService(retrofit: Retrofit): IgdbApiService {
return retrofit.create(IgdbApiService::class.java)
}


private fun createRetrofit(): Retrofit {
@Provides
fun provideRetrofit(
okHttpClient: OkHttpClient,
moshi: Moshi
): Retrofit {
return Retrofit.Builder()
.client(createOkHttpClient())
.client(okHttpClient)
.baseUrl(Constants.IGDB_API_BASE_URL + "/")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(createMoshi()))
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(ApiResultCallAdapterFactory())
.build()
}


private fun createOkHttpClient(): OkHttpClient {
@Provides
fun provideOkHttpClient(
authorizationInterceptor: AuthorizationInterceptor,
httpLoggingInterceptor: Provider<HttpLoggingInterceptor>
): OkHttpClient {
return OkHttpClient.Builder()
.apply {
addInterceptor(createAuthorizationInterceptor())
addInterceptor(authorizationInterceptor)

if(BuildConfig.DEBUG) {
addInterceptor(createHttpLoggingInterceptor())
addInterceptor(httpLoggingInterceptor.get())
}
}
.build()
}


private fun createAuthorizationInterceptor(): AuthorizationInterceptor {
@Provides
fun provideAuthorizationInterceptor(): AuthorizationInterceptor {
return AuthorizationInterceptor(BuildConfig.IGDB_API_KEY)
}


private fun createHttpLoggingInterceptor(): HttpLoggingInterceptor {
@Provides
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor {
return HttpLoggingInterceptor()
.apply { level = HttpLoggingInterceptor.Level.BODY }
}


private fun createMoshi(): Moshi {
@Provides
fun provideMoshi(): Moshi {
return Moshi.Builder()
.add(AgeRatingCategoryAdapter())
.add(AgeRatingTypeAdapter())
Expand All @@ -96,27 +123,23 @@ internal object IgdbApiFactory {
}


private fun createIgdbApiQueryBuilder(): IgdbApiQueryBuilder {
@Provides
fun provideIgdbApiQueryBuilder(
apicalypseQueryBuilderFactory: ApicalypseQueryBuilderFactory,
apicalypseSerializer: ApicalypseSerializer,
queryTimestampProvider: QueryTimestampProvider
): IgdbApiQueryBuilder {
return IgdbApiQueryBuilderImpl(
apicalypseQueryBuilderFactory = createApicalypseQueryBuilderFactory(),
apicalypseSerializer = createApicalypseSerializer(),
queryTimestampProvider = createQueryTimestampProvider()
apicalypseQueryBuilderFactory = apicalypseQueryBuilderFactory,
apicalypseSerializer = apicalypseSerializer,
queryTimestampProvider = queryTimestampProvider
)
}


private fun createApicalypseQueryBuilderFactory(): ApicalypseQueryBuilderFactory {
return ApicalypseQueryBuilderFactory()
}


private fun createApicalypseSerializer(): ApicalypseSerializer {
return ApicalypseSerializerFactory().create()
}


private fun createQueryTimestampProvider(): QueryTimestampProvider {
return QueryTimestampProviderFactory.create()
@Provides
fun provideApicalypseQueryBuilderFactory(): ApicalypseQueryBuilderFactory {
return ApicalypseQueryBuilderFactory
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2020 Paul Rybitskyi, [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.paulrybitskyi.gamedge.igdb.api.di

import com.paulrybitskyi.gamedge.data.datastores.GamesServerDataStore
import com.paulrybitskyi.gamedge.igdb.api.IgdbApi
import com.paulrybitskyi.gamedge.igdb.api.datastore.EntityMapper
import com.paulrybitskyi.gamedge.igdb.api.datastore.GamesServerDataStoreImpl
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import javax.inject.Singleton

@InstallIn(ApplicationComponent::class)
@Module
internal object DataStoreModule {


@Singleton
@Provides
fun provideGamesServerDataStore(
igdbApi: IgdbApi,
entityMapper: EntityMapper
): GamesServerDataStore {
return GamesServerDataStoreImpl(
igdbApi = igdbApi,
entityMapper = entityMapper
)
}


@Provides
fun provideEntityMapper(): EntityMapper {
return EntityMapper()
}


}
4 changes: 0 additions & 4 deletions igdb-apicalypse/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@

plugins {
kotlin()
}

dependencies {
implementation(deps.kotlin.stdLib)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.paulrybitskyi.gamedge.igdb.apicalypse.querybuilder

import com.paulrybitskyi.gamedge.igdb.apicalypse.querybuilder.whereclause.WhereClauseBuilderFactory

class ApicalypseQueryBuilderFactory {
object ApicalypseQueryBuilderFactory {


fun newBuilder(): ApicalypseQueryBuilder {
Expand Down

0 comments on commit 35240b1

Please sign in to comment.