刷新令牌并使用RxSwift重试请求

时间:2019-06-03 14:20:36

标签: swift rx-swift moya

我从后端收到一个accessToken和refreshToken。当我收到带有statusCode 401的响应时,我需要用refreshToken(accessToken = refreshToken)替换accessToken并重试该请求。如果请求第三次失败,则需要请求用户再次登录。我正在尝试使用下面描述的代码来应用此逻辑,但是它不起作用。该请求不会再次被调用。我该怎么办?

// example of usage:
public func login(with parameters: LoginParameters) -> Single<User> {
        return provider.rx.request(MultiTarget(UserTarget.login(parameters))).handleResponse(User.self, using: jsonDecoder)
    }

func handleResponse<D: Decodable>(_ type: D.Type, atKeyPath keyPath: String? = nil, using decoder: JSONDecoder = JSONDecoder(), failsOnEmptyData: Bool = true) -> Single<D> {
        return self.flatMap({ (response) -> Single<D> in

            switch response.statusCode {
            case 200...299:
                RequestsFailedCount.count = 0
                return Single.just(try response.map(type, atKeyPath: keyPath, using: decoder, failsOnEmptyData: failsOnEmptyData))
            case 500:
                return Single.error(try response.map(ResponseError.self))
            default:
                if response.statusCode == 401, let urlResponse = response.response {

                    guard RequestsFailedCount.count <= 3 else {
                        RequestsFailedCount.count = 0
                        return Single.error(InvalidChallengeError())
                    }

                    RequestsFailedCount.count += 1
                    if let challenge = urlResponse.allHeaderFields[HeaderType.authenticate.rawValue] as? String, challenge.contains(self.challenge) {
                        var keychain = KeychainManager()
                        keychain.accessToken = keychain.refreshToken

                        return self.retry(1).map(type, atKeyPath: keyPath, using: decoder, failsOnEmptyData: failsOnEmptyData)
                    } else {
                        return Single.error(try response.map(ResponseError.self))
                    }

                } else {
                    return Single.error(try response.map(ResponseError.self))
                }
            }
        })
    }

1 个答案:

答案 0 :(得分:0)

这里有一篇文章,其中包含指向应帮助您的代码的链接:https://medium.com/@danielt1263/retrying-a-network-request-despite-having-an-invalid-token-b8b89340d29

结果是,您需要创建一个令牌获取服务,该服务将保留当前令牌并插入网络调用链以在必要时更新令牌...

它会这样使用:

func getData<T>(response: @escaping Response, tokenAcquisitionService: TokenAcquisitionService<T>, request: @escaping (T) throws -> URLRequest) -> Observable<(response: HTTPURLResponse, data: Data)> {
    return Observable
        .deferred { tokenAcquisitionService.token.take(1) } // gets the current token
        .map { try request($0) } // creates the request using the current token
        .flatMap { response($0) } // gets response from server
        .map { response in
            guard response.response.statusCode != 401 else { throw TokenAcquisitionError.unauthorized }
            return response
        }
        .retryWhen { $0.renewToken(with: tokenAcquisitionService) }
}

tokenAcquisitionService会收到未经授权的错误,并尝试从服务器获取新的令牌。如果成功,它将重试该链。

请注意,即使您同时有多个请求在运行,该服务也可以正常运行,并且其中包含一个测试工具,证明其可以正常工作。