Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SerggioC/4d041e90efd19513b66068a3d50b0a72 to your computer and use it in GitHub Desktop.
Save SerggioC/4d041e90efd19513b66068a3d50b0a72 to your computer and use it in GitHub Desktop.
write to Buffer from Retroft interceptor
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val firebaseToken = Utils.getFirebaseToken(context)
val originalRequest: Request = chain.request()
var newRequest = originalRequest.newBuilder()
.addHeader("content-type", "application/json;charset=UTF-8")
.addHeader("accept", "application/json")
.addHeader("api-version", "1.0")
.addHeader("authorization", "Bearer $firebaseToken")
.addHeader("DEVICEOS", "Android " + versionCodeName + ", API Level: " + Build.VERSION.SDK_INT)
.addHeader("APPVERSION", BuildConfig.VERSION_NAME + " " + BuildConfig.VERSION_CODE)
.addHeader("APPLANG", Locale.getDefault().language)
.build()
if (newRequest.url.toString() == BuildConfig.API_ADDRESS + "api/transacoes.php" || newRequest.url.toString() == BuildConfig.API_ADDRESS + "api/transacoes_log.php") {
var signature: String? = ""
if (newRequest.body != null) {
val buffer = Buffer()
newRequest.body?.writeTo(buffer)
signature = generateHashWithHmac256(
Base64.encodeToString(
buffer.readString(StandardCharsets.UTF_8).toByteArray(), Base64.NO_WRAP
)
)
}
newRequest = newRequest.newBuilder()
.addHeader("X-Biip-Signature", signature ?: "")
.build()
}
val response = chain.proceed(newRequest)
return response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment