Skip to content

Commit

Permalink
Merge pull request #19 from team-haribo/feature/14-write-the-logic-login
Browse files Browse the repository at this point in the history
🔀 :: (#14) - write the logic login
  • Loading branch information
diejdkll authored Jan 15, 2024
2 parents 7066bb1 + 6db8e6d commit be0a2de
Show file tree
Hide file tree
Showing 30 changed files with 511 additions and 6 deletions.
38 changes: 38 additions & 0 deletions core/common/src/main/java/com/goms/common/Event.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.goms.common

sealed class Event<out T>(
val data: T? = null
) {

object Loading : Event<Nothing>()

// 20X: 성공
class Success<T>(data: T? = null) : Event<T>(data = data)

// 400: 올바르지 않은 요청
object BadRequest : Event<Nothing>()

// 401: 비인증 상태
object Unauthorized : Event<Nothing>()

// 403: 권한이 없음
object ForBidden : Event<Nothing>()

// 404: 요청한 리소스를 찾을 수 없음
object NotFound : Event<Nothing>()

// 406: 클라이언트가 허용되지 않는 규격을 요청
object NotAcceptable : Event<Nothing>()

// 408: 요청시간초과
object TimeOut : Event<Nothing>()

// 409: 충돌발생
object Conflict : Event<Nothing>()

// 50X: 서버에러
object Server : Event<Nothing>()

// 예상하지 못한 에러
object UnKnown : Event<Nothing>()
}
67 changes: 67 additions & 0 deletions core/common/src/main/java/com/goms/common/errorHandling.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.goms.common

import android.util.Log
import com.goms.common.exception.*

suspend fun <T> Throwable.errorHandling(
badRequestAction: suspend () -> Unit = {},
unauthorizedAction: suspend () -> Unit = {},
forBiddenAction: suspend () -> Unit = {},
notFoundAction: suspend () -> Unit = {},
notAcceptableAction: suspend () -> Unit = {},
timeOutAction: suspend () -> Unit = {},
conflictAction: suspend () -> Unit = {},
serverAction: suspend () -> Unit = {},
unknownAction: suspend () -> Unit = {},
): Event<T> =
when (this) {
is BadRequestException -> {
errorLog("BadRequestException", message)
badRequestAction()
Event.BadRequest
}
is UnauthorizedException, is TokenExpirationException -> {
errorLog("UnauthorizedException", message)
unauthorizedAction()
Event.Unauthorized
}
is ForBiddenException -> {
errorLog("ForBiddenException", message)
forBiddenAction()
Event.ForBidden
}
is NotFoundException -> {
errorLog("NotFoundException", message)
notFoundAction()
Event.NotFound
}
is NotAcceptableException -> {
errorLog("NotAcceptableException", message)
notAcceptableAction()
Event.NotAcceptable
}
is TimeOutException -> {
errorLog("TimeOutException", message)
timeOutAction()
Event.TimeOut
}
is ConflictException -> {
errorLog("ConflictException", message)
conflictAction()
Event.Conflict
}
is ServerException -> {
errorLog("ServerException", message)
serverAction()
Event.Server
}
else -> {
errorLog("UnKnownException", message)
unknownAction()
Event.UnKnown
}
}

private fun errorLog(tag: String, msg: String?) {
Log.d("ErrorHandling-$tag", msg ?: "알 수 없는 오류")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.goms.common.exception

// 400: 올바르지 않은 요청
class BadRequestException(
override val message: String?
) : RuntimeException()


// 401: 비인증 상태
class UnauthorizedException(
override val message: String?
) : RuntimeException()


// 403: 권한이 없음
class ForBiddenException(
override val message: String?
) : RuntimeException()


// 404: 요청한 리소스를 찾을 수 없음
class NotFoundException(
override val message: String?
) : RuntimeException()


// 406: 클라이언트가 허용되지 않는 규격을 요청
class NotAcceptableException(
override val message: String?
) : RuntimeException()

// 408: 요청시간초과
class TimeOutException(
override val message: String?
) : RuntimeException()


// 409: 충돌발생
class ConflictException(
override val message: String?
) : RuntimeException()


// 50X: 서버에러
class ServerException(
override val message: String?
) : RuntimeException()


// 정의되지 않은 HTTP 상태 코드나 사용자 정의 상태 코드
class OtherHttpException(
val code: Int,
override val message: String?
) : RuntimeException()

// 예상하지 못한 에러
class UnKnownException(
override val message: String?
) : RuntimeException()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.goms.common.exception

class NoInternetException : RuntimeException() {
override val message: String
get() = "네트워크가 불안정합니다, 데이터나 와이파이 연결 상태를 확인해주세요."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.goms.common.exception

import java.io.IOException

class TokenExpirationException : IOException() {
override val message: String
get() = "토큰이 만료되었습니다. 다시 로그인 해주세요"
}
17 changes: 17 additions & 0 deletions core/data/src/main/java/com/goms/data/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.goms.data.di

import com.goms.data.repository.auth.AuthRepository
import com.goms.data.repository.auth.AuthRepositoryImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {
@Binds
abstract fun bindAuthRepository(
authRepositoryImpl: AuthRepositoryImpl
): AuthRepository
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.goms.data.repository.auth

import com.goms.model.request.auth.LoginRequest
import com.goms.model.response.auth.LoginResponse
import kotlinx.coroutines.flow.Flow

interface AuthRepository {
suspend fun login(body: LoginRequest): Flow<LoginResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.goms.data.repository.auth

import com.goms.model.request.auth.LoginRequest
import com.goms.model.response.auth.LoginResponse
import com.goms.network.datasource.auth.AuthDataSource
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class AuthRepositoryImpl @Inject constructor(
private val remoteAuthDataSource: AuthDataSource
) : AuthRepository {
override suspend fun login(body: LoginRequest): Flow<LoginResponse> {
return remoteAuthDataSource.login(body = body)
}
}
13 changes: 13 additions & 0 deletions core/domain/src/main/java/com/goms/domain/auth/LoginUseCase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.goms.domain.auth

import com.goms.data.repository.auth.AuthRepository
import com.goms.model.request.auth.LoginRequest
import javax.inject.Inject

class LoginUseCase @Inject constructor(
private val authRepository: AuthRepository
) {
suspend operator fun invoke(body: LoginRequest) = kotlin.runCatching {
authRepository.login(body = body)
}
}
Empty file.
Empty file.
Empty file.
4 changes: 0 additions & 4 deletions core/model/src/main/java/com/goms/model/Ci.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.goms.model.request.auth

data class LoginRequest(
val code: String
)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.goms.model.response.auth

data class LoginResponse(
val accessToken: String,
val refreshToken: String,
val accessTokenExp: String,
val refreshTokenExp: String,
val authority: String
)
Empty file.
18 changes: 18 additions & 0 deletions core/network/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import java.io.FileInputStream
import java.util.Properties

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
id("goms.android.core")
id("goms.android.hilt")
}

android {
buildFeatures {
buildConfig = true
}

defaultConfig {
buildConfigField("String", "BASE_URL", getApiKey("BASE_URL"))
}

namespace = "com.goms.network"
}

Expand All @@ -20,4 +31,11 @@ dependencies {
implementation(libs.retrofit.core)
implementation(libs.retrofit.kotlin.serialization)
implementation(libs.retrofit.moshi.converter)
}

fun getApiKey(propertyKey: String): String {
val propFile = rootProject.file("./local.properties")
val properties = Properties()
properties.load(FileInputStream(propFile))
return properties.getProperty(propertyKey)
}
Empty file.
13 changes: 13 additions & 0 deletions core/network/src/main/java/com/goms/network/api/AuthAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.goms.network.api

import com.goms.model.request.auth.LoginRequest
import com.goms.model.response.auth.LoginResponse
import retrofit2.http.Body
import retrofit2.http.POST

interface AuthAPI {
@POST("/api/v2/auth/signin")
suspend fun login(
@Body body: LoginRequest
): LoginResponse
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.goms.network.datasource.auth

import com.goms.model.request.auth.LoginRequest
import com.goms.model.response.auth.LoginResponse
import kotlinx.coroutines.flow.Flow

interface AuthDataSource {
suspend fun login(body: LoginRequest): Flow<LoginResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.goms.network.datasource.auth

import com.goms.model.request.auth.LoginRequest
import com.goms.model.response.auth.LoginResponse
import com.goms.network.api.AuthAPI
import com.goms.network.util.GomsApiHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

class AuthDataSourceImpl @Inject constructor(
private val authAPI: AuthAPI
) : AuthDataSource {
override suspend fun login(body: LoginRequest): Flow<LoginResponse> = flow {
emit(
GomsApiHandler<LoginResponse>()
.httpRequest { authAPI.login(body = body) }
.sendRequest()
)
}.flowOn(Dispatchers.IO)
}
Loading

0 comments on commit be0a2de

Please sign in to comment.