Created
July 10, 2022 21:28
-
-
Save SerggioC/4d041e90efd19513b66068a3d50b0a72 to your computer and use it in GitHub Desktop.
write to Buffer from Retroft interceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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