Search code examples
iosrx-swift

RxSwift - Recursive Observables?


Learning RxSwift - Here's my Problem:

i have a webservice that fetches data using an active access token, whenever the token expired , then first call the token generate api and then call the current request to run again. so that it will have an active access token to valid results.

but i have problem in getting the response for token and then call the prev. request?

so i tried adding an observable request , then in response check if the token is invalid, then call another observable to return an active token, once token is received , call the older request again.

func apirequest(_ urlConvertible:URLRequestConvertible) -> Observable<[String:AnyObject]> {
  return Observable.create({ observer  -> Disposable in
         let _ = Alamofire.request(urlConvertible).responseJSON  
                      { response in 
         if isTokenExpired() {
             self.generateToken().subscribe(onNext: response {
               self.apirequest(oldRequest)
          })
        }
      }
      return Disposables.create()
   })
}

i was expecting like any Rx operators or any ideas to try?

Thanks


Solution

  • I wrote an article about how to do this: https://medium.com/@danielt1263/retrying-a-network-request-despite-having-an-invalid-token-b8b89340d29

    Wrap your network calling code in something like this:

    /// Builds and makes network requests using the token provided by the service. Will request a new token and retry if the result is an unauthorized (401) error.
    ///
    /// - Parameters:
    ///   - response: A function that sends requests to the network and emits responses. Can be for example `URLSession.shared.rx.response`
    ///   - tokenAcquisitionService: The object responsible for tracking the auth token. All requests should use the same object.
    ///   - request: A function that can build the request when given a token.
    ///   - Returns: response of a guaranteed authorized network request.
    public func getData<T>(response: @escaping (URLRequest) -> Observable<(response: HTTPURLResponse, data: Data)>, tokenAcquisitionService: TokenAcquisitionService<T>, request: @escaping (T) throws -> URLRequest) -> Observable<(response: HTTPURLResponse, data: Data)> {
    return Observable
            .deferred { tokenAcquisitionService.token.take(1) }
            .map { try request($0) }
            .flatMap { response($0) }
            .map { response in
                guard response.response.statusCode != 401 else { throw TokenAcquisitionError.unauthorized }
                return response
            }
            .retryWhen { $0.renewToken(with: tokenAcquisitionService) }
    }
    

    The article and accompanying gist shows how to write the tokenAcquisitionService and includes unit tests.