-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerisureRetryHandler.cs
39 lines (37 loc) · 1.57 KB
/
VerisureRetryHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Verisure.GraphQL.Data;
namespace Verisure.GraphQL {
public class VerisureRetryHandler : DelegatingHandler {
private readonly VerisureClient client;
public VerisureRetryHandler(HttpClientHandler innerHandler, VerisureClient client) : base(innerHandler) {
this.client = client;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
HttpResponseMessage response = null;
for (int i = 0; i < 5; i++) {
response = await base.SendAsync(request, cancellationToken);
if (response.IsSuccessStatusCode) {
return response;
}
else if (response.StatusCode == HttpStatusCode.Unauthorized) {
if (client.cookieContainer.GetCookies(client.ApiUrl).Any(x => x.Name == "vs-refresh")) {
await client.RefreshTokenAsync();
response = await base.SendAsync(request, cancellationToken);
if (response.IsSuccessStatusCode) {
return response;
}
}
else {
throw new HttpRequestException("No RefreshToken found, unable to reauthenticate. Have you logged in?");
}
}
}
return response;
}
}
}