Add proper login call with adapter for Fever api

This commit is contained in:
Shinokuni 2021-12-25 16:22:16 +01:00
parent 162fcfa75d
commit eb6d487f8f
6 changed files with 99 additions and 19 deletions

View File

@ -1,23 +1,29 @@
package com.readrops.api.services.fever
import android.util.Log
import com.readrops.api.services.fever.adapters.FeverAPIAdapter
import com.readrops.api.utils.ApiUtils
import com.readrops.db.entities.Feed
import com.squareup.moshi.Moshi
import okhttp3.MultipartBody
class FeverDataSource(val service: FeverService) {
class FeverDataSource(private val service: FeverService) {
suspend fun login(login: String, password: String) {
val credentials = ApiUtils.md5hash("$login:$password")
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("api_key", credentials)
.build()
.setType(MultipartBody.FORM)
.addFormDataPart("api_key", credentials)
.build()
val response = service.login(requestBody)
Log.d("TAG", "login: ")
val adapter = Moshi.Builder()
.add(Boolean::class.java, FeverAPIAdapter())
.build()
.adapter(Boolean::class.java)
adapter.fromJson(response.source())!!
}
suspend fun getFeeds(): List<Feed> = service.getFeeds()

View File

@ -0,0 +1,34 @@
package com.readrops.api.services.fever.adapters
import com.readrops.api.utils.exceptions.ParseException
import com.squareup.moshi.*
class FeverAPIAdapter : JsonAdapter<Boolean>() {
@ToJson
override fun toJson(writer: JsonWriter, value: Boolean?) {
// useless here
}
@FromJson
override fun fromJson(reader: JsonReader): Boolean = with(reader) {
return try {
beginObject()
skipName()
skipValue()
var authenticated = 0
if (nextName() == "auth") {
authenticated = nextInt()
}
skipName()
skipValue()
authenticated == 1
} catch (e: Exception) {
throw ParseException(e.message)
}
}
}

View File

@ -0,0 +1,23 @@
package com.readrops.api.services.fever.adapters
import com.readrops.api.TestUtils
import com.squareup.moshi.Moshi
import okio.Buffer
import kotlin.test.Test
import kotlin.test.assertEquals
class FeverAPIAdapterTest {
private val adapter = Moshi.Builder()
.add(Boolean::class.java, FeverAPIAdapter())
.build()
.adapter(Boolean::class.java)
@Test
fun authenticatedTest() {
val stream = TestUtils.loadResource("services/fever/authentication.json")
val value = adapter.fromJson(Buffer().readFrom(stream))!!
assertEquals(value, true)
}
}

View File

@ -0,0 +1,5 @@
{
"api_version": 3,
"auth": 1,
"last_refreshed_on_time": 1640284745
}

View File

@ -17,6 +17,7 @@ import com.readrops.app.repositories.NextNewsRepository
import com.readrops.app.utils.GlideApp
import com.readrops.db.entities.account.Account
import com.readrops.db.entities.account.AccountType
import kotlinx.coroutines.Dispatchers
import org.koin.android.ext.koin.androidApplication
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModel
@ -33,7 +34,7 @@ val appModule = module {
AccountType.FRESHRSS -> FreshRSSRepository(get(parameters = { parametersOf(Credentials.toCredentials(account)) }),
get(), androidContext(), account)
AccountType.FEVER -> FeverRepository(get(parameters = { parametersOf(Credentials.toCredentials(account)) }),
get(), get(), account)
Dispatchers.IO, get(), get(), account)
else -> throw IllegalArgumentException("Account type not supported")
}
}

View File

@ -9,29 +9,40 @@ import com.readrops.db.entities.Feed
import com.readrops.db.entities.account.Account
import io.reactivex.Completable
import io.reactivex.Single
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.rx2.await
import kotlinx.coroutines.rx2.rxCompletable
class FeverRepository(
val feverDataSource: FeverDataSource,
database: Database,
context: Context,
account: Account?,
) :
ARepository(database, context, account) {
private val feverDataSource: FeverDataSource,
private val dispatcher: CoroutineDispatcher,
database: Database,
context: Context,
account: Account?,
) : ARepository(database, context, account) {
override fun login(account: Account, insert: Boolean): Completable {
return rxCompletable {
feverDataSource.login(account.login!!, account.password!!)
return rxCompletable(context = dispatcher) {
try {
feverDataSource.login(account.login!!, account.password!!)
account.displayedName = account.accountType!!.name
database.accountDao().insert(account)
.doOnSuccess { account.id = it.toInt() }
.await()
} catch (e: Exception) {
error(e.message!!)
}
}
}
override fun sync(feeds: MutableList<Feed>?, update: FeedUpdate?): Completable {
return rxCompletable {
override fun sync(feeds: List<Feed>?, update: FeedUpdate?): Completable {
return rxCompletable(context = dispatcher) {
}
}
override fun addFeeds(results: MutableList<ParsingResult>?): Single<MutableList<FeedInsertionResult>> {
override fun addFeeds(results: List<ParsingResult>?): Single<List<FeedInsertionResult>> {
TODO("Not yet implemented")
}
}