Use class for Api

This commit is contained in:
sim 2023-03-05 16:03:56 +01:00
parent 2f92edc511
commit 94b3e4d933
6 changed files with 61 additions and 95 deletions

View File

@ -19,39 +19,28 @@ import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.m
import org.unifiedpush.distributor.nextpush.api.response.ApiResponse
import java.util.concurrent.TimeUnit
object Api {
class Api(val context: Context) {
private val TAG = Api::class.java.simpleName
private var provider: ApiProviderFactory? = null
private var source: EventSource? = null
private val Context.baseUrl: String
get() = getAccount(this)?.url ?: "http://0.0.0.0/"
private val baseUrl: String
get() = getAccount(context)?.url ?: "http://0.0.0.0/"
private fun Context.withApiProvider(block: (ApiProvider) -> Unit) {
(
provider ?: run {
Log.d(TAG, "Setting SSOProvider")
when (accountType) {
AccountType.SSO -> ApiSSOFactory(this)
AccountType.Direct -> ApiDirectFactory(this)
}.apply {
provider = this
}
}
).getProviderAndExecute(block)
private fun withApiProvider(block: (ApiProvider) -> Unit) {
when (context.accountType) {
AccountType.SSO -> ApiSSOFactory(context)
AccountType.Direct -> ApiDirectFactory(context)
}.getProviderAndExecute(block)
}
fun apiDestroy() {
Log.d(TAG, "Destroying API")
provider?.destroyProvider()
provider = null
source?.cancel()
source = null
syncSource?.cancel()
syncSource = null
}
fun Context.apiSync() {
deviceId?.let {
fun apiSync() {
context.deviceId?.let {
syncDevice(it)
}
?: run {
@ -70,7 +59,7 @@ object Api {
override fun onNext(response: ApiResponse) {
response.deviceId.let {
deviceId = it
context.deviceId = it
}
}
@ -80,7 +69,7 @@ object Api {
override fun onComplete() {
// Sync once it is registered
deviceId?.let {
context.deviceId?.let {
syncDevice(it)
}
Log.d(TAG, "mApi register: onComplete")
@ -93,7 +82,7 @@ object Api {
}
}
private fun Context.syncDevice(deviceId: String) {
private fun syncDevice(deviceId: String) {
val client = OkHttpClient.Builder()
.readTimeout(0, TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
@ -104,12 +93,12 @@ object Api {
.get()
.build()
source = EventSources.createFactory(client).newEventSource(request, SSEListener(this))
syncSource = EventSources.createFactory(client).newEventSource(request, SSEListener(context))
Log.d(TAG, "cSync done.")
}
fun Context.apiDeleteDevice(block: () -> Unit = {}) {
val deviceId = deviceId ?: return
fun apiDeleteDevice(block: () -> Unit = {}) {
val deviceId = context.deviceId ?: return
try {
withApiProvider { apiProvider ->
apiProvider.deleteDevice(deviceId)
@ -136,19 +125,19 @@ object Api {
block()
}
})
this.deviceId = null
context.deviceId = null
}
} catch (e: NoProviderException) {
e.printStackTrace()
}
}
fun Context.apiCreateApp(
fun apiCreateApp(
appName: String,
block: (String?) -> Unit
) {
// The unity of connector token must already be checked here
val parameters = deviceId?.let {
val parameters = context.deviceId?.let {
mutableMapOf(
"deviceId" to it,
"appName" to appName
@ -188,7 +177,7 @@ object Api {
}
}
fun Context.apiDeleteApp(nextpushToken: String, block: () -> Unit) {
fun apiDeleteApp(nextpushToken: String, block: () -> Unit) {
try {
withApiProvider { apiProvider ->
apiProvider.deleteApp(nextpushToken)
@ -220,4 +209,8 @@ object Api {
e.printStackTrace()
}
}
companion object {
private var syncSource: EventSource? = null
}
}

View File

@ -1,19 +1,15 @@
package org.unifiedpush.distributor.nextpush.api.provider
import android.content.Context
import android.util.Log
import okhttp3.* // ktlint-disable no-wildcard-imports
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint
import org.unifiedpush.distributor.nextpush.utils.TAG
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
class ApiDirectFactory(val context: Context) : ApiProviderFactory {
private var apiProvider: ApiProvider? = null
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
val account = getAccount(context) ?: run {
throw NoProviderException("No account found")
@ -24,22 +20,13 @@ class ApiDirectFactory(val context: Context) : ApiProviderFactory {
val client = account.getAccount(context) as OkHttpClient? ?: run {
throw NoProviderException("No client found")
}
apiProvider?.let(block)
?: run {
Log.d(TAG, "Creating new provider")
Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("$url$mApiEndpoint").build()
.create(ApiProvider::class.java).let {
apiProvider = it
block(it)
}
}
}
override fun destroyProvider() {
// Nothing
}
}

View File

@ -3,5 +3,4 @@ package org.unifiedpush.distributor.nextpush.api.provider
class NoProviderException(message: String) : Exception(message)
interface ApiProviderFactory {
fun getProviderAndExecute(block: (ApiProvider) -> Unit)
fun destroyProvider()
}

View File

@ -6,32 +6,26 @@ import com.google.gson.GsonBuilder
import com.nextcloud.android.sso.api.NextcloudAPI
import com.nextcloud.android.sso.model.SingleSignOnAccount
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
import org.unifiedpush.distributor.nextpush.utils.TAG
import retrofit2.NextcloudRetrofitApiBuilder
class ApiSSOFactory(val context: Context) : ApiProviderFactory {
private val TAG = ApiSSOFactory::class.java.simpleName
private var apiProvider: ApiProvider? = null
private var nextcloudAPI: NextcloudAPI? = null
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
var nextcloudAPI: NextcloudAPI? = null
val account = getAccount(context) ?: run {
throw NoProviderException("No account found")
}
val client = account.getAccount(context) as SingleSignOnAccount? ?: run {
throw NoProviderException("No client found")
}
apiProvider?.let(block)
?: run {
Log.d(TAG, "Creating new provider")
val ssoApiCallback = object : NextcloudAPI.ApiConnectedListener {
override fun onConnected() {
Log.d(TAG, "Api connected.")
nextcloudAPI?.let { nextcloudAPI ->
NextcloudRetrofitApiBuilder(nextcloudAPI, ApiProvider.mApiEndpoint)
.create(ApiProvider::class.java).let {
apiProvider = it
block(it)
}
}
@ -49,9 +43,3 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory {
)
}
}
override fun destroyProvider() {
nextcloudAPI?.stop()
nextcloudAPI = null
}
}

View File

@ -4,9 +4,7 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
import org.unifiedpush.distributor.nextpush.api.Api.apiCreateApp
import org.unifiedpush.distributor.nextpush.api.Api.apiDeleteApp
import org.unifiedpush.distributor.nextpush.api.Api.apiDeleteDevice
import org.unifiedpush.distributor.nextpush.api.Api
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint
import org.unifiedpush.distributor.nextpush.utils.TAG
@ -115,11 +113,11 @@ object Distributor {
sendUnregistered(context, it)
db.unregisterApp(it)
}
context.apiDeleteDevice(block)
Api(context).apiDeleteDevice(block)
}
fun createApp(context: Context, appName: String, connectorToken: String, block: () -> Unit) {
context.apiCreateApp(appName) { nextpushToken ->
Api(context).apiCreateApp(appName) { nextpushToken ->
nextpushToken?.let {
getDb(context).registerApp(appName, connectorToken, it)
}
@ -133,7 +131,7 @@ object Distributor {
db.getAppToken(
connectorToken
)?.let { nextpushToken ->
context.apiDeleteApp(nextpushToken) {
Api(context).apiDeleteApp(nextpushToken) {
db.unregisterApp(connectorToken)
block()
}

View File

@ -8,8 +8,7 @@ import android.os.IBinder
import android.os.PowerManager
import android.util.Log
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
import org.unifiedpush.distributor.nextpush.api.Api.apiDestroy
import org.unifiedpush.distributor.nextpush.api.Api.apiSync
import org.unifiedpush.distributor.nextpush.api.Api
import org.unifiedpush.distributor.nextpush.api.SSEListener.Companion.lastEventDate
import org.unifiedpush.distributor.nextpush.utils.NOTIFICATION_ID_FOREGROUND
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.createForegroundNotification
@ -18,6 +17,7 @@ import org.unifiedpush.distributor.nextpush.utils.TAG
class StartService : Service() {
private val networkCallback = RestartNetworkCallback(this)
private var api: Api? = null
override fun onBind(intent: Intent?): IBinder? {
return null
@ -41,7 +41,7 @@ class StartService : Service() {
override fun onDestroy() {
Log.d(TAG, "Destroying Service")
apiDestroy()
api?.apiDestroy()
wakeLock?.let {
while (it.isHeld) {
it.release()
@ -69,8 +69,9 @@ class StartService : Service() {
acquire(10000L /*10 secs*/)
}
}
apiSync()
api = Api(this).also {
it.apiSync()
}
}
companion object StartServiceCompanion {