-
Notifications
You must be signed in to change notification settings - Fork 2
/
Extensions.swift
40 lines (37 loc) · 1.5 KB
/
Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// Extensions.swift
// raketa
//
// Created by Alexandr Mavrichev on 30.10.17.
//
import Foundation
import RxSwift
import Moya
import XCGLogger
extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response {
public func retryWithAuthIfNeeded() -> Single<ElementType> {
return retryWhen { e in
Observable.zip(e, Observable.range(start: 1, count: 3),
resultSelector: { $1 })
.flatMap { i -> PrimitiveSequence<SingleTrait, Access> in
return DataAPIProvider.rx
.request(.refreshToken(token: "YOUR REFRESH TOKEN"))
.filterSuccessfulStatusCodes()
.mapObject(Access.self)
.catchError { error in
if case MoyaError.statusCode(let response) = error {
if response.statusCode == 401 {
// Logout logic
}
}
return Single.error(error)
}.flatMap({ access -> PrimitiveSequence<SingleTrait, Access> in
XCGLogger.default.debug(access.accessToken)
XCGLogger.default.debug(access.refreshToken)
// Refresh your access and reresh token here
return Single.just(access)
})
}
}
}
}