-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
week7: 서버통신 심화 및 권한 요청, 저장소 활용 #17
base: develope-xml
Are you sure you want to change the base?
Changes from all commits
c2ea51f
96e545a
f7a3761
797b79a
ec96591
ed9ea27
4f7cf39
9e6b3db
d239f96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.sopt.now | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.appcompat.app.AppCompatDelegate | ||
import dagger.hilt.android.HiltAndroidApp | ||
|
||
@HiltAndroidApp | ||
class NowSopt : Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
appContext = applicationContext | ||
|
||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) | ||
} | ||
|
||
companion object { | ||
lateinit var appContext: Context | ||
private set | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.sopt.now.data.di | ||
|
||
import android.util.Log | ||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import com.sopt.now.data.service.AuthService | ||
import com.sopt.now.data.service.FriendService | ||
import com.sopt.now.data.service.UserService | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.Interceptor | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Retrofit | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { | ||
private const val CONTENT_TYPE = "application/json" | ||
private val json: Json = | ||
Json { | ||
ignoreUnknownKeys = true | ||
} | ||
|
||
private fun getLogOkHttpClient(): Interceptor { | ||
val loggingInterceptor = | ||
HttpLoggingInterceptor { message -> | ||
Log.d("Retrofit2", "CONNECTION INFO -> $message") | ||
} | ||
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | ||
return loggingInterceptor | ||
} | ||
|
||
private val okHttpClient = | ||
OkHttpClient.Builder() | ||
.addInterceptor(getLogOkHttpClient()) | ||
.build() | ||
|
||
@Singleton | ||
@Provides | ||
fun provideRetrofit(): Retrofit.Builder { | ||
return Retrofit.Builder() | ||
.client(okHttpClient) | ||
.addConverterFactory(json.asConverterFactory(CONTENT_TYPE.toMediaType())) | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideAuthService( | ||
retrofitBuilder: Retrofit.Builder, | ||
@AuthBaseUrl url: String, | ||
): AuthService { | ||
return retrofitBuilder.baseUrl(url).build().create(AuthService::class.java) | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideUserService( | ||
retrofitBuilder: Retrofit.Builder, | ||
@AuthBaseUrl url: String, | ||
): UserService { | ||
return retrofitBuilder.baseUrl(url).build().create(UserService::class.java) | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideFriendService( | ||
retrofitBuilder: Retrofit.Builder, | ||
@FriendBaseUrl url: String, | ||
): FriendService { | ||
return retrofitBuilder.baseUrl(url).build().create(FriendService::class.java) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.sopt.now.data.di | ||
|
||
import com.sopt.now.BuildConfig | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Qualifier | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object UrlModule { | ||
@AuthBaseUrl | ||
@Provides | ||
fun provideAuthBaseUrl(): String = BuildConfig.AUTH_BASE_URL | ||
|
||
@FriendBaseUrl | ||
@Provides | ||
fun provideFriendBaseUrl(): String = BuildConfig.FRIEND_BASE_URL | ||
} | ||
|
||
@Retention(AnnotationRetention.BINARY) | ||
@Qualifier | ||
annotation class AuthBaseUrl | ||
|
||
@Retention(AnnotationRetention.BINARY) | ||
@Qualifier | ||
annotation class FriendBaseUrl |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.sopt.now.data.repository | ||
|
||
import com.sopt.now.data.request.RequestSignInDto | ||
import com.sopt.now.data.request.RequestSignUpDto | ||
import com.sopt.now.data.response.ResponseSignInDto | ||
import com.sopt.now.data.response.ResponseSignUpDto | ||
import com.sopt.now.data.service.AuthService | ||
import retrofit2.Response | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class AuthRepository | ||
@Inject | ||
constructor( | ||
private val authService: AuthService, | ||
) { | ||
suspend fun signUp(request: RequestSignUpDto): Response<ResponseSignUpDto> { | ||
return authService.signUp(request) | ||
} | ||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [2] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와,, Response 와 ResponseSignUpDto 가 그런 차이가 있었는지는 생각도 못했습니다. 그럼 ResponseSignUpDto 는 서버나 로컬디비, 파이어베이스 어디서 받아와도 가능한 형태겠군요. 레포지토리 패턴에 대해 다시 한번 생각해보겠습니다. 너무 도움 많이 됐습니다! 감사합니다! |
||
|
||
suspend fun signIn(request: RequestSignInDto): Response<ResponseSignInDto> { | ||
return authService.signIn(request) | ||
} | ||
Comment on lines
+18
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 잘 따라하셨는데요? ㅋㅋㅋㅋㅋㅋ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 과찬이십니다 허허 이제 이해하는 일만 남았네요 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.sopt.now.data.repository | ||
|
||
import com.sopt.now.data.response.ResponseFriendsInfoDto | ||
import com.sopt.now.data.service.FriendService | ||
import retrofit2.Response | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class FriendRepository | ||
@Inject | ||
constructor( | ||
private val friendService: FriendService, | ||
) { | ||
suspend fun getFriendsInfo(i: Int): Response<ResponseFriendsInfoDto> { | ||
return friendService.getFriendsInfo(2) | ||
} | ||
} | ||
SYAAINN marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.sopt.now.data.repository | ||
|
||
import com.sopt.now.data.response.ResponseUserInfoDto | ||
import com.sopt.now.data.service.UserService | ||
import retrofit2.Response | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class UserRepository | ||
@Inject | ||
constructor( | ||
private val userService: UserService, | ||
) { | ||
suspend fun getUserInfo(memberId: Int): Response<ResponseUserInfoDto> { | ||
return userService.getUserInfo(memberId) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.sopt.now.data.service | ||
|
||
import com.sopt.now.data.request.RequestSignInDto | ||
import com.sopt.now.data.request.RequestSignUpDto | ||
import com.sopt.now.data.response.ResponseSignInDto | ||
import com.sopt.now.data.response.ResponseSignUpDto | ||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
|
||
interface AuthService { | ||
@POST("member/join") | ||
suspend fun signUp( | ||
@Body request: RequestSignUpDto, | ||
): Response<ResponseSignUpDto> | ||
|
||
@POST("member/login") | ||
suspend fun signIn( | ||
@Body request: RequestSignInDto, | ||
): Response<ResponseSignInDto> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.sopt.now.data.service | ||
|
||
import com.sopt.now.data.response.ResponseFriendsInfoDto | ||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
import retrofit2.http.Query | ||
|
||
interface FriendService { | ||
@GET("/api/users") | ||
suspend fun getFriendsInfo( | ||
@Query("page") page: Int, | ||
): Response<ResponseFriendsInfoDto> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.sopt.now.data.service | ||
|
||
import com.sopt.now.data.response.ResponseUserInfoDto | ||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
import retrofit2.http.Header | ||
|
||
interface UserService { | ||
@GET("member/info") | ||
suspend fun getUserInfo( | ||
@Header("memberId") memberId: Int, | ||
): Response<ResponseUserInfoDto> | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제 꿀팁공유 열심히 따라해보셨군요~~ 민재님을 위해 썼었답니다 ㅋㅋㅋㅋㅋ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
없었으면 이번 과제 못했습니다.. 압도적 감사