-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Api response extensions #754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ed2c0c8
f99509c
2f71fd2
65d5bca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.network.utils.apiResponse | ||
|
|
||
| import com.infomaniak.core.network.models.ApiResponse | ||
|
|
||
| class ApiErrorException(response: ApiResponse<*>) : Exception("Error is null but required for an error $response") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.network.utils.apiResponse | ||
|
|
||
| import com.infomaniak.core.network.models.ApiResponse | ||
|
|
||
| class ApiResponseException(response: ApiResponse<*>) : Exception("Data is null but required for a success $response") | ||
|
benjaminVadon marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.core.network.utils.apiResponse | ||
|
benjaminVadon marked this conversation as resolved.
|
||
|
|
||
| import com.infomaniak.core.network.models.ApiError | ||
| import com.infomaniak.core.network.models.ApiResponse | ||
| import io.sentry.Sentry | ||
|
|
||
| inline fun <T> ApiResponse<T>.onSuccess(block: T.() -> Unit): ApiResponse<T> = apply { | ||
| if (isSuccess()) { | ||
| data?.run(block) ?: Sentry.captureException(ApiResponseException(this)) | ||
| } | ||
|
Comment on lines
+24
to
+27
|
||
| } | ||
|
|
||
| inline fun <T> ApiResponse<T>.onError(block: ApiError.() -> Unit): ApiResponse<T> = apply { | ||
| if (isError()) { | ||
| error?.run(block) ?: Sentry.captureException(ApiErrorException(this)) | ||
| } | ||
| } | ||
|
|
||
| inline fun <T> ApiResponse<T>.on( | ||
| onSuccess: T.() -> Unit, | ||
| onError: ApiError.() -> Unit | ||
| ): ApiResponse<T> = onSuccess(onSuccess).onError(onError) | ||
Uh oh!
There was an error while loading. Please reload this page.