You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
version: Moya/Combine (15.0.0), CombineExt (1.8.0)
Now I have a need to re -refresh the token when the token expires. After success, I need to re -execute the previous request again. The code is as follows:
privatefunc request(_ target:Target)->AnyPublisher<Moya.Response,APIError>{letactualRequest= provider.requestPublisher(target)return actualRequest.tryMap{ response inletjson=try? response.mapJSON()as?[String:Any]
if let json,let code =json["code"]as?String{
if code =="ERR003"{throwAPIError.tokenExpired(reason:"token expired")}
if code =="ERR004"{throwAPIError.refreshTokenExpired(reason:"login expired")}}return response
}.mapError{ error in
if let error = error as?APIError{return error
}
if let error = error as?MoyaError{returnAPIError.moyaError(from: error)}returnAPIError.unknown
}// `retryWhen` operator is provided by `CombineExt`.retryWhen{ errorPublisher inreturn errorPublisher.filter{ error in
switch error {// When the error type is `tokenexpired`,Only go to re -requestcase.tokenExpired(_):return true
default:return false
}}.setFailureType(to:APIError.self).flatMap{ error ->AnyPublisher<Void,APIError>in
switch error {case.tokenExpired(_):// This is my definition of re -refreshing tokenreturnLogInManager.share.refreshToken()default:returnJust(()).setFailureType(to:APIError.self).mapToVoid().eraseToAnyPublisher()}}}.eraseToAnyPublisher()}// The type of error I customizedenumAPIError:Error,LocalizedError{case unknown, tokenExpired(reason:String), refreshTokenExpired(reason:String), moyaError(from:MoyaError)varerrorDescription:String?{
switch self{case.unknown:return"Unknown error"case.tokenExpired(let reason),.refreshTokenExpired(let reason):return reason
case.moyaError(let from):return from.localizedDescription
}}}
However, after the success of the token, this code did not execute the original request. Does anyone know what happened in my code?
The text was updated successfully, but these errors were encountered:
version: Moya/Combine (15.0.0), CombineExt (1.8.0)
Now I have a need to re -refresh the token when the token expires. After success, I need to re -execute the previous request again. The code is as follows:
However, after the success of the token, this code did not execute the original request. Does anyone know what happened in my code?
The text was updated successfully, but these errors were encountered: