Skip to content

Instantly share code, notes, and snippets.

@aqua30
Created August 22, 2022 08:50
Show Gist options
  • Save aqua30/1c4a2aa25ad8b361db0c0484d29ddf89 to your computer and use it in GitHub Desktop.
Save aqua30/1c4a2aa25ad8b361db0c0484d29ddf89 to your computer and use it in GitHub Desktop.
data class ApiResponse<T>(
val httpCode: Int = HttpURLConnection.HTTP_OK,
val body: T? = null,
val errorMessage: String? = null,
)
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