generated from AND-SOPT-ANDROID/and-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from AND-SOPT-ANDROID/week4
4주차 과제
- Loading branch information
Showing
57 changed files
with
1,278 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
app/src/main/java/org/sopt/and/adapter/ResponseConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.sopt.and.adapter | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.jsonObject | ||
import kotlinx.serialization.serializer | ||
import okhttp3.ResponseBody | ||
import retrofit2.Converter | ||
import retrofit2.Retrofit | ||
import java.lang.reflect.Type | ||
|
||
class ResultConverter<T>( | ||
private val serializer: KSerializer<T>, | ||
private val json: Json | ||
) : Converter<ResponseBody, T> { | ||
override fun convert(responseBody: ResponseBody): T? { | ||
return responseBody.use { | ||
val jsonObject = json.parseToJsonElement(responseBody.string()).jsonObject | ||
val result = jsonObject["result"] ?: return null | ||
json.decodeFromJsonElement(serializer, result) | ||
} | ||
} | ||
} | ||
|
||
class ResultConverterFactory : Converter.Factory() { | ||
|
||
override fun responseBodyConverter( | ||
type: Type, | ||
annotations: Array<out Annotation>, | ||
retrofit: Retrofit | ||
): Converter<ResponseBody, *> { | ||
val json = Json { ignoreUnknownKeys = true } | ||
val resultSerializer = json.serializersModule.serializer(type) | ||
return ResultConverter(resultSerializer, json) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.sopt.and.di | ||
|
||
import com.sopt.data.api.remote.UserApi | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import retrofit2.Retrofit | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object ApiModule { | ||
|
||
@Singleton | ||
@Provides | ||
fun provideUserApi(retrofit: Retrofit): UserApi { | ||
return retrofit.create(UserApi::class.java) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.sopt.and.di | ||
|
||
import com.sopt.data.IODispatcher | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.coroutines.Dispatchers | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object CoroutineModule { | ||
|
||
@IODispatcher | ||
@Singleton | ||
@Provides | ||
fun provideIODispatcher() = Dispatchers.IO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.sopt.and.di | ||
|
||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import com.sopt.domain.exception.NetworkError | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.Interceptor | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import org.sopt.and.BuildConfig | ||
import org.sopt.and.adapter.ResultConverterFactory | ||
import retrofit2.Retrofit | ||
import java.util.concurrent.TimeUnit | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { | ||
|
||
@Singleton | ||
@Provides | ||
fun provideClient( | ||
responseInterceptor: Interceptor | ||
): OkHttpClient { | ||
return OkHttpClient.Builder() | ||
.connectTimeout(10, TimeUnit.SECONDS) | ||
.readTimeout(30, TimeUnit.SECONDS) | ||
.writeTimeout(15, TimeUnit.SECONDS) | ||
.addInterceptor(HttpLoggingInterceptor().apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
}).addInterceptor(responseInterceptor) | ||
.build() | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideResponseInterceptor( | ||
) : Interceptor = Interceptor { chain -> | ||
val response = chain.proceed(chain.request()) | ||
|
||
if (response.isSuccessful.not()) { | ||
val errorBody = response.body?.string() | ||
val errorResponse = try { | ||
Json.decodeFromString<ErrorResponse>(errorBody ?: "") | ||
} catch (e: Exception) { | ||
null | ||
} | ||
|
||
throw NetworkError( | ||
statusCode = response.code, | ||
errorCode = errorResponse?.code?.toInt() ?: -1, | ||
message = response.message, | ||
) | ||
} | ||
return@Interceptor response | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideRetrofit( | ||
client: OkHttpClient | ||
): Retrofit { | ||
val json = Json { ignoreUnknownKeys = true } | ||
return Retrofit.Builder() | ||
.baseUrl(BuildConfig.BASE_URL) | ||
.client(client) | ||
.addConverterFactory(ResultConverterFactory()) | ||
.addConverterFactory(json.asConverterFactory("application/json".toMediaType())) | ||
.build() | ||
} | ||
} | ||
|
||
@Serializable | ||
private data class ErrorResponse( | ||
val code: String | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.sopt.data.api.remote | ||
|
||
import com.sopt.data.dto.user.HobbyDto | ||
import com.sopt.data.dto.user.SignInDto | ||
import com.sopt.data.dto.user.SignUpDto | ||
import com.sopt.data.request.SignInRequest | ||
import com.sopt.data.request.SignUpRequest | ||
import com.sopt.data.request.UpdateProfileRequest | ||
import retrofit2.http.Body | ||
import retrofit2.http.GET | ||
import retrofit2.http.Header | ||
import retrofit2.http.POST | ||
import retrofit2.http.PUT | ||
import retrofit2.http.Path | ||
|
||
interface UserApi { | ||
|
||
@POST("user") | ||
suspend fun signUp(@Body signUpRequest: SignUpRequest): SignUpDto | ||
|
||
@POST("login") | ||
suspend fun signIn(@Body signInRequest: SignInRequest): SignInDto | ||
|
||
@GET("user/my-hobby") | ||
suspend fun fetchMyHobby(@Header("token") token: String): HobbyDto | ||
|
||
@GET("user/{no}/hobby") | ||
suspend fun fetchUserHobby(@Header("token") token: String, @Path("no") no: Int): HobbyDto | ||
|
||
@PUT("user") | ||
suspend fun updateProfile( | ||
@Header("token") token: String, | ||
@Body updateProfileRequest: UpdateProfileRequest | ||
) | ||
} |
24 changes: 24 additions & 0 deletions
24
data/src/main/java/com/sopt/data/datasource/local/TokenLocalDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.sopt.data.datasource.local | ||
|
||
import android.content.SharedPreferences | ||
import com.sopt.data.TokenEncryptedSharedPref | ||
import javax.inject.Inject | ||
|
||
class TokenLocalDataSource @Inject constructor( | ||
@TokenEncryptedSharedPref private val tokenEncryptedSharedPref: SharedPreferences | ||
) { | ||
|
||
fun saveToken(token: String) { | ||
tokenEncryptedSharedPref.edit() | ||
.putString(TOKEN, token) | ||
.apply() | ||
} | ||
|
||
fun getToken(): String { | ||
return tokenEncryptedSharedPref.getString(TOKEN, "").orEmpty() | ||
} | ||
|
||
companion object { | ||
private const val TOKEN = "token" | ||
} | ||
} |
Oops, something went wrong.