Skip to content

Commit

Permalink
refactor: core:network -> remote:common
Browse files Browse the repository at this point in the history
  • Loading branch information
jinukeu committed Jun 19, 2024
1 parent 9b04af9 commit 60ff079
Show file tree
Hide file tree
Showing 49 changed files with 89 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class RemoteConventionPlugin : Plugin<Project> {

dependencies {
"implementation"(project(":core:model"))
"implementation"(project(":core:network"))
"implementation"(project(":remote:common"))

"implementation"(libs.findBundle("coroutine").get())

Expand Down

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ protobuf = "3.24.4"
espresso-core = "3.5.1"
junit-ktx = "1.1.5"
androidx-test-runner = "1.5.2"
junit = "1.1.5"

[plugins]
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
Expand Down Expand Up @@ -190,6 +191,7 @@ espresso-core = { group = "androidx.test.espresso", name = "espresso-core", vers

protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" }
protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junit" }

[bundles]
firebase = ["firebase-analytics"]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.suwiki.android.library)
Expand All @@ -8,15 +6,15 @@ plugins {
}

android {
namespace = "com.suwiki.core.network"
namespace = "com.suwiki.remote.common"

buildTypes {
getByName("debug") {
buildConfigField("String", "BASE_URL", "\"http://54.180.72.97:8080\"")
buildConfigField("String", "BASE_URL", "String.valueOf(\"http://54.180.72.97:8080\")")
}

getByName("release") {
buildConfigField("String", "BASE_URL", "\"https://api.suwiki.kr\"")
buildConfigField("String", "BASE_URL", "String.valueOf(\"https://api.suwiki.kr\")")
}
}
}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.suwiki.core.network.api
package com.suwiki.remote.common.api

import com.suwiki.core.network.model.TokenResponse
import com.suwiki.core.network.retrofit.ApiResult
import com.suwiki.remote.common.di.AUTH_HEADER
import com.suwiki.remote.common.model.TokenResponse
import com.suwiki.remote.common.retrofit.ApiResult
import retrofit2.http.Header
import retrofit2.http.POST

// TODO : v2 api로 업그레이드 필요.
interface AuthApi {
companion object {
const val USER = "/user"
const val AUTH_HEADER = "Authorization"
}

// Refresh Token
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.suwiki.core.network.authenticator
package com.suwiki.remote.common.authenticator

import com.suwiki.core.model.exception.SuwikiServerError
import com.suwiki.core.network.di.RETROFIT_TAG
import com.suwiki.core.network.repository.AuthRepository
import com.suwiki.core.network.retrofit.Json
import com.suwiki.remote.common.di.AUTH_HEADER
import com.suwiki.remote.common.di.RETROFIT_TAG
import com.suwiki.remote.common.repository.AuthRepository
import com.suwiki.remote.common.retrofit.Json
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
Expand All @@ -23,19 +24,22 @@ internal class TokenAuthenticator @Inject constructor(

mutex.withLock {
val accessToken = authRepository.accessToken.first()
val alreadyRefreshed = response.request.header(AUTH_HEADER)?.contains(accessToken) == false
val alreadyRefreshed =
response.request.header(AUTH_HEADER)?.contains(accessToken) == false
// if request's header's token is different, then that means the access token has already been refreshed
// we return the response with the locally persisted token in the header
if (alreadyRefreshed) {
Timber.tag(RETROFIT_TAG).d("TokenAuthenticator - 이미 토큰이 갱신됨 / 중단된 API 재요청")
Timber.tag(RETROFIT_TAG)
.d("TokenAuthenticator - 이미 토큰이 갱신됨 / 중단된 API 재요청")

return@runBlocking response.request.newBuilder()
.header(AUTH_HEADER, accessToken)
.build()
}

if (authRepository.reissueRefreshToken()) {
Timber.tag(RETROFIT_TAG).d("TokenAuthenticator - 토큰 갱신 성공 / 중단된 API 재요청")
Timber.tag(RETROFIT_TAG)
.d("TokenAuthenticator - 토큰 갱신 성공 / 중단된 API 재요청")
response.request
.newBuilder()
.removeHeader(AUTH_HEADER)
Expand All @@ -46,10 +50,6 @@ internal class TokenAuthenticator @Inject constructor(
}
}
}

companion object {
const val AUTH_HEADER = "Authorization"
}
}

private val okhttp3.Response.isTokenExpired: Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.suwiki.core.network.di
package com.suwiki.remote.common.di

import com.suwiki.core.network.api.AuthApi
import com.suwiki.remote.common.api.AuthApi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -14,7 +14,7 @@ object ApiModule {

@Singleton
@Provides
fun provideAuthApi(@NormalRetrofit retrofit: Retrofit): AuthApi {
fun provideAuthApi(@NoAuthRetrofit retrofit: Retrofit): AuthApi {
return retrofit.create(AuthApi::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.suwiki.remote.common.di

internal const val RETROFIT_TAG = "Retrofit2"
internal const val AUTH_HEADER = "Authorization"
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.suwiki.core.network.di
package com.suwiki.remote.common.di

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import com.suwiki.core.network.BuildConfig
import com.suwiki.core.network.authenticator.TokenAuthenticator
import com.suwiki.core.network.interceptor.AuthenticationInterceptor
import com.suwiki.core.network.retrofit.ResultCallAdapterFactory
import com.suwiki.remote.common.BuildConfig
import com.suwiki.remote.common.authenticator.TokenAuthenticator
import com.suwiki.remote.common.interceptor.AuthenticationInterceptor
import com.suwiki.remote.common.retrofit.ResultCallAdapterFactory
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -25,8 +25,8 @@ object NetworkModule {

@Singleton
@Provides
@NormalOkHttpClient
fun provideNormalHttpClient(
@NoAuthOkHttpClient
fun provideNoAuthHttpClient(
loggingInterceptor: HttpLoggingInterceptor,
): OkHttpClient {
return OkHttpClient.Builder()
Expand Down Expand Up @@ -72,9 +72,9 @@ object NetworkModule {

@Singleton
@Provides
@NormalRetrofit
fun provideNormalRetrofit(
@NormalOkHttpClient okHttpClient: OkHttpClient,
@NoAuthRetrofit
fun provideNoAuthRetrofit(
@NoAuthOkHttpClient okHttpClient: OkHttpClient,
json: Json,
): Retrofit {
return Retrofit.Builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.suwiki.core.network.di
package com.suwiki.remote.common.di

import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
internal annotation class NormalOkHttpClient
internal annotation class NoAuthOkHttpClient

@Qualifier
@Retention(AnnotationRetention.BINARY)
internal annotation class AuthOkHttpClient

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class NormalRetrofit
annotation class NoAuthRetrofit

@Qualifier
@Retention(AnnotationRetention.BINARY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.suwiki.core.network.di
package com.suwiki.remote.common.di

import com.suwiki.core.network.repository.AuthRepository
import com.suwiki.core.network.repository.AuthRepositoryImpl
import com.suwiki.remote.common.repository.AuthRepository
import com.suwiki.remote.common.repository.AuthRepositoryImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.suwiki.core.network.interceptor
package com.suwiki.remote.common.interceptor

import com.suwiki.core.network.authenticator.TokenAuthenticator
import com.suwiki.core.network.di.RETROFIT_TAG
import com.suwiki.core.network.repository.AuthRepository
import com.suwiki.remote.common.di.AUTH_HEADER
import com.suwiki.remote.common.di.RETROFIT_TAG
import com.suwiki.remote.common.repository.AuthRepository
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
Expand All @@ -16,7 +16,7 @@ internal class AuthenticationInterceptor @Inject constructor(
return runBlocking {
val accessToken = authRepository.accessToken.first()
val request = chain.request().newBuilder()
.addHeader(TokenAuthenticator.AUTH_HEADER, accessToken).build()
.addHeader(AUTH_HEADER, accessToken).build()
Timber.tag(RETROFIT_TAG)
.d(
"AuthenticationInterceptor - intercept() called / request header: %s",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.suwiki.core.network.model
package com.suwiki.remote.common.model

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.suwiki.core.network.repository
package com.suwiki.remote.common.repository

import kotlinx.coroutines.flow.Flow

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.suwiki.core.network.repository
package com.suwiki.remote.common.repository

import com.suwiki.core.network.api.AuthApi
import com.suwiki.core.network.di.RETROFIT_TAG
import com.suwiki.core.network.retrofit.onFailure
import com.suwiki.core.network.retrofit.onSuccess
import com.suwiki.core.security.SecurityPreferences
import com.suwiki.remote.common.api.AuthApi
import com.suwiki.remote.common.di.RETROFIT_TAG
import com.suwiki.remote.common.retrofit.onFailure
import com.suwiki.remote.common.retrofit.onSuccess
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import timber.log.Timber
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.suwiki.core.network.retrofit
package com.suwiki.remote.common.retrofit

import com.suwiki.core.model.exception.ForbiddenException
import com.suwiki.core.model.exception.NetworkException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.suwiki.core.network.retrofit
package com.suwiki.remote.common.retrofit

import okhttp3.Request
import okio.Timeout
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.suwiki.core.network.retrofit
package com.suwiki.remote.common.retrofit

import kotlinx.serialization.json.Json

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.suwiki.core.network.retrofit
package com.suwiki.remote.common.retrofit

import retrofit2.Call
import retrofit2.CallAdapter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.suwiki.core.network.retrofit
package com.suwiki.remote.common.retrofit

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.suwiki.remote.lectureevaluation.editor.api

import com.suwiki.core.network.retrofit.ApiResult
import com.suwiki.remote.common.retrofit.ApiResult
import com.suwiki.remote.lectureevaluation.editor.request.PostExamEvaluationRequest
import com.suwiki.remote.lectureevaluation.editor.request.UpdateExamEvaluationRequest
import retrofit2.http.Body
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.suwiki.remote.lectureevaluation.editor.api

import com.suwiki.core.network.retrofit.ApiResult
import com.suwiki.remote.common.retrofit.ApiResult
import com.suwiki.remote.lectureevaluation.editor.request.PostLectureEvaluationRequest
import com.suwiki.remote.lectureevaluation.editor.request.UpdateLectureEvaluationRequest
import retrofit2.http.Body
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.suwiki.remote.lectureevaluation.editor.di

import com.suwiki.core.network.di.AuthRetrofit
import com.suwiki.remote.common.di.AuthRetrofit
import com.suwiki.remote.lectureevaluation.editor.api.ExamEditorApi
import com.suwiki.remote.lectureevaluation.editor.api.LectureEditorApi
import dagger.Module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.suwiki.remote.lectureevaluation.my.api

import com.suwiki.core.network.retrofit.ApiResult
import com.suwiki.remote.common.retrofit.ApiResult
import com.suwiki.remote.lectureevaluation.my.response.DataResponse
import com.suwiki.remote.lectureevaluation.my.response.MyExamEvaluationResponse
import com.suwiki.remote.lectureevaluation.my.response.PurchaseHistoryResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.suwiki.remote.lectureevaluation.my.api

import com.suwiki.core.network.retrofit.ApiResult
import com.suwiki.remote.common.retrofit.ApiResult
import com.suwiki.remote.lectureevaluation.my.response.DataResponse
import com.suwiki.remote.lectureevaluation.my.response.MyLectureEvaluationResponse
import retrofit2.http.GET
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.suwiki.remote.lectureevaluation.my.di

import com.suwiki.core.network.di.AuthRetrofit
import com.suwiki.remote.common.di.AuthRetrofit
import com.suwiki.remote.lectureevaluation.my.api.ExamMyApi
import com.suwiki.remote.lectureevaluation.my.api.LectureMyApi
import dagger.Module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.suwiki.remote.lectureevaluation.viewerreporter.api

import com.suwiki.core.network.retrofit.ApiResult
import com.suwiki.remote.common.retrofit.ApiResult
import com.suwiki.remote.lectureevaluation.viewerreporter.api.LectureReportApi.Companion.REPORT
import com.suwiki.remote.lectureevaluation.viewerreporter.api.LectureReportApi.Companion.USER
import com.suwiki.remote.lectureevaluation.viewerreporter.request.ReportExamRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.suwiki.remote.lectureevaluation.viewerreporter.api

import com.suwiki.core.network.retrofit.ApiResult
import com.suwiki.remote.common.retrofit.ApiResult
import com.suwiki.remote.lectureevaluation.viewerreporter.api.LectureViewerApi.Companion.QUERY_LECTURE_ID
import com.suwiki.remote.lectureevaluation.viewerreporter.response.exam.ExamEvaluationListResponse
import retrofit2.http.GET
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.suwiki.remote.lectureevaluation.viewerreporter.api

import com.suwiki.core.network.retrofit.ApiResult
import com.suwiki.remote.common.retrofit.ApiResult
import com.suwiki.remote.lectureevaluation.viewerreporter.request.ReportLectureRequest
import retrofit2.http.Body
import retrofit2.http.POST
Expand Down
Loading

0 comments on commit 60ff079

Please sign in to comment.