bitwarden-android/src/App/Services/ApiService.cs

47 lines
1.4 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
2016-05-03 00:50:16 +03:00
using System.Collections.Generic;
2016-05-02 09:52:09 +03:00
using System.Net.Http;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models.Api;
using ModernHttpClient;
using Newtonsoft.Json;
namespace Bit.App.Services
{
public class ApiService : IApiService
{
public ApiService()
{
Client = new HttpClient(new NativeMessageHandler());
Client.BaseAddress = new Uri("https://api.bitwarden.com");
}
public HttpClient Client { get; set; }
public async Task<ApiResult<T>> HandleErrorAsync<T>(HttpResponseMessage response)
{
try
{
var responseContent = await response.Content.ReadAsStringAsync();
var errorResponseModel = JsonConvert.DeserializeObject<ErrorResponse>(responseContent);
2016-05-03 00:50:16 +03:00
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());
2016-05-02 09:52:09 +03:00
}
2016-05-03 00:50:16 +03:00
catch(JsonReaderException)
{ }
2016-05-02 09:52:09 +03:00
2016-05-03 00:50:16 +03:00
return ApiResult<T>.Failed(response.StatusCode, new ApiError { Message = "An unknown error has occured." });
2016-05-02 09:52:09 +03:00
}
}
}