mirror of
https://github.com/bitwarden/mobile
synced 2025-12-13 14:53:18 +00:00
Added icons for iOS. Broke out data access into repositories. Added syncing service.
This commit is contained in:
50
src/App/Repositories/BaseApiRepository.cs
Normal file
50
src/App/Repositories/BaseApiRepository.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.App.Models.Api;
|
||||
using ModernHttpClient;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.App.Repositories
|
||||
{
|
||||
public abstract class BaseApiRepository
|
||||
{
|
||||
public BaseApiRepository()
|
||||
{
|
||||
Client = new HttpClient(new NativeMessageHandler());
|
||||
Client.BaseAddress = new Uri("https://api.bitwarden.com");
|
||||
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
}
|
||||
|
||||
protected virtual HttpClient Client { get; private set; }
|
||||
protected abstract string ApiRoute { get; }
|
||||
|
||||
public async Task<ApiResult<T>> HandleErrorAsync<T>(HttpResponseMessage response)
|
||||
{
|
||||
try
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var errorResponseModel = JsonConvert.DeserializeObject<ErrorResponse>(responseContent);
|
||||
|
||||
var errors = new List<ApiError>();
|
||||
foreach(var valError in errorResponseModel.ValidationErrors)
|
||||
{
|
||||
foreach(var errorMessage in valError.Value)
|
||||
{
|
||||
errors.Add(new ApiError { Message = errorMessage });
|
||||
}
|
||||
}
|
||||
|
||||
return ApiResult<T>.Failed(response.StatusCode, errors.ToArray());
|
||||
}
|
||||
catch(JsonReaderException)
|
||||
{ }
|
||||
|
||||
return ApiResult<T>.Failed(response.StatusCode, new ApiError { Message = "An unknown error has occured." });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user