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.Models.Api;
|
|
|
|
|
using Newtonsoft.Json;
|
2016-07-02 01:54:00 +03:00
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-06 07:17:38 +03:00
|
|
|
|
namespace Bit.App.Repositories
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
public abstract class BaseApiRepository
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
public BaseApiRepository(IConnectivity connectivity)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
Connectivity = connectivity;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-02 01:54:00 +03:00
|
|
|
|
protected IConnectivity Connectivity { get; private set; }
|
2016-05-06 07:17:38 +03:00
|
|
|
|
protected abstract string ApiRoute { get; }
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-07-02 01:54:00 +03:00
|
|
|
|
protected ApiResult HandledNotConnected()
|
|
|
|
|
{
|
|
|
|
|
return ApiResult.Failed(System.Net.HttpStatusCode.RequestTimeout, new ApiError { Message = "Not connected to the internet." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected ApiResult<T> HandledNotConnected<T>()
|
|
|
|
|
{
|
|
|
|
|
return ApiResult<T>.Failed(System.Net.HttpStatusCode.RequestTimeout, new ApiError { Message = "Not connected to the internet." });
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-07 02:33:04 +03:00
|
|
|
|
protected ApiResult HandledWebException()
|
|
|
|
|
{
|
|
|
|
|
return ApiResult.Failed(System.Net.HttpStatusCode.BadGateway, new ApiError { Message = "There is a problem connecting to the server." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected ApiResult<T> HandledWebException<T>()
|
|
|
|
|
{
|
|
|
|
|
return ApiResult<T>.Failed(System.Net.HttpStatusCode.BadGateway, new ApiError { Message = "There is a problem connecting to the server." });
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-02 01:54:00 +03:00
|
|
|
|
protected async Task<ApiResult<T>> HandleErrorAsync<T>(HttpResponseMessage response)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-06-26 03:54:17 +03:00
|
|
|
|
var errors = await ParseErrorsAsync(response);
|
2016-05-03 00:50:16 +03:00
|
|
|
|
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
|
|
|
|
}
|
2016-06-26 03:54:17 +03:00
|
|
|
|
|
2016-07-02 01:54:00 +03:00
|
|
|
|
protected async Task<ApiResult> HandleErrorAsync(HttpResponseMessage response)
|
2016-06-26 03:54:17 +03:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var errors = await ParseErrorsAsync(response);
|
|
|
|
|
return ApiResult.Failed(response.StatusCode, errors.ToArray());
|
|
|
|
|
}
|
|
|
|
|
catch(JsonReaderException)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
return ApiResult.Failed(response.StatusCode, new ApiError { Message = "An unknown error has occured." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<List<ApiError>> ParseErrorsAsync(HttpResponseMessage response)
|
|
|
|
|
{
|
|
|
|
|
var errors = new List<ApiError>();
|
|
|
|
|
if(response.StatusCode == System.Net.HttpStatusCode.BadRequest)
|
|
|
|
|
{
|
|
|
|
|
var responseContent = await response.Content.ReadAsStringAsync();
|
|
|
|
|
var errorResponseModel = JsonConvert.DeserializeObject<ErrorResponse>(responseContent);
|
|
|
|
|
|
|
|
|
|
foreach(var valError in errorResponseModel.ValidationErrors)
|
|
|
|
|
{
|
|
|
|
|
foreach(var errorMessage in valError.Value)
|
|
|
|
|
{
|
|
|
|
|
errors.Add(new ApiError { Message = errorMessage });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|