Created
August 22, 2022 08:50
-
-
Save aqua30/1c4a2aa25ad8b361db0c0484d29ddf89 to your computer and use it in GitHub Desktop.
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
data class ApiResponse<T>( | |
val httpCode: Int = HttpURLConnection.HTTP_OK, | |
val body: T? = null, | |
val errorMessage: String? = null, | |
) |
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
class UserRepositoryImpl( | |
private val testApis: TestApis | |
): UserRepository { | |
override suspend fun getAllUsers(): ApiResponse<List<User>> { | |
return try { | |
ApiResponse( | |
body = testApis.getAllUsers() | |
) | |
} catch (e: HttpException) { | |
ApiResponse( | |
httpCode = e.code(), | |
errorMessage = "server error" | |
) | |
} catch (e: IOException) { | |
ApiResponse( | |
errorMessage = "connection error" | |
) | |
} | |
} | |
override suspend fun getUserById(id: Int): ApiResponse<User> { | |
return try { | |
ApiResponse( | |
body = testApis.getUserById(id) | |
) | |
} catch (e: HttpException) { | |
ApiResponse( | |
httpCode = e.code(), | |
errorMessage = "server error" | |
) | |
} catch (e: IOException) { | |
ApiResponse( | |
errorMessage = "connection error" | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment