OKHttp Authenticator 不适用于 Retrofit 暂停乐趣
OKHttp Authenticator not working with Retrofit suspend fun
我最近将 Retrofit 更新为 2.7.0
并将 OKHttp 更新为 3.14.4
以利用 Retrofit 界面上的暂停乐趣。
除此之外,我还尝试为刷新令牌逻辑实现 Authenticator。
这是改装界面
interface OfficeApi {
@Authenticated
@POST
suspend fun getCharacter(): Response<CharacterResponse>
}
这是我的身份验证器
class CharacterAuthenticator : Authenticator {
override fun authenticate(
route: Route?,
response: Response
): Request? {
if (responseCount(response) >= 2) return null
return response.request()
.newBuilder()
.removeHeader("Authorization")
.addHeader("Authorization", "Bearer $newToken")
.build()
return null
}
private fun responseCount(response: Response?): Int {
var result = 1
while (response?.priorResponse() != null) result++
return result
}
}
这是改装趣味电话
override suspend fun getCharacter() = safeApiCall(moshiConverter) {
myApi.getCharacter()
}
这是safeApiCall
:
suspend fun <T> safeApiCall(
moshiConverter: MoshiConverter,
apiCall: suspend () -> Response<T>
): Result<T?, ResultError.NetworkError> {
return try {
val response = apiCall()
if (response.isSuccessful) Result.Success(response.body())
else {
val errorBody = response.errorBody()
val errorBodyResponse = if (errorBody != null) {
moshiConverter.fromJsonObject(errorBody.string(), ErrorBodyResponse::class.java)
} else null
Result.Error(
ResultError.NetworkError(
httpCode = response.code(),
httpMessage = response.message(),
serverCode = errorBodyResponse?.code,
serverMessage = errorBodyResponse?.message
)
)
}
} catch (exception: Exception) {
Result.Error(ResultError.NetworkError(-1, exception.message))
}
}
身份验证器工作正常,尝试刷新令牌两次然后放弃。问题是:当它放弃时(return null),改造(safeApiCall
函数)的执行不会继续。通话成功与否,我没有任何反馈。
使用Authenticator和Coroutines有什么问题吗suspend fun
?
删除暂停尝试以下代码
fun getCharacter(): Response<CharacterResponse>
这不是死循环吗?
while (response?.priorResponse() != null)
不应该吗
var curResponse: Response? = response
while (curResponse?.priorResponse() != null) {
result++
curResponse = curResponse.priorResponse()
}
我最近将 Retrofit 更新为 2.7.0
并将 OKHttp 更新为 3.14.4
以利用 Retrofit 界面上的暂停乐趣。
除此之外,我还尝试为刷新令牌逻辑实现 Authenticator。
这是改装界面
interface OfficeApi {
@Authenticated
@POST
suspend fun getCharacter(): Response<CharacterResponse>
}
这是我的身份验证器
class CharacterAuthenticator : Authenticator {
override fun authenticate(
route: Route?,
response: Response
): Request? {
if (responseCount(response) >= 2) return null
return response.request()
.newBuilder()
.removeHeader("Authorization")
.addHeader("Authorization", "Bearer $newToken")
.build()
return null
}
private fun responseCount(response: Response?): Int {
var result = 1
while (response?.priorResponse() != null) result++
return result
}
}
这是改装趣味电话
override suspend fun getCharacter() = safeApiCall(moshiConverter) {
myApi.getCharacter()
}
这是safeApiCall
:
suspend fun <T> safeApiCall(
moshiConverter: MoshiConverter,
apiCall: suspend () -> Response<T>
): Result<T?, ResultError.NetworkError> {
return try {
val response = apiCall()
if (response.isSuccessful) Result.Success(response.body())
else {
val errorBody = response.errorBody()
val errorBodyResponse = if (errorBody != null) {
moshiConverter.fromJsonObject(errorBody.string(), ErrorBodyResponse::class.java)
} else null
Result.Error(
ResultError.NetworkError(
httpCode = response.code(),
httpMessage = response.message(),
serverCode = errorBodyResponse?.code,
serverMessage = errorBodyResponse?.message
)
)
}
} catch (exception: Exception) {
Result.Error(ResultError.NetworkError(-1, exception.message))
}
}
身份验证器工作正常,尝试刷新令牌两次然后放弃。问题是:当它放弃时(return null),改造(safeApiCall
函数)的执行不会继续。通话成功与否,我没有任何反馈。
使用Authenticator和Coroutines有什么问题吗suspend fun
?
删除暂停尝试以下代码
fun getCharacter(): Response<CharacterResponse>
这不是死循环吗?
while (response?.priorResponse() != null)
不应该吗
var curResponse: Response? = response
while (curResponse?.priorResponse() != null) {
result++
curResponse = curResponse.priorResponse()
}