rootstrap / ios-base

Boilerplate for new iOS projects using Swift 5. Provides a handful of functionalities.
https://rootstrap.com/
MIT License
258 stars 63 forks source link

Retry a request in Moya with RxSwift? #150

Closed prateek3255 closed 3 years ago

prateek3255 commented 4 years ago

I am pretty new to RxSwift and I am using the code from the branch mvvm+rxSwift, which has the following function for a request of given ProviderType -

    open func request<T>(for target: ProviderType,
                         at keyPath: String? = nil
                         ) -> Observable<(T, Response)> where T: Codable {
        return provider.rx.request(target)
        .filterSuccessfulStatusCodes()
        .flatMap { [weak self] response in
          let decodedValue = try response.map(T.self,
                                              atKeyPath: keyPath,
                                              using: self?.jsonDecoder ?? JSONDecoder(),
                                              failsOnEmptyData: true)
          return .just((decodedValue, response))
        }
        .asObservable()
        .catchError { [weak self] error in
          guard let self = self else {
            return Observable.error(error)
          }
          return Observable.error(self.handleError(with: error))
        }
    }

This works pretty well, but now I want to retry the request when the request fails with a status code of 401, after fetching the refresh token. I found this comment on a GitHub issue which tells how it can be achieved but I am still confused how it will work in my case (the comment does not provide context related to how the refresh token mechanism works).

germanStabile commented 4 years ago

Hi @prateek3255, thanks for the detailed issue.

Not very into RxSwift myself, but my guess would be to capture 401 calls on catchError, call your refresh token endpoint, and retry the failing call if refresh is successful.

I think it could be helpful to demonstrate this kind of behavior in the ios-base, so we may update on this issue.

glm4 commented 3 years ago

Closing due to the removal of Moya dependency