mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 15:15:34 +03:00
catch WebExceptions during API calls
This commit is contained in:
parent
98ceaba5f5
commit
fe1545fbdf
7 changed files with 218 additions and 92 deletions
|
@ -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,13 +31,20 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/register")),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync(response);
|
||||
}
|
||||
|
||||
return ApiResult.Success(response.StatusCode);
|
||||
return ApiResult.Success(response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,13 +63,20 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/password-hint")),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync(response);
|
||||
}
|
||||
|
||||
return ApiResult.Success(response.StatusCode);
|
||||
return ApiResult.Success(response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,15 +34,22 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/", id)),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync<TResponse>(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<TResponse>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
|
||||
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
|
||||
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException<TResponse>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,15 +68,22 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, ApiRoute),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync<ListResponse<TResponse>>(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<ListResponse<TResponse>>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<ListResponse<TResponse>>(responseContent);
|
||||
return ApiResult<ListResponse<TResponse>>.Success(responseObj, response.StatusCode);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<ListResponse<TResponse>>(responseContent);
|
||||
return ApiResult<ListResponse<TResponse>>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException<ListResponse<TResponse>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,15 +102,22 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, ApiRoute),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync<TResponse>(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<TResponse>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
|
||||
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
|
||||
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException<TResponse>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,15 +136,22 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/", id)),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync<TResponse>(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<TResponse>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
|
||||
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
|
||||
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException<TResponse>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -141,13 +170,20 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/", id)),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync(response);
|
||||
}
|
||||
|
||||
return ApiResult.Success(response.StatusCode);
|
||||
return ApiResult.Success(response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,15 +32,22 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/token")),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync<TokenResponse>(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<TokenResponse>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
|
||||
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
|
||||
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException<TokenResponse>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,15 +66,22 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/token/two-factor")),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync<TokenResponse>(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<TokenResponse>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
|
||||
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
|
||||
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException<TokenResponse>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,15 +33,22 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/", id)),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync<CipherResponse>(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<CipherResponse>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<CipherResponse>(responseContent);
|
||||
return ApiResult<CipherResponse>.Success(responseObj, response.StatusCode);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<CipherResponse>(responseContent);
|
||||
return ApiResult<CipherResponse>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException<CipherResponse>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,15 +67,22 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, ApiRoute),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync<ListResponse<CipherResponse>>(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<ListResponse<CipherResponse>>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<ListResponse<CipherResponse>>(responseContent);
|
||||
return ApiResult<ListResponse<CipherResponse>>.Success(responseObj, response.StatusCode);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<ListResponse<CipherResponse>>(responseContent);
|
||||
return ApiResult<ListResponse<CipherResponse>>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException<ListResponse<CipherResponse>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,15 +101,22 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/history", "?since=", since)),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync<CipherHistoryResponse>(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<CipherHistoryResponse>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<CipherHistoryResponse>(responseContent);
|
||||
return ApiResult<CipherHistoryResponse>.Success(responseObj, response.StatusCode);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<CipherHistoryResponse>(responseContent);
|
||||
return ApiResult<CipherHistoryResponse>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException<CipherHistoryResponse>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,13 +32,20 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/identifier/", identifier, "/token")),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync(response);
|
||||
}
|
||||
|
||||
return ApiResult.Success(response.StatusCode);
|
||||
return ApiResult.Success(response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,8 +64,20 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/identifier/", identifier, "/clear-token")),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
return ApiResult.Success(response.StatusCode);
|
||||
try
|
||||
{
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync(response);
|
||||
}
|
||||
|
||||
return ApiResult.Success(response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,15 +33,22 @@ namespace Bit.App.Repositories
|
|||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "?since=", since)),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
return await HandleErrorAsync<ListResponse<FolderResponse>>(response);
|
||||
}
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<ListResponse<FolderResponse>>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<ListResponse<FolderResponse>>(responseContent);
|
||||
return ApiResult<ListResponse<FolderResponse>>.Success(responseObj, response.StatusCode);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<ListResponse<FolderResponse>>(responseContent);
|
||||
return ApiResult<ListResponse<FolderResponse>>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
return HandledWebException<ListResponse<FolderResponse>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue