Sending a HttpRequest Message, and retrying in case of unauthorised This code simply sends a HttpRequest message and, if it comes back Unauthorized, gets a new login and tries again.

This code is meant to deal with the moment a JSON Web Token goes out of date. In this system the client is notified of this by having its request become Unauthorized, it then picks up a new Token by logging on again. Hiding away that Unauthorized code, it continues to message.

Two things to note:


A HttpRequestMessage has to be recreated every time it is used.
I have a need to recreate the client to pick up the new JWT token 


(Edit: Actually can just go in and change some properties for client, I assume it would be worth changing?)

Basically my question is: How unpleasant is my use of delegates and what lengths should I go to change this code, if any?

private async Task&lt;HttpResponseMessage&gt; SendToFooAsync(Func&lt;HttpClient&gt; client, Func&lt;HttpRequestMessage&gt; requestMessage)
{
    var response = await client().SendAsync(requestMessage());

    if (response.StatusCode == HttpStatusCode.Unauthorized)
    {
        //Re-authenticate, recreate client with new token, and retry
        if (await authService.LoginAsync())
        {
            response = await client().SendAsync(requestMessage());
            Log.Log("Unauthorized code received: Re-Authentication attempt succeeded");
        }
        else
        {
            Log.Log("Unauthorized code received: Authentication attempt has failed");
        }
    }
    return response;
}


Simplest code that calls it:

HttpResponseMessage result = await SendToFooAsync(InitialiseHttpClient, () =&gt; InitialiseRequest(HttpMethod.Post, path));


Most complex code that calls it

Func&lt;HttpClient&gt; client = () =&gt;
            {
                var httpClient = InitialiseHttpClient();
                httpClient.DefaultRequestHeaders.Accept.Add("some json stuff");
                return httpClient;
            };

HttpResponseMessage result = await SendToFooAsync(client, () =&gt;
{
    var message = InitialiseRequest(HttpMethod.Post, path);
    message.Content = new StringContent("some json stuff");
    return message;
});


Where InitialiseRequest and LoginAsync are my own methods outside the scope of this review. Hopefully their use is obvious here.
How unpleasant is my use of delegates and what lengths should I go to change this code, if any?


If you want the caller to provide some small piece of code, then a delegate is exactly the right way to do it, it's not unpleasant at all.

But looking at your two usage samples, it looks like they have some code in common (calling InitialiseHttpClient and InitialiseRequest), so it might make sense to change the signature to something like:

private async Task&lt;HttpResponseMessage&gt; SendToFooAsync(
    HttpMethod method, string uri,
    Action&lt;HttpClient&gt; clientAction = null,
    Action&lt;HttpRequestMessage&gt; requestMessageAction = null)


The two usage samples would then look like this:

SendToFooAsync(HttpMethod.Post, path);

SendToFooAsync(
    HttpMethod.Post, path,
    client =&gt; client.DefaultRequestHeaders.Accept.Add("some json stuff"),
    message =&gt; message.Content = new StringContent("some json stuff"));


Also, I don't like the way you're naming your delegates. Func&lt;HttpClient&gt; is not HttpClient, so it shouldn't be called just client. Something like clientFunc would be more accurate.