1+ package eu.weblibre.flutter_mozilla_components.api
2+
3+ import android.os.Build
4+ import androidx.annotation.RequiresApi
5+ import eu.weblibre.flutter_mozilla_components.GlobalComponents
6+ import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchApi
7+ import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchCookiePolicy
8+ import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchMethod
9+ import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchRedircet
10+ import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchRequest
11+ import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFetchResponse
12+ import eu.weblibre.flutter_mozilla_components.pigeons.GeckoHeader
13+ import kotlinx.coroutines.CoroutineScope
14+ import kotlinx.coroutines.Dispatchers
15+ import kotlinx.coroutines.SupervisorJob
16+ import kotlinx.coroutines.launch
17+ import kotlinx.coroutines.withContext
18+ import mozilla.components.concept.fetch.MutableHeaders
19+ import mozilla.components.concept.fetch.Request
20+ import java.util.concurrent.TimeUnit
21+
22+ class GeckoFetchApiImpl : GeckoFetchApi {
23+ private val components by lazy {
24+ requireNotNull(GlobalComponents .components) { " Components not initialized" }
25+ }
26+
27+ companion object {
28+ private val coroutineScope = CoroutineScope (Dispatchers .Main + SupervisorJob ())
29+ }
30+
31+ @RequiresApi(Build .VERSION_CODES .TIRAMISU )
32+ override fun fetch (
33+ request : GeckoFetchRequest ,
34+ callback : (Result <GeckoFetchResponse >) -> Unit
35+ ) {
36+ val headers = MutableHeaders ()
37+ for (header in request.headers) {
38+ headers.append(header.key, header.value)
39+ }
40+
41+ val request = Request (
42+ url = request.url,
43+ method = when (request.method) {
44+ GeckoFetchMethod .GET -> Request .Method .GET
45+ GeckoFetchMethod .HEAD -> Request .Method .HEAD
46+ GeckoFetchMethod .POST -> Request .Method .POST
47+ GeckoFetchMethod .PUT -> Request .Method .PUT
48+ GeckoFetchMethod .DELETE -> Request .Method .DELETE
49+ GeckoFetchMethod .CONNECT -> Request .Method .CONNECT
50+ GeckoFetchMethod .OPTIONS -> Request .Method .OPTIONS
51+ GeckoFetchMethod .TRACE -> Request .Method .TRACE
52+ },
53+ headers = headers,
54+ connectTimeout = if (request.connectTimeoutMillis != null ) Pair (
55+ request.connectTimeoutMillis,
56+ TimeUnit .MILLISECONDS
57+ ) else null ,
58+ readTimeout = if (request.readTimeoutMillis != null ) Pair (
59+ request.readTimeoutMillis,
60+ TimeUnit .MILLISECONDS
61+ ) else null ,
62+ body = if (request.body != null ) Request .Body .fromString(request.body) else null ,
63+ redirect = when (request.redirect) {
64+ GeckoFetchRedircet .FOLLOW -> Request .Redirect .FOLLOW
65+ GeckoFetchRedircet .MANUAL -> Request .Redirect .MANUAL
66+ },
67+ cookiePolicy = when (request.cookiePolicy) {
68+ GeckoFetchCookiePolicy .INCLUDE -> Request .CookiePolicy .INCLUDE
69+ GeckoFetchCookiePolicy .OMIT -> Request .CookiePolicy .OMIT
70+ },
71+ useCaches = request.useCaches,
72+ private = request.private,
73+ useOhttp = request.useOhttp,
74+ referrerUrl = request.referrerUrl,
75+ conservative = request.conservative
76+ )
77+
78+ coroutineScope.launch {
79+ withContext(Dispatchers .Main ) {
80+ val response = components.core.client.fetch(request)
81+ callback(
82+ Result .success(
83+ GeckoFetchResponse (
84+ url = response.url,
85+ status = response.status.toLong(),
86+ body = response.body.useStream { stream -> stream.readAllBytes() },
87+ headers = response.headers.map { it -> GeckoHeader (it.name, it.value) }
88+ )))
89+ }
90+ }
91+ }
92+ }
0 commit comments