title: Refresh token and retry the request using RxSwift
body: <p>I receive from my backend a accessToken and a refreshToken. When I receive a respons with statusCode 401 I need to replace the accessToken with the refreshToken (accessToken = refreshToken) and try the request again. If the request fail for the third time I need to request the user to log in again. I'm trying to apply this logic using the code described bellow, but it isn't working. The request is not being called again. How can I do this?</p>
  
  <pre><code>// example of usage:
  public func login(with parameters: LoginParameters) -&gt; Single&lt;User&gt; {
          return provider.rx.request(MultiTarget(UserTarget.login(parameters))).handleResponse(User.self, using: jsonDecoder)
      }
  
  func handleResponse&lt;D: Decodable&gt;(_ type: D.Type, atKeyPath keyPath: String? = nil, using decoder: JSONDecoder = JSONDecoder(), failsOnEmptyData: Bool = true) -&gt; Single&lt;D&gt; {
          return self.flatMap({ (response) -&gt; Single&lt;D&gt; in
  
              switch response.statusCode {
              case 200...299:
                  RequestsFailedCount.count = 0
                  return Single.just(try response.map(type, atKeyPath: keyPath, using: decoder, failsOnEmptyData: failsOnEmptyData))
              case 500:
                  return Single.error(try response.map(ResponseError.self))
              default:
                  if response.statusCode == 401, let urlResponse = response.response {
  
                      guard RequestsFailedCount.count &lt;= 3 else {
                          RequestsFailedCount.count = 0
                          return Single.error(InvalidChallengeError())
                      }
  
                      RequestsFailedCount.count += 1
                      if let challenge = urlResponse.allHeaderFields[HeaderType.authenticate.rawValue] as? String, challenge.contains(self.challenge) {
                          var keychain = KeychainManager()
                          keychain.accessToken = keychain.refreshToken
  
                          return self.retry(1).map(type, atKeyPath: keyPath, using: decoder, failsOnEmptyData: failsOnEmptyData)
                      } else {
                          return Single.error(try response.map(ResponseError.self))
                      }
  
                  } else {
                      return Single.error(try response.map(ResponseError.self))
                  }
              }
          })
      }
  </code></pre>