@@ -24,13 +24,21 @@ import com.duckduckgo.duckplayer.api.PrivatePlayerMode.AlwaysAsk
24
24
import com.duckduckgo.duckplayer.api.PrivatePlayerMode.Disabled
25
25
import com.duckduckgo.duckplayer.api.PrivatePlayerMode.Enabled
26
26
import com.squareup.anvil.annotations.ContributesBinding
27
+ import dagger.Lazy
27
28
import dagger.SingleInstanceIn
29
+ import java.io.IOException
30
+ import java.io.InputStream
28
31
import javax.inject.Inject
32
+ import javax.inject.Named
29
33
import kotlinx.coroutines.CoroutineScope
30
34
import kotlinx.coroutines.flow.Flow
31
35
import kotlinx.coroutines.flow.combine
32
36
import kotlinx.coroutines.launch
33
37
import kotlinx.coroutines.withContext
38
+ import logcat.logcat
39
+ import okhttp3.Headers.Companion.toHeaders
40
+ import okhttp3.OkHttpClient
41
+ import okhttp3.Request
34
42
35
43
interface DuckPlayerFeatureRepository {
36
44
fun getDuckPlayerRemoteConfigJson (): String
@@ -73,6 +81,11 @@ interface DuckPlayerFeatureRepository {
73
81
74
82
suspend fun wasUsedBefore (): Boolean
75
83
suspend fun setUsed ()
84
+
85
+ suspend fun requestEmbed (
86
+ url : String ,
87
+ headers : Map <String , String >,
88
+ ): InputStream ?
76
89
}
77
90
78
91
@SingleInstanceIn(AppScope ::class )
@@ -81,6 +94,7 @@ class RealDuckPlayerFeatureRepository @Inject constructor(
81
94
private val duckPlayerDataStore : DuckPlayerDataStore ,
82
95
@AppCoroutineScope private val appCoroutineScope : CoroutineScope ,
83
96
private val dispatcherProvider : DispatcherProvider ,
97
+ @Named(" api" ) private val okHttpClient : Lazy <OkHttpClient >,
84
98
) : DuckPlayerFeatureRepository {
85
99
86
100
override fun getDuckPlayerRemoteConfigJson (): String {
@@ -212,4 +226,17 @@ class RealDuckPlayerFeatureRepository @Inject constructor(
212
226
override suspend fun setUsed () {
213
227
duckPlayerDataStore.setUsed()
214
228
}
229
+
230
+ override suspend fun requestEmbed (
231
+ url : String ,
232
+ headers : Map <String , String >,
233
+ ): InputStream ? {
234
+ return try {
235
+ val okHttpRequest = Request .Builder ().url(url).headers(headers.toHeaders()).build()
236
+ withContext(dispatcherProvider.io()) { okHttpClient.get().newCall(okHttpRequest).execute().body?.byteStream() }
237
+ } catch (e: IOException ) {
238
+ logcat { " Request failed: ${e.message} " }
239
+ null
240
+ }
241
+ }
215
242
}
0 commit comments