Skip to content

Commit 76ed9b5

Browse files
refactor: use AuthInterceptor
1 parent c3daeb4 commit 76ed9b5

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

app/src/main/java/woowacourse/shopping/data/API.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ import woowacourse.shopping.data.cart.service.CartService
1111
import woowacourse.shopping.data.product.service.ProductService
1212

1313
object API {
14-
private val client: OkHttpClient =
14+
private val client: OkHttpClient by lazy {
1515
OkHttpClient
1616
.Builder()
17+
.addAuthInterceptor()
1718
.addHttpLoggingInterceptor()
1819
.build()
20+
}
1921

20-
private val retrofit =
22+
private val retrofit by lazy {
2123
Retrofit
2224
.Builder()
2325
.baseUrl(BuildConfig.BASE_URL)
2426
.client(client)
2527
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
2628
.build()
29+
}
2730

2831
val productService: ProductService = retrofit.create(ProductService::class.java)
2932
val cartService: CartService = retrofit.create(CartService::class.java)
@@ -35,4 +38,6 @@ object API {
3538
if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
3639
},
3740
)
41+
42+
private fun OkHttpClient.Builder.addAuthInterceptor() = addInterceptor(AuthInterceptor())
3843
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package woowacourse.shopping.data
2+
3+
import okhttp3.Interceptor
4+
import okhttp3.Request
5+
import okhttp3.Response
6+
7+
class AuthInterceptor : Interceptor {
8+
override fun intercept(chain: Interceptor.Chain): Response {
9+
val originalRequest = chain.request()
10+
val authToken: String = AuthStorage.authToken ?: return chain.proceed(originalRequest)
11+
12+
val request: Request =
13+
originalRequest
14+
.newBuilder()
15+
.addHeader(
16+
"Authorization",
17+
authToken,
18+
).build()
19+
20+
val response: Response = chain.proceed(request)
21+
return response
22+
}
23+
}

app/src/main/java/woowacourse/shopping/data/AuthStorage.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ object AuthStorage {
3131
get() = storage.getString(KEY_PW, DEFAULT_PW)
3232
set(value) = storage.edit { putString(KEY_PW, value) }
3333

34-
val authorization by lazy {
35-
val valueToEncode = "$id:$pw".toByteArray()
36-
"Basic " + Base64.getEncoder().encodeToString(valueToEncode)
37-
}
34+
val authToken: String?
35+
get() {
36+
if (id == null || pw == null) {
37+
return null
38+
}
39+
40+
val valueToEncode = "$id:$pw".toByteArray()
41+
return BASIC_AUTH_FORMAT.format(Base64.getEncoder().encodeToString(valueToEncode))
42+
}
3843

3944
private const val KEY_ID = "woowacourse.shopping.KEY_ID"
40-
private const val DEFAULT_ID = "jerry8282"
45+
private const val DEFAULT_ID = "giovannijunseokim"
4146
private const val KEY_PW = "woowacourse.shopping.KEY_PW"
4247
private const val DEFAULT_PW = "password"
4348
private const val FILE_NAME = "auth"
49+
private const val BASIC_AUTH_FORMAT = "Basic %s"
4450
}

app/src/main/java/woowacourse/shopping/data/cart/service/CartService.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import retrofit2.Call
44
import retrofit2.http.Body
55
import retrofit2.http.DELETE
66
import retrofit2.http.GET
7-
import retrofit2.http.Header
87
import retrofit2.http.PATCH
98
import retrofit2.http.POST
109
import retrofit2.http.Path
1110
import retrofit2.http.Query
12-
import woowacourse.shopping.data.AuthStorage
1311
import woowacourse.shopping.data.cart.dto.CartItemRequest
1412
import woowacourse.shopping.data.cart.dto.CartQuantityResponse
1513
import woowacourse.shopping.data.cart.dto.CartResponse
@@ -20,35 +18,27 @@ interface CartService {
2018
fun getCart(
2119
@Query("page") page: Int,
2220
@Query("size") size: Int,
23-
@Header("Authorization") header: String = AuthStorage.authorization,
2421
): Call<CartResponse>
2522

2623
@GET("/cart-items")
27-
fun getAllCart(
28-
@Header("Authorization") header: String = AuthStorage.authorization,
29-
): Call<CartResponse>
24+
fun getAllCart(): Call<CartResponse>
3025

3126
@POST("/cart-items")
3227
fun postCartItem(
3328
@Body request: CartRequest,
34-
@Header("Authorization") header: String = AuthStorage.authorization,
3529
): Call<Unit>
3630

3731
@DELETE("/cart-items/{cartItemId}")
3832
fun deleteShoppingCartItem(
3933
@Path("cartItemId") cartItemId: Long,
40-
@Header("Authorization") header: String = AuthStorage.authorization,
4134
): Call<Unit>
4235

4336
@PATCH("/cart-items/{id}")
4437
fun patchCartItemQuantity(
4538
@Path("id") id: Long,
4639
@Body request: CartItemRequest,
47-
@Header("Authorization") header: String = AuthStorage.authorization,
4840
): Call<Unit>
4941

5042
@GET("/cart-items/counts")
51-
fun getCartItemQuantity(
52-
@Header("Authorization") header: String = AuthStorage.authorization,
53-
): Call<CartQuantityResponse>
43+
fun getCartItemQuantity(): Call<CartQuantityResponse>
5444
}

0 commit comments

Comments
 (0)