Extract Authorization header to an interceptor

This commit is contained in:
Kirill Kamakin
2022-12-10 08:15:46 +01:00
parent 915fd77daa
commit 54d0c895a9
14 changed files with 103 additions and 106 deletions

View File

@@ -0,0 +1,6 @@
package gq.kirmanak.mealient.datasource
interface AuthenticationProvider {
suspend fun getAuthHeader(): String?
}

View File

@@ -6,6 +6,8 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dagger.multibindings.IntoSet
import gq.kirmanak.mealient.datasource.impl.AuthInterceptor
import gq.kirmanak.mealient.datasource.impl.CacheBuilderImpl
import gq.kirmanak.mealient.datasource.impl.NetworkRequestWrapperImpl
import gq.kirmanak.mealient.datasource.impl.OkHttpBuilderImpl
@@ -18,6 +20,7 @@ import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1Impl
import gq.kirmanak.mealient.datasource.v1.MealieServiceV1
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import retrofit2.Converter
@@ -31,8 +34,6 @@ interface DataSourceModule {
companion object {
const val AUTHORIZATION_HEADER_NAME = "Authorization"
@Provides
@Singleton
fun provideJson(): Json = Json {
@@ -87,4 +88,9 @@ interface DataSourceModule {
@Binds
@Singleton
fun bindNetworkRequestWrapper(networkRequestWrapperImpl: NetworkRequestWrapperImpl): NetworkRequestWrapper
@Binds
@Singleton
@IntoSet
fun bindAuthInterceptor(authInterceptor: AuthInterceptor): Interceptor
}

View File

@@ -0,0 +1,32 @@
package gq.kirmanak.mealient.datasource.impl
import gq.kirmanak.mealient.datasource.AuthenticationProvider
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Response
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.Singleton
@Singleton
class AuthInterceptor @Inject constructor(
private val authenticationProvider: Provider<AuthenticationProvider>,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val token = runBlocking { authenticationProvider.get().getAuthHeader() }
val request = if (token == null) {
chain.request()
} else {
chain.request()
.newBuilder()
.header(HEADER_NAME, token)
.build()
}
return chain.proceed(request)
}
companion object {
private const val HEADER_NAME = "Authorization"
}
}

View File

@@ -10,7 +10,6 @@ interface MealieDataSourceV0 {
suspend fun addRecipe(
baseUrl: String,
token: String?,
recipe: AddRecipeRequestV0,
): String
@@ -29,20 +28,17 @@ interface MealieDataSourceV0 {
suspend fun requestRecipes(
baseUrl: String,
token: String?,
start: Int,
limit: Int,
): List<GetRecipeSummaryResponseV0>
suspend fun requestRecipeInfo(
baseUrl: String,
token: String?,
slug: String,
): GetRecipeResponseV0
suspend fun parseRecipeFromURL(
baseUrl: String,
token: String?,
request: ParseRecipeURLRequestV0,
): String
}

View File

@@ -26,12 +26,11 @@ class MealieDataSourceV0Impl @Inject constructor(
override suspend fun addRecipe(
baseUrl: String,
token: String?,
recipe: AddRecipeRequestV0,
): String = networkRequestWrapper.makeCallAndHandleUnauthorized(
block = { service.addRecipe("$baseUrl/api/recipes/create", token, recipe) },
block = { service.addRecipe("$baseUrl/api/recipes/create", recipe) },
logMethod = { "addRecipe" },
logParameters = { "baseUrl = $baseUrl, token = $token, recipe = $recipe" }
logParameters = { "baseUrl = $baseUrl, recipe = $recipe" }
)
override suspend fun authenticate(
@@ -64,33 +63,30 @@ class MealieDataSourceV0Impl @Inject constructor(
override suspend fun requestRecipes(
baseUrl: String,
token: String?,
start: Int,
limit: Int,
): List<GetRecipeSummaryResponseV0> = networkRequestWrapper.makeCallAndHandleUnauthorized(
block = { service.getRecipeSummary("$baseUrl/api/recipes/summary", token, start, limit) },
block = { service.getRecipeSummary("$baseUrl/api/recipes/summary", start, limit) },
logMethod = { "requestRecipes" },
logParameters = { "baseUrl = $baseUrl, token = $token, start = $start, limit = $limit" }
logParameters = { "baseUrl = $baseUrl, start = $start, limit = $limit" }
)
override suspend fun requestRecipeInfo(
baseUrl: String,
token: String?,
slug: String,
): GetRecipeResponseV0 = networkRequestWrapper.makeCallAndHandleUnauthorized(
block = { service.getRecipe("$baseUrl/api/recipes/$slug", token) },
block = { service.getRecipe("$baseUrl/api/recipes/$slug") },
logMethod = { "requestRecipeInfo" },
logParameters = { "baseUrl = $baseUrl, token = $token, slug = $slug" }
logParameters = { "baseUrl = $baseUrl, slug = $slug" }
)
override suspend fun parseRecipeFromURL(
baseUrl: String,
token: String?,
request: ParseRecipeURLRequestV0
): String = networkRequestWrapper.makeCallAndHandleUnauthorized(
block = { service.createRecipeFromURL("$baseUrl/api/recipes/create-url", token, request) },
block = { service.createRecipeFromURL("$baseUrl/api/recipes/create-url", request) },
logMethod = { "parseRecipeFromURL" },
logParameters = { "baseUrl = $baseUrl, token = $token, request = $request" }
logParameters = { "baseUrl = $baseUrl, request = $request" }
)
}

View File

@@ -1,6 +1,5 @@
package gq.kirmanak.mealient.datasource.v0
import gq.kirmanak.mealient.datasource.DataSourceModule.Companion.AUTHORIZATION_HEADER_NAME
import gq.kirmanak.mealient.datasource.v0.models.*
import retrofit2.http.*
@@ -17,7 +16,6 @@ interface MealieServiceV0 {
@POST
suspend fun addRecipe(
@Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Body addRecipeRequestV0: AddRecipeRequestV0,
): String
@@ -29,7 +27,6 @@ interface MealieServiceV0 {
@GET
suspend fun getRecipeSummary(
@Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Query("start") start: Int,
@Query("limit") limit: Int,
): List<GetRecipeSummaryResponseV0>
@@ -37,13 +34,11 @@ interface MealieServiceV0 {
@GET
suspend fun getRecipe(
@Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
): GetRecipeResponseV0
@POST
suspend fun createRecipeFromURL(
@Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Body request: ParseRecipeURLRequestV0,
): String
}

View File

@@ -11,13 +11,11 @@ interface MealieDataSourceV1 {
suspend fun createRecipe(
baseUrl: String,
token: String?,
recipe: CreateRecipeRequestV1,
): String
suspend fun updateRecipe(
baseUrl: String,
token: String?,
slug: String,
recipe: UpdateRecipeRequestV1,
): GetRecipeResponseV1
@@ -37,20 +35,17 @@ interface MealieDataSourceV1 {
suspend fun requestRecipes(
baseUrl: String,
token: String?,
page: Int,
perPage: Int,
): List<GetRecipeSummaryResponseV1>
suspend fun requestRecipeInfo(
baseUrl: String,
token: String?,
slug: String,
): GetRecipeResponseV1
suspend fun parseRecipeFromURL(
baseUrl: String,
token: String?,
request: ParseRecipeURLRequestV1,
): String
}

View File

@@ -27,23 +27,21 @@ class MealieDataSourceV1Impl @Inject constructor(
override suspend fun createRecipe(
baseUrl: String,
token: String?,
recipe: CreateRecipeRequestV1
): String = networkRequestWrapper.makeCallAndHandleUnauthorized(
block = { service.createRecipe("$baseUrl/api/recipes", token, recipe) },
block = { service.createRecipe("$baseUrl/api/recipes", recipe) },
logMethod = { "createRecipe" },
logParameters = { "baseUrl = $baseUrl, token = $token, recipe = $recipe" }
logParameters = { "baseUrl = $baseUrl, recipe = $recipe" }
)
override suspend fun updateRecipe(
baseUrl: String,
token: String?,
slug: String,
recipe: UpdateRecipeRequestV1
): GetRecipeResponseV1 = networkRequestWrapper.makeCallAndHandleUnauthorized(
block = { service.updateRecipe("$baseUrl/api/recipes/$slug", token, recipe) },
block = { service.updateRecipe("$baseUrl/api/recipes/$slug", recipe) },
logMethod = { "updateRecipe" },
logParameters = { "baseUrl = $baseUrl, token = $token, slug = $slug, recipe = $recipe" }
logParameters = { "baseUrl = $baseUrl, slug = $slug, recipe = $recipe" }
)
override suspend fun authenticate(
@@ -76,33 +74,30 @@ class MealieDataSourceV1Impl @Inject constructor(
override suspend fun requestRecipes(
baseUrl: String,
token: String?,
page: Int,
perPage: Int
): List<GetRecipeSummaryResponseV1> = networkRequestWrapper.makeCallAndHandleUnauthorized(
block = { service.getRecipeSummary("$baseUrl/api/recipes", token, page, perPage) },
block = { service.getRecipeSummary("$baseUrl/api/recipes", page, perPage) },
logMethod = { "requestRecipes" },
logParameters = { "baseUrl = $baseUrl, token = $token, page = $page, perPage = $perPage" }
logParameters = { "baseUrl = $baseUrl, page = $page, perPage = $perPage" }
).items
override suspend fun requestRecipeInfo(
baseUrl: String,
token: String?,
slug: String
): GetRecipeResponseV1 = networkRequestWrapper.makeCallAndHandleUnauthorized(
block = { service.getRecipe("$baseUrl/api/recipes/$slug", token) },
block = { service.getRecipe("$baseUrl/api/recipes/$slug") },
logMethod = { "requestRecipeInfo" },
logParameters = { "baseUrl = $baseUrl, token = $token, slug = $slug" }
logParameters = { "baseUrl = $baseUrl, slug = $slug" }
)
override suspend fun parseRecipeFromURL(
baseUrl: String,
token: String?,
request: ParseRecipeURLRequestV1
): String = networkRequestWrapper.makeCallAndHandleUnauthorized(
block = { service.createRecipeFromURL("$baseUrl/api/recipes/create-url", token, request) },
block = { service.createRecipeFromURL("$baseUrl/api/recipes/create-url", request) },
logMethod = { "parseRecipeFromURL" },
logParameters = { "baseUrl = $baseUrl, token = $token, request = $request" }
logParameters = { "baseUrl = $baseUrl, request = $request" }
)

View File

@@ -1,6 +1,5 @@
package gq.kirmanak.mealient.datasource.v1
import gq.kirmanak.mealient.datasource.DataSourceModule.Companion.AUTHORIZATION_HEADER_NAME
import gq.kirmanak.mealient.datasource.v1.models.*
import retrofit2.http.*
@@ -17,14 +16,12 @@ interface MealieServiceV1 {
@POST
suspend fun createRecipe(
@Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Body addRecipeRequest: CreateRecipeRequestV1,
): String
@PATCH
suspend fun updateRecipe(
@Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Body addRecipeRequest: UpdateRecipeRequestV1,
): GetRecipeResponseV1
@@ -36,7 +33,6 @@ interface MealieServiceV1 {
@GET
suspend fun getRecipeSummary(
@Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Query("page") page: Int,
@Query("perPage") perPage: Int,
): GetRecipesResponseV1
@@ -44,13 +40,11 @@ interface MealieServiceV1 {
@GET
suspend fun getRecipe(
@Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
): GetRecipeResponseV1
@POST
suspend fun createRecipeFromURL(
@Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Body request: ParseRecipeURLRequestV1,
): String
}