Skip to content

Instantly share code, notes, and snippets.

@Lavanyagaur22
Last active April 6, 2019 08:09
Show Gist options
  • Save Lavanyagaur22/09ed2d66ad3d98a90a42e3e2b04fe5c6 to your computer and use it in GitHub Desktop.
Save Lavanyagaur22/09ed2d66ad3d98a90a42e3e2b04fe5c6 to your computer and use it in GitHub Desktop.
private fun getConflictInterceptor(context: Context): Interceptor? {
conflictInterceptor?.let {
return it
} ?: kotlin.run {
conflictInterceptor = Interceptor {
val request = it.request()
//all the HTTP work happens, producing a response to satisfy the request.
val response = it.proceed(request)
//To access the response body twice, clone the buffer before reading it.
//By that the original buffer is neither emptied nor closed and we can read it twice.
val responseBody = response.body()
val bufferedSource = responseBody?.source()
bufferedSource?.request(Long.MAX_VALUE)
val buffer = bufferedSource?.buffer()
val responseBodyString = buffer?.clone()?.readString(Charset.forName("UTF-8")) ?: ""
//To see for conflict, "VoyagerConflict" which comes in the message is searched for.
if (responseBodyString.contains("VoyagerConflict"))
showToast(context)
return@Interceptor response
}
}
return conflictInterceptor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment