1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-26 12:16:07 +01:00

setup settings api repository

This commit is contained in:
Kyle Spearrin 2017-02-08 21:19:09 -05:00
parent 2d605f5dfb
commit 2c1ebc0439
6 changed files with 97 additions and 0 deletions

View File

@ -222,6 +222,7 @@ namespace Bit.Android
.RegisterType<IAccountsApiRepository, AccountsApiRepository>(new ContainerControlledLifetimeManager())
.RegisterType<ICipherApiRepository, CipherApiRepository>(new ContainerControlledLifetimeManager())
.RegisterType<ISettingsRepository, SettingsRepository>(new ContainerControlledLifetimeManager())
.RegisterType<ISettingsApiRepository, SettingsApiRepository>(new ContainerControlledLifetimeManager())
// Other
.RegisterInstance(CrossSettings.Current, new ContainerControlledLifetimeManager())
.RegisterInstance(CrossConnectivity.Current, new ContainerControlledLifetimeManager())

View File

@ -0,0 +1,11 @@
using System.Threading.Tasks;
using Bit.App.Models.Api;
using Bit.App.Models.Api.Response;
namespace Bit.App.Abstractions
{
public interface ISettingsApiRepository
{
Task<ApiResult<DomainsReponse>> GetDomains(bool excluded = false);
}
}

View File

@ -35,6 +35,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Abstractions\Repositories\ISettingsApiRepository.cs" />
<Compile Include="Abstractions\Repositories\IAccountsApiRepository.cs" />
<Compile Include="Abstractions\Repositories\IDeviceApiRepository.cs" />
<Compile Include="Abstractions\Repositories\ISettingsRepository.cs" />
@ -93,6 +94,7 @@
<Compile Include="Models\Api\Request\TokenRequest.cs" />
<Compile Include="Models\Api\Response\CipherHistoryResponse.cs" />
<Compile Include="Models\Api\Response\CipherResponse.cs" />
<Compile Include="Models\Api\Response\DomainsReponse.cs" />
<Compile Include="Models\Api\Response\ErrorResponse.cs" />
<Compile Include="Models\Api\Response\FolderResponse.cs" />
<Compile Include="Models\Api\Response\ListResponse.cs" />
@ -141,6 +143,7 @@
<Compile Include="Pages\Vault\VaultAutofillListLoginsPage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Abstractions\Repositories\ILoginRepository.cs" />
<Compile Include="Repositories\SettingsApiRepository.cs" />
<Compile Include="Repositories\ApiRepository.cs" />
<Compile Include="Repositories\AccountsApiRepository.cs" />
<Compile Include="Repositories\ConnectApiRepository.cs" />

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace Bit.App.Models.Api.Response
{
public class DomainsReponse
{
public IEnumerable<IEnumerable<string>> EquivalentDomains { get; set; }
public IEnumerable<GlobalDomains> GlobalEquivalentDomains { get; set; }
public class GlobalDomains
{
public byte Type { get; set; }
public IEnumerable<string> Domains { get; set; }
public bool Excluded { get; set; }
}
}
}

View File

@ -0,0 +1,64 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models.Api;
using Plugin.Connectivity.Abstractions;
using Bit.App.Models.Api.Response;
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";
public virtual async Task<ApiResult<DomainsReponse>> GetDomains(bool excluded = false)
{
if(!Connectivity.IsConnected)
{
return HandledNotConnected<DomainsReponse>();
}
var tokenStateResponse = await HandleTokenStateAsync<DomainsReponse>();
if(!tokenStateResponse.Succeeded)
{
return tokenStateResponse;
}
using(var client = HttpService.Client)
{
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)
{
return await HandleErrorAsync<DomainsReponse>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<DomainsReponse>(responseContent);
return ApiResult<DomainsReponse>.Success(responseObj, response.StatusCode);
}
catch
{
return HandledWebException<DomainsReponse>();
}
}
}
}
}

View File

@ -277,6 +277,7 @@ namespace Bit.iOS
.RegisterType<IAccountsApiRepository, AccountsApiRepository>(new ContainerControlledLifetimeManager())
.RegisterType<ICipherApiRepository, CipherApiRepository>(new ContainerControlledLifetimeManager())
.RegisterType<ISettingsRepository, SettingsRepository>(new ContainerControlledLifetimeManager())
.RegisterType<ISettingsApiRepository, SettingsApiRepository>(new ContainerControlledLifetimeManager())
// Other
.RegisterInstance(CrossConnectivity.Current, new ContainerControlledLifetimeManager())
.RegisterInstance(UserDialogs.Instance, new ContainerControlledLifetimeManager())