2016-06-18 23:10:09 +03:00
|
|
|
|
using System;
|
2016-06-22 05:29:29 +03:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-06-18 23:10:09 +03:00
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Bit.App.Models.Api;
|
2016-06-22 05:29:29 +03:00
|
|
|
|
using Newtonsoft.Json;
|
2016-07-02 01:54:00 +03:00
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
2016-08-07 02:33:04 +03:00
|
|
|
|
using System.Net;
|
2016-06-18 23:10:09 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Repositories
|
|
|
|
|
{
|
|
|
|
|
public class DeviceApiRepository : ApiRepository<DeviceRequest, DeviceResponse, string>, IDeviceApiRepository
|
|
|
|
|
{
|
2016-12-24 18:54:18 +03:00
|
|
|
|
public DeviceApiRepository(
|
|
|
|
|
IConnectivity connectivity,
|
2017-02-05 07:31:37 +03:00
|
|
|
|
IHttpService httpService,
|
|
|
|
|
ITokenService tokenService)
|
|
|
|
|
: base(connectivity, httpService, tokenService)
|
2016-07-02 01:54:00 +03:00
|
|
|
|
{ }
|
|
|
|
|
|
2017-10-04 06:56:10 +03:00
|
|
|
|
protected override string ApiRoute => "/devices";
|
2016-06-22 05:29:29 +03:00
|
|
|
|
|
2016-08-07 01:45:23 +03:00
|
|
|
|
public virtual async Task<ApiResult> PutTokenAsync(string identifier, DeviceTokenRequest request)
|
2016-06-22 05:29:29 +03:00
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
if(!Connectivity.IsConnected)
|
2016-06-22 05:29:29 +03:00
|
|
|
|
{
|
2016-08-07 01:45:23 +03:00
|
|
|
|
return HandledNotConnected();
|
2016-07-02 01:54:00 +03:00
|
|
|
|
}
|
2017-02-05 07:31:37 +03:00
|
|
|
|
|
|
|
|
|
var tokenStateResponse = await HandleTokenStateAsync();
|
|
|
|
|
if(!tokenStateResponse.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
return tokenStateResponse;
|
|
|
|
|
}
|
2016-06-22 05:29:29 +03:00
|
|
|
|
|
2017-05-07 03:20:57 +03:00
|
|
|
|
using(var client = HttpService.ApiClient)
|
2016-06-22 05:29:29 +03:00
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
var requestMessage = new TokenHttpRequestMessage(request)
|
|
|
|
|
{
|
|
|
|
|
Method = HttpMethod.Put,
|
2017-08-11 21:24:32 +03:00
|
|
|
|
RequestUri = new Uri(string.Concat(client.BaseAddress, ApiRoute, "/identifier/", identifier, "/token")),
|
2016-07-02 01:54:00 +03:00
|
|
|
|
};
|
2016-06-22 05:29:29 +03:00
|
|
|
|
|
2016-08-07 02:33:04 +03:00
|
|
|
|
try
|
2016-07-02 01:54:00 +03:00
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
|
2016-08-07 02:33:04 +03:00
|
|
|
|
if(!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
return await HandleErrorAsync(response).ConfigureAwait(false);
|
2016-08-07 02:33:04 +03:00
|
|
|
|
}
|
2016-07-02 01:54:00 +03:00
|
|
|
|
|
2016-08-07 02:33:04 +03:00
|
|
|
|
return ApiResult.Success(response.StatusCode);
|
|
|
|
|
}
|
2017-02-06 17:55:35 +03:00
|
|
|
|
catch
|
2016-08-07 02:33:04 +03:00
|
|
|
|
{
|
|
|
|
|
return HandledWebException();
|
|
|
|
|
}
|
2016-07-02 01:54:00 +03:00
|
|
|
|
}
|
2016-06-22 05:29:29 +03:00
|
|
|
|
}
|
2016-08-06 07:41:00 +03:00
|
|
|
|
|
2016-08-06 22:21:59 +03:00
|
|
|
|
public virtual async Task<ApiResult> PutClearTokenAsync(string identifier)
|
2016-08-06 07:41:00 +03:00
|
|
|
|
{
|
|
|
|
|
if(!Connectivity.IsConnected)
|
|
|
|
|
{
|
2016-08-06 22:21:59 +03:00
|
|
|
|
return HandledNotConnected();
|
2016-08-06 07:41:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-07 03:20:57 +03:00
|
|
|
|
using(var client = HttpService.ApiClient)
|
2016-08-06 07:41:00 +03:00
|
|
|
|
{
|
|
|
|
|
var requestMessage = new TokenHttpRequestMessage
|
|
|
|
|
{
|
|
|
|
|
Method = HttpMethod.Put,
|
2017-08-11 21:24:32 +03:00
|
|
|
|
RequestUri = new Uri(
|
|
|
|
|
string.Concat(client.BaseAddress, ApiRoute, "/identifier/", identifier, "/clear-token"))
|
2016-08-06 07:41:00 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-08-07 02:33:04 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
|
2016-08-07 02:33:04 +03:00
|
|
|
|
if(!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
return await HandleErrorAsync(response).ConfigureAwait(false);
|
2016-08-07 02:33:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ApiResult.Success(response.StatusCode);
|
|
|
|
|
}
|
2017-02-06 17:55:35 +03:00
|
|
|
|
catch
|
2016-08-07 02:33:04 +03:00
|
|
|
|
{
|
|
|
|
|
return HandledWebException();
|
|
|
|
|
}
|
2016-08-06 07:41:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-18 23:10:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|