aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-26 18:35:01 -0500
committerAllan Wang <me@allanwang.ca>2018-12-26 18:35:01 -0500
commitbddb58f035b9190732f22db4b2cc1464b68fff17 (patch)
treef650731e97bd94d5dd2725b13d657059eb9c4ee1 /app/src/main/kotlin/com
parent3a3096be58bacd9408c10ef5d8add6c32204d4e9 (diff)
downloadfrost-bddb58f035b9190732f22db4b2cc1464b68fff17.tar.gz
frost-bddb58f035b9190732f22db4b2cc1464b68fff17.tar.bz2
frost-bddb58f035b9190732f22db4b2cc1464b68fff17.zip
Merge new auth flyweight and update hd image fetcher
Diffstat (limited to 'app/src/main/kotlin/com')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/FbRequest.kt25
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt30
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/services/FrostRequestService.kt14
3 files changed, 27 insertions, 42 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/FbRequest.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/FbRequest.kt
index 1aa2a1b6..0692fbfd 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/FbRequest.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/FbRequest.kt
@@ -24,38 +24,25 @@ import com.pitchedapps.frost.facebook.FB_URL_BASE
import com.pitchedapps.frost.facebook.FB_USER_MATCHER
import com.pitchedapps.frost.facebook.USER_AGENT_BASIC
import com.pitchedapps.frost.facebook.get
-import com.pitchedapps.frost.rx.RxFlyweight
+import com.pitchedapps.frost.rx.Flyweight
import com.pitchedapps.frost.utils.L
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.channels.Channel
-import kotlinx.coroutines.launch
-import kotlinx.coroutines.selects.select
+import kotlinx.coroutines.GlobalScope
import okhttp3.Call
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.logging.HttpLoggingInterceptor
import org.apache.commons.text.StringEscapeUtils
-import kotlin.coroutines.Continuation
-import kotlin.coroutines.suspendCoroutine
/**
* Created by Allan Wang on 21/12/17.
*/
-private class RxAuth : RxFlyweight<String, Long, RequestAuth>() {
-
- override fun call(input: String) = input.getAuth()
-
- override fun validate(input: String, cond: Long) =
- System.currentTimeMillis() - cond < 3600000 // valid for an hour
-
- override fun cache(input: String) = System.currentTimeMillis()
+ val fbAuth = Flyweight<String, RequestAuth>(GlobalScope, 100,3600000 /* an hour */) {
+ it.getAuth()
}
-private val auth = RxAuth()
-
/**
* Synchronously fetch [RequestAuth] from cookie
* [action] will only be called if a valid auth is found.
@@ -64,7 +51,7 @@ private val auth = RxAuth()
fun String?.fbRequest(fail: () -> Unit = {}, action: RequestAuth.() -> Unit) {
if (this == null) return fail()
try {
- val auth = auth(this).blockingGet()
+ val auth = fbAuth(this).blockingGet()
auth.action()
} catch (e: Exception) {
L.e { "Failed auth for ${hashCode()}: ${e.message}" }
@@ -72,8 +59,6 @@ fun String?.fbRequest(fail: () -> Unit = {}, action: RequestAuth.() -> Unit) {
}
}
-data class FbRequest(val cookie: String, val request: suspend (RequestAuth) -> Unit)
-
/**
* Underlying container for all fb requests
*/
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt
index e0ccea81..4afd8e8a 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt
@@ -33,8 +33,9 @@ import com.pitchedapps.frost.facebook.FB_URL_BASE
import com.pitchedapps.frost.facebook.formattedFbUrl
import com.pitchedapps.frost.facebook.get
import io.reactivex.Maybe
+import kotlinx.coroutines.runBlocking
+import kotlinx.coroutines.withTimeout
import okhttp3.Call
-import okhttp3.Request
import java.io.IOException
import java.io.InputStream
@@ -123,21 +124,22 @@ class HdImageFetcher(private val model: HdImageMaybe) : DataFetcher<InputStream>
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
if (!model.isValid) return callback.fail("Model is invalid")
- model.cookie.fbRequest(fail = { callback.fail("Invalid auth") }) {
- if (cancelled) return@fbRequest callback.fail("Cancelled")
- val url = getFullSizedImage(model.id).invoke()
- ?: return@fbRequest callback.fail("Null url")
- if (cancelled) return@fbRequest callback.fail("Cancelled")
- if (!url.contains("png") && !url.contains("jpg")) return@fbRequest callback.fail("Invalid format")
- urlCall = Request.Builder().url(url).get().call()
-
- inputStream = try {
- urlCall?.execute()?.body()?.byteStream()
- } catch (e: IOException) {
- null
+ val result: Result<InputStream?> = runCatching {
+ runBlocking {
+ withTimeout(20000L) {
+ val auth = fbAuth.fetch(model.cookie)
+ if (cancelled) throw RuntimeException("Cancelled")
+ val url = auth.getFullSizedImage(model.id).invoke() ?: throw RuntimeException("Null url")
+ if (cancelled) throw RuntimeException("Cancelled")
+ if (!url.contains("png") && !url.contains("jpg")) throw RuntimeException("Invalid format")
+ urlCall?.execute()?.body()?.byteStream()
+ }
}
- callback.onDataReady(inputStream)
}
+ if (result.isSuccess)
+ callback.onDataReady(result.getOrNull())
+ else
+ callback.onLoadFailed(result.exceptionOrNull() as? Exception ?: RuntimeException("Failed"))
}
override fun cleanup() {
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostRequestService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostRequestService.kt
index 989f1b24..c88f3946 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostRequestService.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostRequestService.kt
@@ -25,7 +25,7 @@ import android.content.Intent
import android.os.BaseBundle
import android.os.PersistableBundle
import com.pitchedapps.frost.facebook.requests.RequestAuth
-import com.pitchedapps.frost.facebook.requests.fbRequest
+import com.pitchedapps.frost.facebook.requests.fbAuth
import com.pitchedapps.frost.facebook.requests.markNotificationRead
import com.pitchedapps.frost.utils.EnumBundle
import com.pitchedapps.frost.utils.EnumBundleCompanion
@@ -179,15 +179,13 @@ class FrostRequestService : BaseJobService() {
}
launch(Dispatchers.IO) {
try {
- var failed = true
- cookie.fbRequest {
- L.d { "Requesting frost service for ${command.name}" }
- command.invoke(this, bundle)
- failed = false
- }
+ val auth = fbAuth.fetch(cookie)
+ command.invoke(auth, bundle)
L.d {
- "${if (failed) "Failed" else "Finished"} frost service for ${command.name} in ${System.currentTimeMillis() - startTime} ms"
+ "Finished frost service for ${command.name} in ${System.currentTimeMillis() - startTime} ms"
}
+ } catch (e: Exception) {
+ L.e(e) { "Failed frost service for ${command.name} in ${System.currentTimeMillis() - startTime} ms" }
} finally {
jobFinished(params, false)
}