Skip to content

Instantly share code, notes, and snippets.

@Alqueraf
Last active August 25, 2020 19:38
Show Gist options
  • Save Alqueraf/7fbc5946b96d67d1ec0ee54aa3251a80 to your computer and use it in GitHub Desktop.
Save Alqueraf/7fbc5946b96d67d1ec0ee54aa3251a80 to your computer and use it in GitHub Desktop.
Medium User Login Request
suspend fun userLogin(emailLoginRequest: EmailLoginRequest): User? = withContext(Dispatchers.IO) {
try {
// Email Login
val user = httpClient.post<User>(Endpoints.Login) {
body = emailLoginRequest
header("X-Requested-With", "XMLHttpRequest")
}
user
} catch (t: Throwable) {
// Handle Error
when (t) {
is ClientRequestException ->
if (t.response?.status?.value == 403) {
// Invalid login credentials
null
} else {
// Unhandled error
null
}
is UnresolvedAddressException -> {
// Internet Error
null
}
else -> {
// Unhandled error
null
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment