catch WebExceptions during API calls

This commit is contained in:
Kyle Spearrin 2016-08-06 19:33:04 -04:00
parent 98ceaba5f5
commit fe1545fbdf
7 changed files with 218 additions and 92 deletions

View file

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models.Api;
using Plugin.Connectivity.Abstractions;
using System.Net;
namespace Bit.App.Repositories
{
@ -30,6 +31,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/register")),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -38,6 +41,11 @@ namespace Bit.App.Repositories
return ApiResult.Success(response.StatusCode);
}
catch(WebException)
{
return HandledWebException();
}
}
}
public virtual async Task<ApiResult> PostPasswordHintAsync(PasswordHintRequest requestObj)
@ -55,6 +63,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/password-hint")),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -63,6 +73,11 @@ namespace Bit.App.Repositories
return ApiResult.Success(response.StatusCode);
}
catch(WebException)
{
return HandledWebException();
}
}
}
}
}

View file

@ -6,6 +6,7 @@ using Bit.App.Abstractions;
using Bit.App.Models.Api;
using Newtonsoft.Json;
using Plugin.Connectivity.Abstractions;
using System.Net;
namespace Bit.App.Repositories
{
@ -33,6 +34,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/", id)),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -43,6 +46,11 @@ namespace Bit.App.Repositories
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
}
catch(WebException)
{
return HandledWebException<TResponse>();
}
}
}
public virtual async Task<ApiResult<ListResponse<TResponse>>> GetAsync()
@ -60,6 +68,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, ApiRoute),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -70,6 +80,11 @@ namespace Bit.App.Repositories
var responseObj = JsonConvert.DeserializeObject<ListResponse<TResponse>>(responseContent);
return ApiResult<ListResponse<TResponse>>.Success(responseObj, response.StatusCode);
}
catch(WebException)
{
return HandledWebException<ListResponse<TResponse>>();
}
}
}
public virtual async Task<ApiResult<TResponse>> PostAsync(TRequest requestObj)
@ -87,6 +102,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, ApiRoute),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -97,6 +114,11 @@ namespace Bit.App.Repositories
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
}
catch(WebException)
{
return HandledWebException<TResponse>();
}
}
}
public virtual async Task<ApiResult<TResponse>> PutAsync(TId id, TRequest requestObj)
@ -114,6 +136,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/", id)),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -124,6 +148,11 @@ namespace Bit.App.Repositories
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
}
catch(WebException)
{
return HandledWebException<TResponse>();
}
}
}
public virtual async Task<ApiResult> DeleteAsync(TId id)
@ -141,6 +170,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/", id)),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -149,6 +180,11 @@ namespace Bit.App.Repositories
return ApiResult.Success(response.StatusCode);
}
catch(WebException)
{
return HandledWebException();
}
}
}
}
}

View file

@ -5,6 +5,7 @@ using Bit.App.Abstractions;
using Bit.App.Models.Api;
using Newtonsoft.Json;
using Plugin.Connectivity.Abstractions;
using System.Net;
namespace Bit.App.Repositories
{
@ -31,6 +32,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/token")),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -41,6 +44,11 @@ namespace Bit.App.Repositories
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
}
catch(WebException)
{
return HandledWebException<TokenResponse>();
}
}
}
public virtual async Task<ApiResult<TokenResponse>> PostTokenTwoFactorAsync(TokenTwoFactorRequest requestObj)
@ -58,6 +66,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/token/two-factor")),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -68,6 +78,11 @@ namespace Bit.App.Repositories
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
}
catch(WebException)
{
return HandledWebException<TokenResponse>();
}
}
}
}
}

View file

@ -28,6 +28,16 @@ namespace Bit.App.Repositories
return ApiResult<T>.Failed(System.Net.HttpStatusCode.RequestTimeout, new ApiError { Message = "Not connected to the internet." });
}
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." });
}
protected async Task<ApiResult<T>> HandleErrorAsync<T>(HttpResponseMessage response)
{
try

View file

@ -6,6 +6,7 @@ using Bit.App.Abstractions;
using Bit.App.Models.Api;
using Newtonsoft.Json;
using Plugin.Connectivity.Abstractions;
using System.Net;
namespace Bit.App.Repositories
{
@ -32,6 +33,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/", id)),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -42,6 +45,11 @@ namespace Bit.App.Repositories
var responseObj = JsonConvert.DeserializeObject<CipherResponse>(responseContent);
return ApiResult<CipherResponse>.Success(responseObj, response.StatusCode);
}
catch(WebException)
{
return HandledWebException<CipherResponse>();
}
}
}
public virtual async Task<ApiResult<ListResponse<CipherResponse>>> GetAsync()
@ -59,6 +67,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, ApiRoute),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -69,6 +79,11 @@ namespace Bit.App.Repositories
var responseObj = JsonConvert.DeserializeObject<ListResponse<CipherResponse>>(responseContent);
return ApiResult<ListResponse<CipherResponse>>.Success(responseObj, response.StatusCode);
}
catch(WebException)
{
return HandledWebException<ListResponse<CipherResponse>>();
}
}
}
public virtual async Task<ApiResult<CipherHistoryResponse>> GetByRevisionDateWithHistoryAsync(DateTime since)
@ -86,6 +101,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/history", "?since=", since)),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -96,6 +113,11 @@ namespace Bit.App.Repositories
var responseObj = JsonConvert.DeserializeObject<CipherHistoryResponse>(responseContent);
return ApiResult<CipherHistoryResponse>.Success(responseObj, response.StatusCode);
}
catch(WebException)
{
return HandledWebException<CipherHistoryResponse>();
}
}
}
}
}

View file

@ -5,6 +5,7 @@ using Bit.App.Abstractions;
using Bit.App.Models.Api;
using Newtonsoft.Json;
using Plugin.Connectivity.Abstractions;
using System.Net;
namespace Bit.App.Repositories
{
@ -31,6 +32,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/identifier/", identifier, "/token")),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -39,6 +42,11 @@ namespace Bit.App.Repositories
return ApiResult.Success(response.StatusCode);
}
catch(WebException)
{
return HandledWebException();
}
}
}
public virtual async Task<ApiResult> PutClearTokenAsync(string identifier)
@ -56,9 +64,21 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/identifier/", identifier, "/clear-token")),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync(response);
}
return ApiResult.Success(response.StatusCode);
}
catch(WebException)
{
return HandledWebException();
}
}
}
}
}

View file

@ -6,6 +6,7 @@ using Bit.App.Abstractions;
using Bit.App.Models.Api;
using Newtonsoft.Json;
using Plugin.Connectivity.Abstractions;
using System.Net;
namespace Bit.App.Repositories
{
@ -32,6 +33,8 @@ namespace Bit.App.Repositories
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "?since=", since)),
};
try
{
var response = await client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
@ -42,6 +45,11 @@ namespace Bit.App.Repositories
var responseObj = JsonConvert.DeserializeObject<ListResponse<FolderResponse>>(responseContent);
return ApiResult<ListResponse<FolderResponse>>.Success(responseObj, response.StatusCode);
}
catch(WebException)
{
return HandledWebException<ListResponse<FolderResponse>>();
}
}
}
}
}