1
0
mirror of https://github.com/bitwarden/mobile synced 2026-01-03 00:53:27 +00:00

Await async calls after loading. Added ConfigureAwaits throughout API repositories. Formatting.

This commit is contained in:
Kyle Spearrin
2016-08-16 19:20:41 -04:00
parent 83bcd39791
commit 2c05c9595b
16 changed files with 126 additions and 109 deletions

View File

@@ -33,10 +33,10 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync(response);
return await HandleErrorAsync(response).ConfigureAwait(false);
}
return ApiResult.Success(response.StatusCode);
@@ -65,10 +65,10 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync(response);
return await HandleErrorAsync(response).ConfigureAwait(false);
}
return ApiResult.Success(response.StatusCode);

View File

@@ -36,13 +36,13 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync<TResponse>(response);
return await HandleErrorAsync<TResponse>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
}
@@ -70,13 +70,13 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync<ListResponse<TResponse>>(response);
return await HandleErrorAsync<ListResponse<TResponse>>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<ListResponse<TResponse>>(responseContent);
return ApiResult<ListResponse<TResponse>>.Success(responseObj, response.StatusCode);
}
@@ -104,13 +104,13 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync<TResponse>(response);
return await HandleErrorAsync<TResponse>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
}
@@ -138,13 +138,13 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync<TResponse>(response);
return await HandleErrorAsync<TResponse>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<TResponse>(responseContent);
return ApiResult<TResponse>.Success(responseObj, response.StatusCode);
}
@@ -172,10 +172,10 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync(response);
return await HandleErrorAsync(response).ConfigureAwait(false);
}
return ApiResult.Success(response.StatusCode);

View File

@@ -34,13 +34,13 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync<TokenResponse>(response);
return await HandleErrorAsync<TokenResponse>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
}
@@ -68,13 +68,13 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync<TokenResponse>(response);
return await HandleErrorAsync<TokenResponse>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
}

View File

@@ -20,48 +20,54 @@ namespace Bit.App.Repositories
protected ApiResult HandledNotConnected()
{
return ApiResult.Failed(System.Net.HttpStatusCode.RequestTimeout, new ApiError { Message = "Not connected to the internet." });
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." });
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." });
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." });
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
{
var errors = await ParseErrorsAsync(response);
var errors = await ParseErrorsAsync(response).ConfigureAwait(false);
return ApiResult<T>.Failed(response.StatusCode, errors.ToArray());
}
catch(JsonReaderException)
{ }
return ApiResult<T>.Failed(response.StatusCode, new ApiError { Message = "An unknown error has occured." });
return ApiResult<T>.Failed(response.StatusCode,
new ApiError { Message = "An unknown error has occured." });
}
protected async Task<ApiResult> HandleErrorAsync(HttpResponseMessage response)
{
try
{
var errors = await ParseErrorsAsync(response);
var errors = await ParseErrorsAsync(response).ConfigureAwait(false);
return ApiResult.Failed(response.StatusCode, errors.ToArray());
}
catch(JsonReaderException)
{ }
return ApiResult.Failed(response.StatusCode, new ApiError { Message = "An unknown error has occured." });
return ApiResult.Failed(response.StatusCode,
new ApiError { Message = "An unknown error has occured." });
}
private async Task<List<ApiError>> ParseErrorsAsync(HttpResponseMessage response)
@@ -69,7 +75,7 @@ namespace Bit.App.Repositories
var errors = new List<ApiError>();
if(response.StatusCode == System.Net.HttpStatusCode.BadRequest)
{
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var errorResponseModel = JsonConvert.DeserializeObject<ErrorResponse>(responseContent);
foreach(var valError in errorResponseModel.ValidationErrors)

View File

@@ -35,13 +35,13 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync<CipherResponse>(response);
return await HandleErrorAsync<CipherResponse>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<CipherResponse>(responseContent);
return ApiResult<CipherResponse>.Success(responseObj, response.StatusCode);
}
@@ -69,13 +69,13 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync<ListResponse<CipherResponse>>(response);
return await HandleErrorAsync<ListResponse<CipherResponse>>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<ListResponse<CipherResponse>>(responseContent);
return ApiResult<ListResponse<CipherResponse>>.Success(responseObj, response.StatusCode);
}
@@ -103,13 +103,13 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync<CipherHistoryResponse>(response);
return await HandleErrorAsync<CipherHistoryResponse>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<CipherHistoryResponse>(responseContent);
return ApiResult<CipherHistoryResponse>.Success(responseObj, response.StatusCode);
}

View File

@@ -34,10 +34,10 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync(response);
return await HandleErrorAsync(response).ConfigureAwait(false);
}
return ApiResult.Success(response.StatusCode);
@@ -61,15 +61,16 @@ namespace Bit.App.Repositories
var requestMessage = new TokenHttpRequestMessage
{
Method = HttpMethod.Put,
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/identifier/", identifier, "/clear-token")),
RequestUri = new Uri(client.BaseAddress,
string.Concat(ApiRoute, "/identifier/", identifier, "/clear-token"))
};
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync(response);
return await HandleErrorAsync(response).ConfigureAwait(false);
}
return ApiResult.Success(response.StatusCode);

View File

@@ -35,13 +35,13 @@ namespace Bit.App.Repositories
try
{
var response = await client.SendAsync(requestMessage);
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
return await HandleErrorAsync<ListResponse<FolderResponse>>(response);
return await HandleErrorAsync<ListResponse<FolderResponse>>(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<ListResponse<FolderResponse>>(responseContent);
return ApiResult<ListResponse<FolderResponse>>.Success(responseObj, response.StatusCode);
}