Skip to content

Commit

Permalink
[feat/#10] response body가 없는 경우 Response 객체를 이용하여 서버통신 상태를 검사합니다.
Browse files Browse the repository at this point in the history
  • Loading branch information
SYAAINN committed Dec 4, 2024
1 parent 0d19161 commit 189f674
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package org.sopt.and.data.remote.datasource
import org.sopt.and.data.remote.model.base.ApiResponse
import org.sopt.and.data.remote.model.request.UserInfoUpdateRequestDto
import org.sopt.and.data.remote.model.response.HobbyResponseDto
import retrofit2.Response

interface UserRemoteDataSource {
suspend fun getMyHobby(token: String): ApiResponse<HobbyResponseDto>
suspend fun getOthersHobby(token: String, userNo: Int): ApiResponse<HobbyResponseDto>
suspend fun updateUserInfo(token: String, userInfoUpdateRequestDto: UserInfoUpdateRequestDto): ApiResponse<Unit>
suspend fun updateUserInfo(token: String, userInfoUpdateRequestDto: UserInfoUpdateRequestDto): Response<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.sopt.and.data.remote.model.base.ApiResponse
import org.sopt.and.data.remote.model.request.UserInfoUpdateRequestDto
import org.sopt.and.data.remote.model.response.HobbyResponseDto
import org.sopt.and.data.remote.service.UserService
import retrofit2.Response
import javax.inject.Inject

class UserRemoteDataSourceImpl @Inject constructor(
Expand All @@ -20,6 +21,6 @@ class UserRemoteDataSourceImpl @Inject constructor(
override suspend fun updateUserInfo(
token: String,
userInfoUpdateRequestDto: UserInfoUpdateRequestDto
): ApiResponse<Unit> =
): Response<Unit> =
userService.updateUserInfo(token = token, userInfoUpdateRequestDto = userInfoUpdateRequestDto)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import org.sopt.and.data.remote.model.request.LoginRequestDto
import org.sopt.and.data.remote.model.request.UserRegistrationRequestDto
import org.sopt.and.data.remote.model.response.LoginResponseDto
import org.sopt.and.data.remote.model.response.UserRegistrationResponseDto
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.POST

interface AuthService {
@POST("user")
fun registerUser(
suspend fun registerUser(
@Body userRegistrationRequestDto: UserRegistrationRequestDto
): ApiResponse<UserRegistrationResponseDto>

@POST("login")
fun login(
suspend fun login(
@Body loginRequestDto: LoginRequestDto
): ApiResponse<LoginResponseDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.sopt.and.data.remote.service
import org.sopt.and.data.remote.model.base.ApiResponse
import org.sopt.and.data.remote.model.request.UserInfoUpdateRequestDto
import org.sopt.and.data.remote.model.response.HobbyResponseDto
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Header
Expand All @@ -11,19 +12,19 @@ import retrofit2.http.Path

interface UserService {
@GET("user/my-hobby")
fun getMyHobby(
suspend fun getMyHobby(
@Header("token") token: String
): ApiResponse<HobbyResponseDto>

@GET("user/{no}/hobby")
fun getOthersHobby(
suspend fun getOthersHobby(
@Header("token") token: String,
@Path("no") userNo: Int
): ApiResponse<HobbyResponseDto>

@PUT("user")
fun updateUserInfo(
suspend fun updateUserInfo(
@Header("token") token: String,
@Body userInfoUpdateRequestDto: UserInfoUpdateRequestDto
): ApiResponse<Unit>
): Response<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import javax.inject.Inject

class UserRepositoryImpl @Inject constructor(
private val userRemoteDataSource: UserRemoteDataSource
): UserRepository {
) : UserRepository {
override suspend fun getMyHobby(token: String): Result<Hobby> {
return runCatching {
userRemoteDataSource.getMyHobby(token = token).handleApiResponse().getOrThrow().toDomain()
Expand All @@ -29,10 +29,18 @@ class UserRepositoryImpl @Inject constructor(
hobby: String?
): Result<Unit> {
return runCatching {
userRemoteDataSource.updateUserInfo(token = token, userInfoUpdateRequestDto = UserInfoUpdateRequestDto(
password = password,
hobby = hobby
)).handleApiResponse().getOrThrow()
val response = userRemoteDataSource.updateUserInfo(
token = token,
userInfoUpdateRequestDto = UserInfoUpdateRequestDto(
password = password,
hobby = hobby
)
)
if(response.isSuccessful) {
Result.success(Unit)
} else {
Result.failure(Exception("Error : ${response.code()} ${response.message()}"))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.sopt.and.presentation.ui.main.screen

import org.sopt.and.domain.model.Hobby

sealed class UserInfoUpdateState {
data object Idle: UserInfoUpdateState()
data object Loading: UserInfoUpdateState()
data object Success: UserInfoUpdateState()
data class Failure(val errorMessage: String): UserInfoUpdateState()
data object Idle : UserInfoUpdateState()
data object Loading : UserInfoUpdateState()
data object Success : UserInfoUpdateState()
data class Failure(val errorMessage: String) : UserInfoUpdateState()
}

0 comments on commit 189f674

Please sign in to comment.