我正在使用Moya与我的 API 进行通信。对于我的许多端点,我要求对用户进行身份验证(即不记名令牌基于 Authorization 标头)。
在此处的 Moya 文档中,我找到了如何包含 Authorization 标头以及不记名令牌。
但是,我现在需要实现身份验证令牌刷新,但我不确定如何执行此操作。
我在 Moya 的 Github 上找到了这个线程,其答案看起来可能会有所帮助,但我不知道将代码放在哪里。答案的代码如下所示:
// (Endpoint<Target>, NSURLRequest -> Void) -> Void
static func endpointResolver<T>() -> MoyaProvider<T>.RequestClosure where T: TargetType {
return { (endpoint, closure) in
let request = endpoint.urlRequest!
request.httpShouldHandleCookies = false
if (tokenIsOK) {
// Token is valid, so just resume the request and let AccessTokenPlugin set the Authentication header
closure(.success(request))
return
}
// authenticationProvider is a MoyaProvider<Authentication> for example
authenticationProvider.request(.refreshToken(params)) …Run Code Online (Sandbox Code Playgroud)