Skip to content

Instantly share code, notes, and snippets.

@emedinaa
Created May 6, 2022 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emedinaa/b7462a19b830aee93c49af19d345611c to your computer and use it in GitHub Desktop.
Save emedinaa/b7462a19b830aee93c49af19d345611c to your computer and use it in GitHub Desktop.
Retrofit + coroutines
override suspend fun notesByUser(
userId: String
): OperationResult<List<Note>> {
return try {
val response = serviceApi.notesByUser(userId)
val statusCode = response.code()
//200 300 400
if (response.isSuccessful) { //200
val body: NotesResponse? = response.body()
OperationResult.Success(body?.data?.map {
it.toNote()
} ?: emptyList())
} else {
//response.code()
val errorBody = response.errorBody()?.string()
OperationResult.Failure(Exception(errorBody))
}
} catch (exception: Exception) {
//500
OperationResult.Failure(exception)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment