Skip to content

Commit

Permalink
fix(android): project-wide requests headers rewritten by GIPHY
Browse files Browse the repository at this point in the history
fix #92
  • Loading branch information
pshoniuk committed Feb 8, 2023
1 parent 14e1c08 commit b0ce09c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.giphyreactnativesdk

import android.net.Uri
import com.facebook.imagepipeline.request.ImageRequest
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.facebook.react.bridge.JavaOnlyMap
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.modules.fresco.ReactNetworkImageRequest
import com.giphy.sdk.ui.GiphyFrescoImageRequestHandler

private fun headersToRNMap(headers: Map<String, String>): ReadableMap {
val rv = JavaOnlyMap()
headers.entries.forEach { rv.putString(it.key, it.value) }
return rv
}

class GiphyRNFrescoImageRequestHandler : GiphyFrescoImageRequestHandler {
override fun getRequest(
source: Uri,
headers: Map<String, String>,
cacheChoice: ImageRequest.CacheChoice
): ImageRequest {
val requestBuilder =
ImageRequestBuilder.newBuilderWithSource(source).setCacheChoice(cacheChoice)
return ReactNetworkImageRequest.fromBuilderWithHeaders(
requestBuilder,
headersToRNMap(headers),
)
}
}
12 changes: 4 additions & 8 deletions android/src/main/java/com/giphyreactnativesdk/GiphySDKModule.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.giphyreactnativesdk

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.*
import com.giphy.sdk.ui.Giphy
import com.giphyreactnativesdk.utils.RNSDKInfo
import com.giphyreactnativesdk.utils.getVideoPlayerFactory
Expand Down Expand Up @@ -40,13 +37,12 @@ class GiphySDKModule(reactContext: ReactApplicationContext) :

if (apiKey != null) {
val appInfo = RNSDKInfo(reactApplicationContext)
Giphy.configure(
Giphy.configureRNSDK(
reactApplicationContext,
apiKey,
verificationMode,
metadata = hashMapOf(
appInfo.name to appInfo.version
)
metadata = hashMapOf(appInfo.name to appInfo.version),
frescoImageRequestHandler = GiphyRNFrescoImageRequestHandler()
)
}
}
Expand Down

0 comments on commit b0ce09c

Please sign in to comment.