Catch NoProviderException

This commit is contained in:
sim 2023-03-05 12:43:50 +01:00
parent f9ca9e80e8
commit 5d64a9b27d
1 changed files with 110 additions and 100 deletions

View File

@ -14,11 +14,8 @@ import org.unifiedpush.distributor.nextpush.account.Account.accountType
import org.unifiedpush.distributor.nextpush.account.Account.deviceId import org.unifiedpush.distributor.nextpush.account.Account.deviceId
import org.unifiedpush.distributor.nextpush.account.Account.getAccount import org.unifiedpush.distributor.nextpush.account.Account.getAccount
import org.unifiedpush.distributor.nextpush.account.AccountType import org.unifiedpush.distributor.nextpush.account.AccountType
import org.unifiedpush.distributor.nextpush.api.provider.ApiDirectFactory import org.unifiedpush.distributor.nextpush.api.provider.* // ktlint-disable no-wildcard-imports
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint
import org.unifiedpush.distributor.nextpush.api.provider.ApiProviderFactory
import org.unifiedpush.distributor.nextpush.api.provider.ApiSSOFactory
import org.unifiedpush.distributor.nextpush.api.response.ApiResponse import org.unifiedpush.distributor.nextpush.api.response.ApiResponse
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -61,34 +58,37 @@ object Api {
Log.d(TAG, "No deviceId found.") Log.d(TAG, "No deviceId found.")
val parameters = mapOf("deviceName" to Build.MODEL) val parameters = mapOf("deviceName" to Build.MODEL)
try {
withApiProvider { apiProvider -> withApiProvider { apiProvider ->
apiProvider.createDevice(parameters) apiProvider.createDevice(parameters)
?.subscribeOn(Schedulers.newThread()) ?.subscribeOn(Schedulers.newThread())
?.observeOn(Schedulers.newThread()) ?.observeOn(Schedulers.newThread())
?.subscribe(object : Observer<ApiResponse?> { ?.subscribe(object : Observer<ApiResponse?> {
override fun onSubscribe(d: Disposable) { override fun onSubscribe(d: Disposable) {
Log.d(TAG, "onSubscribe") Log.d(TAG, "onSubscribe")
}
override fun onNext(response: ApiResponse) {
response.deviceId.let {
deviceId = it
} }
}
override fun onError(e: Throwable) { override fun onNext(response: ApiResponse) {
e.printStackTrace() response.deviceId.let {
} deviceId = it
}
override fun onComplete() {
// Sync once it is registered
deviceId?.let {
syncDevice(it)
} }
Log.d(TAG, "mApi register: onComplete")
} override fun onError(e: Throwable) {
}) e.printStackTrace()
}
override fun onComplete() {
// Sync once it is registered
deviceId?.let {
syncDevice(it)
}
Log.d(TAG, "mApi register: onComplete")
}
})
}
} catch (e: NoProviderException) {
e.printStackTrace()
} }
} }
} }
@ -110,33 +110,36 @@ object Api {
fun Context.apiDeleteDevice(block: () -> Unit = {}) { fun Context.apiDeleteDevice(block: () -> Unit = {}) {
val deviceId = deviceId ?: return val deviceId = deviceId ?: return
try {
withApiProvider { apiProvider -> withApiProvider { apiProvider ->
apiProvider.deleteDevice(deviceId) apiProvider.deleteDevice(deviceId)
?.subscribeOn(Schedulers.newThread()) ?.subscribeOn(Schedulers.newThread())
?.observeOn(Schedulers.newThread()) ?.observeOn(Schedulers.newThread())
?.subscribe(object : Observer<ApiResponse?> { ?.subscribe(object : Observer<ApiResponse?> {
override fun onSubscribe(d: Disposable) { override fun onSubscribe(d: Disposable) {
Log.d(TAG, "Subscribed to deleteDevice.") Log.d(TAG, "Subscribed to deleteDevice.")
}
override fun onNext(response: ApiResponse) {
if (response.success) {
Log.d(TAG, "Device successfully deleted.")
} else {
Log.d(TAG, "An error occurred while deleting the device.")
} }
}
override fun onError(e: Throwable) { override fun onNext(response: ApiResponse) {
e.printStackTrace() if (response.success) {
} Log.d(TAG, "Device successfully deleted.")
} else {
Log.d(TAG, "An error occurred while deleting the device.")
}
}
override fun onComplete() { override fun onError(e: Throwable) {
block() e.printStackTrace()
} }
})
this.deviceId = null override fun onComplete() {
block()
}
})
this.deviceId = null
}
} catch (e: NoProviderException) {
e.printStackTrace()
} }
} }
@ -151,63 +154,70 @@ object Api {
"appName" to appName "appName" to appName
) )
} ?: return } ?: return
try {
withApiProvider { apiProvider -> withApiProvider { apiProvider ->
apiProvider.createApp(parameters) apiProvider.createApp(parameters)
?.subscribeOn(Schedulers.newThread()) ?.subscribeOn(Schedulers.newThread())
?.observeOn(Schedulers.newThread()) ?.observeOn(Schedulers.newThread())
?.subscribe(object : Observer<ApiResponse?> { ?.subscribe(object : Observer<ApiResponse?> {
override fun onSubscribe(d: Disposable) { override fun onSubscribe(d: Disposable) {
Log.d(TAG, "Subscribed to createApp.") Log.d(TAG, "Subscribed to createApp.")
}
override fun onNext(response: ApiResponse) {
val nextpushToken = if (response.success) {
Log.d(TAG, "App successfully created.")
response.token
} else {
Log.d(TAG, "An error occurred while creating the application.")
null
} }
block(nextpushToken)
}
override fun onError(e: Throwable) { override fun onNext(response: ApiResponse) {
block(null) val nextpushToken = if (response.success) {
e.printStackTrace() Log.d(TAG, "App successfully created.")
} response.token
} else {
Log.d(TAG, "An error occurred while creating the application.")
null
}
block(nextpushToken)
}
override fun onComplete() {} override fun onError(e: Throwable) {
}) block(null)
e.printStackTrace()
}
override fun onComplete() {}
})
}
} catch (e: NoProviderException) {
e.printStackTrace()
} }
} }
fun Context.apiDeleteApp(nextpushToken: String, block: () -> Unit) { fun Context.apiDeleteApp(nextpushToken: String, block: () -> Unit) {
withApiProvider { apiProvider -> try {
apiProvider.deleteApp(nextpushToken) withApiProvider { apiProvider ->
?.subscribeOn(Schedulers.newThread()) apiProvider.deleteApp(nextpushToken)
?.observeOn(Schedulers.newThread()) ?.subscribeOn(Schedulers.newThread())
?.subscribe(object : Observer<ApiResponse?> { ?.observeOn(Schedulers.newThread())
override fun onSubscribe(d: Disposable) { ?.subscribe(object : Observer<ApiResponse?> {
Log.d(TAG, "Subscribed to deleteApp.") override fun onSubscribe(d: Disposable) {
} Log.d(TAG, "Subscribed to deleteApp.")
override fun onNext(response: ApiResponse) {
if (response.success) {
Log.d(TAG, "App successfully deleted.")
} else {
Log.d(TAG, "An error occurred while deleting the application.")
} }
}
override fun onError(e: Throwable) { override fun onNext(response: ApiResponse) {
e.printStackTrace() if (response.success) {
} Log.d(TAG, "App successfully deleted.")
} else {
Log.d(TAG, "An error occurred while deleting the application.")
}
}
override fun onComplete() { override fun onError(e: Throwable) {
block() e.printStackTrace()
} }
})
override fun onComplete() {
block()
}
})
}
} catch (e: NoProviderException) {
e.printStackTrace()
} }
} }
} }