bitwarden-android/src/App/Repositories/SettingsApiRepository.cs

64 lines
2.2 KiB
C#
Raw Normal View History

2017-02-09 05:19:09 +03:00
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models.Api;
using Plugin.Connectivity.Abstractions;
using Newtonsoft.Json;
namespace Bit.App.Repositories
{
public class SettingsApiRepository : BaseApiRepository, ISettingsApiRepository
{
public SettingsApiRepository(
IConnectivity connectivity,
IHttpService httpService,
ITokenService tokenService)
: base(connectivity, httpService, tokenService)
{ }
protected override string ApiRoute => "settings";
2017-02-09 06:04:07 +03:00
public virtual async Task<ApiResult<DomainsResponse>> GetDomains(bool excluded = false)
2017-02-09 05:19:09 +03:00
{
if(!Connectivity.IsConnected)
{
2017-02-09 06:04:07 +03:00
return HandledNotConnected<DomainsResponse>();
2017-02-09 05:19:09 +03:00
}
2017-02-09 06:04:07 +03:00
var tokenStateResponse = await HandleTokenStateAsync<DomainsResponse>();
2017-02-09 05:19:09 +03:00
if(!tokenStateResponse.Succeeded)
{
return tokenStateResponse;
}
2017-05-07 03:20:57 +03:00
using(var client = HttpService.ApiClient)
2017-02-09 05:19:09 +03:00
{
var requestMessage = new TokenHttpRequestMessage()
{
Method = HttpMethod.Get,
RequestUri = new Uri(client.BaseAddress,
string.Concat(ApiRoute, "/domains?excluded=", excluded.ToString().ToLowerInvariant())),
};
try
{
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
2017-02-09 06:04:07 +03:00
return await HandleErrorAsync<DomainsResponse>(response).ConfigureAwait(false);
2017-02-09 05:19:09 +03:00
}
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
2017-02-09 06:04:07 +03:00
var responseObj = JsonConvert.DeserializeObject<DomainsResponse>(responseContent);
return ApiResult<DomainsResponse>.Success(responseObj, response.StatusCode);
2017-02-09 05:19:09 +03:00
}
catch
{
2017-02-09 06:04:07 +03:00
return HandledWebException<DomainsResponse>();
2017-02-09 05:19:09 +03:00
}
}
}
}
}