aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/facebook
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-28 21:45:46 -0500
committerGitHub <noreply@github.com>2018-12-28 21:45:46 -0500
commit9c3d7c8b6cca17dc10fc310d41e547d1fe1725ea (patch)
tree8e6202efb768d954145038cb8642453c62650c5e /app/src/main/kotlin/com/pitchedapps/frost/facebook
parentc9769223cb014f588d93c1a73da157010e68a1c8 (diff)
parent8c4db7d79d4f9557d0eef2ef707663c5e8a7aac6 (diff)
downloadfrost-9c3d7c8b6cca17dc10fc310d41e547d1fe1725ea.tar.gz
frost-9c3d7c8b6cca17dc10fc310d41e547d1fe1725ea.tar.bz2
frost-9c3d7c8b6cca17dc10fc310d41e547d1fe1725ea.zip
Merge pull request #1269 from AllanWang/enhancement/coroutine-auth
Enhancement/coroutine
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/FbRequest.kt18
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt30
2 files changed, 22 insertions, 26 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 584107cc..50da367d 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,10 +24,12 @@ 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.GlobalScope
+import kotlinx.coroutines.runBlocking
import okhttp3.Call
import okhttp3.FormBody
import okhttp3.OkHttpClient
@@ -38,18 +40,10 @@ import org.apache.commons.text.StringEscapeUtils
/**
* 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.
@@ -58,7 +52,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 = runBlocking { fbAuth.fetch(this@fbRequest) }
auth.action()
} catch (e: Exception) {
L.e { "Failed auth for ${hashCode()}: ${e.message}" }
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() {