title: Alamofire retry request - reactive way
body: <p>I was looking at those two:</p>
  
  <ul>
  <li><a href="http://sapandiwakar.in/refresh-oauth-tokens-using-moya-rxswift/" rel="nofollow noreferrer">http://sapandiwakar.in/refresh-oauth-tokens-using-moya-rxswift/</a></li>
  <li><a href="https://stackoverflow.com/questions/38281760/using-retrywhen-to-update-tokens-based-on-http-error-code">Using retryWhen to update tokens based on http error code</a></li>
  </ul>
  
  <p>And trying to create <em>similiar</em> thing, but without Moya, using Alamofire + RxSwift.</p>
  
  <p>First of all is obviously where should I stick this, since my implementation is divided into a couple smaller parts.
  First of all I have my custom method for generating <strong>reactive</strong> requests:</p>
  
  <pre><code>static func rx_request&lt;T&gt;(requestConvertible: URLRequestConvertible, completion: (Request) -&gt; Observable&lt;T&gt; ) -&gt; Observable&lt;T&gt; {
      let manager: Manager = Manager.sharedInstance
      return manager
          .rx_request { manager -&gt; Request in
              return Alamofire.request(requestConvertible)
          }
          .flatMap { request -&gt; Observable&lt;T&gt; in
              return completion(request)
          }
          .shareReplay(1)
  }
  </code></pre>
  
  <p>Which is later used by specific <code>Request</code>s convenience classes. For example my <code>UserRequests</code> has this private extension to extract some common code from it's methods:</p>
  
  <pre><code>private extension Request {
  
      func rx_userRequest() -&gt; Observable&lt;User&gt; {
          return self
              .validate()
              .rx_responseJSON()
              .flatMap{ (request, json) -&gt; Observable&lt;User&gt; in
                  guard
                      let dict = json as? [ String: AnyObject ],
                      let parsedUser: User = try? Unbox(dict) else {
                          return Observable.error(RequestError.ParsingError)
                  }
  
                  return Observable.just(parsedUser)
              }
              .rx_storeCredentials()
      }
  
  }
  </code></pre>
  
  <p>Because of how things looks like I wonder whare's the <em>right</em> place to put a <code>retry</code> method and also how to implement it? Because depending on the location I can get different input arguments.</p>