How Re-authenticate with Web Service and then Retry REST Call
<p>I'm working with a REST API that returns a 401 if my authorization token has expired.  When I receive a 401, I'd like to run my authentication logic, retrieve a new token and then retry my original call.  What's the best way to do this.</p>

<p>Right now, I have an Authenticator class that "knows" how to authenticate with the API.  The rest of the data access logic lives in a Repository object.  The Repository object has the responsibility of sending requests to the API to retrieve information, using the information stored in the Authenticator.</p>

<p>An example of this is Repository.List() [It's not really static, just writing it this way for brevity).  Conceptually, this is what List() should do.</p>

<ul>
<li>Try to connect to API and get a list of items</li>
<li>If 401 error, re-authenticate and try again</li>
<li>Return the list of items or throw an exception</li>
</ul>

<p>This pattern will be used for all of my methods in all of my repositories, so I'd like a delegate or something that I could use with all of the API calls.</p>

<p>Any ideas?</p>

<p>Thanks,
Greg</p>
