我遇到了以下错误:
Cannot convert value of type '(_) -> Observable<Response>'
to expected argument type '([_]) -> _'
当我尝试编译时:
func request(_ token: MyAPI) -> Observable<Moya.Response> {
switch (target.requiresCSRF, target.requiresOAuth) {
case (false, false): return actualRequest
case (true, false): return CSRFTokenRequest().flatMap { _ in actualRequest }
case (false, true): return OAuthTokenRequest().flatMap { _ in actualRequest }
case (true, true): return Observable.zip([CSRFTokenRequest(), OAuthTokenRequest()]) { _ in actualRequest }
}
}
actualRequest 的类型Observable<Response>
为 CSRFTokenRequest 和 OAuthTokenRequest 的类型均为Observable<String?>
我试图在OAuthTokenRequest之前运行CSRFTokenRequest,然后是实际请求。
以下解决了这个问题......但我不确定这是否是正确的方法......
return RequiresCSRFTokenRequest().flatMap{ _ in
self.RequiresAuthenticationRequest().flatMap{ _ in actualRequest}
}
fileprivate extension Networking{
func RequiresAuthenticationRequest() -> Observable<String> {
/// This has 3 cases
// 0. token is not present, error out
// 1. token is not expired, return token
// 2. token is expired, needs refresh
guard let jwt = AuthManager.shared.accessToken else{
return .just("Error!! No access tokens")
}
// If access token is valid
if AuthManager.shared.expiredAccessToken == false{
return .just(jwt)
}else{
return request(.refreshAccessToken(refreshToken: AuthManager.shared.refreshTokenWithBearer!))
.filterSuccessfulStatusCodes()
.mapObject(type: UserAuthenticationTokens.self)
.do(onNext: {
$0.save()})
.map{ (token) -> String in
// Get new access token that was just saved
return AuthManager.shared.accessToken!
}
}
}
func RequiresCSRFTokenRequest() -> Observable<String> {
// Always get a new csrf token
return request(.getCSRF())
.filterSuccessfulStatusCodes()
.mapObject(type: CSRFToken.self)
.do(onNext: {
$0.save()
}).map{ (token) -> String in
return AuthManager.shared.csrf_token!
}
}
}
谢谢!