I’m trying to extract the token verification to a seperate method and after that the code block in the else statement I would likewise want to make generic.
public void Subscribe(string s1, string s2, string s3)
{
if (token == null || token.IsExpired)
{
RestClient client = CreateRestClient();
RestRequest treq = CreateTokenRequest();
client.ExecuteAsync(treq, (response) =>
{
CreateNewToken(response.Content);
SubscribeToNotifications(s1, s2, s3);
});
}
else
{
var c = new RestClient(ServiceAddress);
var r = new RestRequest("Subscribe?");
r.AddParameter("P1", s1);
r.AddParameter("P2", s2);
r.AddParameter("P3", s3);
r.AddHeader("Authorization", token.TokenString);
c.ExecuteAsync(r, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
SubcribeCompleted(this, new GeneralEventArgs(true, string.Empty));
else if (response.StatusCode == HttpStatusCode.Unauthorized)
SubcribeCompleted(this, new GeneralEventArgs(false, "Unauthorized!"));
else
SubcribeCompleted(this, new GeneralEventArgs(false, "Error"));
});
}
}
The problem is how do I specify which method to call in the callback handler?
I have tried with Func<> but the other methods where I would use the generic token method do not necessarily has the same signature.
Thanks in advance for any input 🙂
Action<RestResponse>is the type ofRestClient.ExecuteAsync.Func<>is used when there is a return value, since there is none,Action<>is used.