pcut_uwp/PCUT/Http.Core/Handlers/DeviceAuthHandler.cs

30 lines
1.1 KiB
C#

using Http.Core.Contexts;
using Http.Core.Exceptions;
using Http.Core.Extensions;
using PCUT.Entities.ApiResponse;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Http.Core.Handlers
{
public class DeviceAuthHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Add("x-device-id", UserContext.Instance.DeviceId);
request.Headers.Add("x-version-id", UserContext.Instance.AppVersion);
var response = await base.SendAsync(request, cancellationToken);
if (response.StatusCode == System.Net.HttpStatusCode.Conflict && response.Content.Headers.ContentLength > 0)
{
var content = await response.DeserializeObjectAsync<MessageResponse>(ensureSuccess: false);
throw new AppOutdatedException(content.ErrorMessage);
}
return response;
}
}
}