Created
May 6, 2022 20:22
-
-
Save emedinaa/b7462a19b830aee93c49af19d345611c to your computer and use it in GitHub Desktop.
Retrofit + coroutines
This file contains hidden or 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
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