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

@ -159,13 +159,15 @@ namespace Bit.App.Pages
{ {
if(string.IsNullOrWhiteSpace(EmailCell.Entry.Text)) if(string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.EmailAddress), AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.EmailAddress), AppResources.Ok);
return; return;
} }
if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text)) if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text))
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.MasterPassword), AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.MasterPassword), AppResources.Ok);
return; return;
} }
@ -180,9 +182,8 @@ namespace Bit.App.Pages
Device = new DeviceRequest(_appIdService, _deviceInfo) Device = new DeviceRequest(_appIdService, _deviceInfo)
}; };
var responseTask = _authService.TokenPostAsync(request);
_userDialogs.ShowLoading("Logging in...", MaskType.Black); _userDialogs.ShowLoading("Logging in...", MaskType.Black);
var response = await responseTask; var response = await _authService.TokenPostAsync(request);
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(!response.Succeeded) if(!response.Succeeded)
{ {

View file

@ -115,7 +115,8 @@ namespace Bit.App.Pages
{ {
if(string.IsNullOrWhiteSpace(CodeCell.Entry.Text)) if(string.IsNullOrWhiteSpace(CodeCell.Entry.Text))
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, "Verification code"), AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
"Verification code"), AppResources.Ok);
return; return;
} }
@ -126,9 +127,8 @@ namespace Bit.App.Pages
Device = new DeviceRequest(_appIdService, _deviceInfo) Device = new DeviceRequest(_appIdService, _deviceInfo)
}; };
var responseTask = _authService.TokenTwoFactorPostAsync(request);
_userDialogs.ShowLoading("Validating code...", MaskType.Black); _userDialogs.ShowLoading("Validating code...", MaskType.Black);
var response = await responseTask; var response = await _authService.TokenTwoFactorPostAsync(request);
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(!response.Succeeded) if(!response.Succeeded)
{ {

View file

@ -4,7 +4,6 @@ using Bit.App.Abstractions;
using Bit.App.Controls; using Bit.App.Controls;
using Bit.App.Models.Api; using Bit.App.Models.Api;
using Bit.App.Resources; using Bit.App.Resources;
using Plugin.DeviceInfo.Abstractions;
using Xamarin.Forms; using Xamarin.Forms;
using XLabs.Ioc; using XLabs.Ioc;
using Acr.UserDialogs; using Acr.UserDialogs;
@ -106,7 +105,8 @@ namespace Bit.App.Pages
{ {
if(string.IsNullOrWhiteSpace(EmailCell.Entry.Text)) if(string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.EmailAddress), AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.EmailAddress), AppResources.Ok);
return; return;
} }
@ -115,9 +115,8 @@ namespace Bit.App.Pages
Email = EmailCell.Entry.Text Email = EmailCell.Entry.Text
}; };
var responseTask = _accountApiRepository.PostPasswordHintAsync(request);
_userDialogs.ShowLoading("Submitting...", MaskType.Black); _userDialogs.ShowLoading("Submitting...", MaskType.Black);
var response = await responseTask; var response = await _accountApiRepository.PostPasswordHintAsync(request);
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(!response.Succeeded) if(!response.Succeeded)
{ {

View file

@ -48,11 +48,14 @@ namespace Bit.App.Pages
PasswordHintCell = new FormEntryCell("Master Password Hint (optional)", useLabelAsPlaceholder: true, PasswordHintCell = new FormEntryCell("Master Password Hint (optional)", useLabelAsPlaceholder: true,
imageSource: "lightbulb", containerPadding: padding); imageSource: "lightbulb", containerPadding: padding);
ConfirmPasswordCell = new FormEntryCell("Re-type Master Password", IsPassword: true, ConfirmPasswordCell = new FormEntryCell("Re-type Master Password", IsPassword: true,
nextElement: PasswordHintCell.Entry, useLabelAsPlaceholder: true, imageSource: "lock", containerPadding: padding); nextElement: PasswordHintCell.Entry, useLabelAsPlaceholder: true, imageSource: "lock",
containerPadding: padding);
PasswordCell = new FormEntryCell(AppResources.MasterPassword, IsPassword: true, PasswordCell = new FormEntryCell(AppResources.MasterPassword, IsPassword: true,
nextElement: ConfirmPasswordCell.Entry, useLabelAsPlaceholder: true, imageSource: "lock", containerPadding: padding); nextElement: ConfirmPasswordCell.Entry, useLabelAsPlaceholder: true, imageSource: "lock",
containerPadding: padding);
EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: PasswordCell.Entry, EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: PasswordCell.Entry,
entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true, imageSource: "envelope", containerPadding: padding); entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true, imageSource: "envelope",
containerPadding: padding);
PasswordHintCell.Entry.ReturnType = Enums.ReturnType.Done; PasswordHintCell.Entry.ReturnType = Enums.ReturnType.Done;
PasswordHintCell.Entry.Completed += Entry_Completed; PasswordHintCell.Entry.Completed += Entry_Completed;
@ -178,16 +181,17 @@ namespace Bit.App.Pages
{ {
Email = normalizedEmail, Email = normalizedEmail,
MasterPasswordHash = _cryptoService.HashPasswordBase64(key, PasswordCell.Entry.Text), MasterPasswordHash = _cryptoService.HashPasswordBase64(key, PasswordCell.Entry.Text),
MasterPasswordHint = !string.IsNullOrWhiteSpace(PasswordHintCell.Entry.Text) ? PasswordHintCell.Entry.Text : null MasterPasswordHint = !string.IsNullOrWhiteSpace(PasswordHintCell.Entry.Text)
? PasswordHintCell.Entry.Text : null
}; };
var responseTask = _accountsApiRepository.PostRegisterAsync(request);
_userDialogs.ShowLoading("Creating account...", MaskType.Black); _userDialogs.ShowLoading("Creating account...", MaskType.Black);
var response = await responseTask; var response = await _accountsApiRepository.PostRegisterAsync(request);
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(!response.Succeeded) if(!response.Succeeded)
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message, AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message,
AppResources.Ok);
return; return;
} }

View file

@ -62,7 +62,8 @@ namespace Bit.App.Pages
if(string.IsNullOrWhiteSpace(nameCell.Entry.Text)) if(string.IsNullOrWhiteSpace(nameCell.Entry.Text))
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.Name), AppResources.Ok);
return; return;
} }
@ -71,21 +72,20 @@ namespace Bit.App.Pages
Name = nameCell.Entry.Text.Encrypt() Name = nameCell.Entry.Text.Encrypt()
}; };
var saveTask = _folderService.SaveAsync(folder);
_userDialogs.ShowLoading("Saving...", MaskType.Black); _userDialogs.ShowLoading("Saving...", MaskType.Black);
await saveTask; var saveResult = await _folderService.SaveAsync(folder);
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(saveTask.Result.Succeeded) if(saveResult.Succeeded)
{ {
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
_userDialogs.Toast("New folder created."); _userDialogs.Toast("New folder created.");
_googleAnalyticsService.TrackAppEvent("CreatedFolder"); _googleAnalyticsService.TrackAppEvent("CreatedFolder");
} }
else if(saveTask.Result.Errors.Count() > 0) else if(saveResult.Errors.Count() > 0)
{ {
await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred); await _userDialogs.AlertAsync(saveResult.Errors.First().Message, AppResources.AnErrorHasOccurred);
} }
else else
{ {

View file

@ -79,27 +79,27 @@ namespace Bit.App.Pages
if(string.IsNullOrWhiteSpace(nameCell.Entry.Text)) if(string.IsNullOrWhiteSpace(nameCell.Entry.Text))
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.Name), AppResources.Ok);
return; return;
} }
folder.Name = nameCell.Entry.Text.Encrypt(); folder.Name = nameCell.Entry.Text.Encrypt();
var saveTask = _folderService.SaveAsync(folder);
_userDialogs.ShowLoading("Saving...", MaskType.Black); _userDialogs.ShowLoading("Saving...", MaskType.Black);
await saveTask; var saveResult = await _folderService.SaveAsync(folder);
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(saveTask.Result.Succeeded) if(saveResult.Succeeded)
{ {
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
_userDialogs.Toast("Folder updated."); _userDialogs.Toast("Folder updated.");
_googleAnalyticsService.TrackAppEvent("EditedFolder"); _googleAnalyticsService.TrackAppEvent("EditedFolder");
} }
else if(saveTask.Result.Errors.Count() > 0) else if(saveResult.Errors.Count() > 0)
{ {
await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred); await _userDialogs.AlertAsync(saveResult.Errors.First().Message, AppResources.AnErrorHasOccurred);
} }
else else
{ {
@ -140,19 +140,19 @@ namespace Bit.App.Pages
return; return;
} }
var deleteTask = _folderService.DeleteAsync(_folderId);
_userDialogs.ShowLoading("Deleting...", MaskType.Black); _userDialogs.ShowLoading("Deleting...", MaskType.Black);
await deleteTask; var deleteTask = await _folderService.DeleteAsync(_folderId);
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if((await deleteTask).Succeeded) if(deleteTask.Succeeded)
{ {
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
_userDialogs.Toast("Folder deleted."); _userDialogs.Toast("Folder deleted.");
} }
else if((await deleteTask).Errors.Count() > 0) else if(deleteTask.Errors.Count() > 0)
{ {
await _userDialogs.AlertAsync((await deleteTask).Errors.First().Message, AppResources.AnErrorHasOccurred); await _userDialogs.AlertAsync(deleteTask.Errors.First().Message, AppResources.AnErrorHasOccurred);
} }
else else
{ {
@ -162,7 +162,8 @@ namespace Bit.App.Pages
private void AlertNoConnection() private void AlertNoConnection()
{ {
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage, AppResources.Ok); DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage,
AppResources.Ok);
} }
} }
} }

View file

@ -102,7 +102,8 @@ namespace Bit.App.Pages
public void AlertNoConnection() public void AlertNoConnection()
{ {
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage, AppResources.Ok); DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage,
AppResources.Ok);
} }
} }
} }

View file

@ -107,13 +107,15 @@ namespace Bit.App.Pages
if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text)) if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text))
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Password), AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.Password), AppResources.Ok);
return; return;
} }
if(string.IsNullOrWhiteSpace(nameCell.Entry.Text)) if(string.IsNullOrWhiteSpace(nameCell.Entry.Text))
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.Name), AppResources.Ok);
return; return;
} }
@ -132,20 +134,19 @@ namespace Bit.App.Pages
site.FolderId = folders.ElementAt(folderCell.Picker.SelectedIndex - 1).Id; site.FolderId = folders.ElementAt(folderCell.Picker.SelectedIndex - 1).Id;
} }
var saveTask = _siteService.SaveAsync(site);
_userDialogs.ShowLoading("Saving...", MaskType.Black); _userDialogs.ShowLoading("Saving...", MaskType.Black);
await saveTask; var saveTask = await _siteService.SaveAsync(site);
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(saveTask.Result.Succeeded) if(saveTask.Succeeded)
{ {
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
_userDialogs.Toast("New site created."); _userDialogs.Toast("New site created.");
_googleAnalyticsService.TrackAppEvent("CreatedSite"); _googleAnalyticsService.TrackAppEvent("CreatedSite");
} }
else if(saveTask.Result.Errors.Count() > 0) else if(saveTask.Errors.Count() > 0)
{ {
await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred); await _userDialogs.AlertAsync(saveTask.Errors.First().Message, AppResources.AnErrorHasOccurred);
} }
else else
{ {
@ -183,7 +184,8 @@ namespace Bit.App.Pages
private void AlertNoConnection() private void AlertNoConnection()
{ {
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage, AppResources.Ok); DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage,
AppResources.Ok);
} }
} }
} }

View file

@ -140,13 +140,15 @@ namespace Bit.App.Pages
if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text)) if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text))
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Password), AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.Password), AppResources.Ok);
return; return;
} }
if(string.IsNullOrWhiteSpace(nameCell.Entry.Text)) if(string.IsNullOrWhiteSpace(nameCell.Entry.Text))
{ {
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok); await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.Name), AppResources.Ok);
return; return;
} }
@ -166,21 +168,20 @@ namespace Bit.App.Pages
site.FolderId = null; site.FolderId = null;
} }
var saveTask = _siteService.SaveAsync(site);
_userDialogs.ShowLoading("Saving...", MaskType.Black); _userDialogs.ShowLoading("Saving...", MaskType.Black);
await saveTask; var saveTask = await _siteService.SaveAsync(site);
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if(saveTask.Result.Succeeded) if(saveTask.Succeeded)
{ {
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
_userDialogs.Toast("Site updated."); _userDialogs.Toast("Site updated.");
_googleAnalyticsService.TrackAppEvent("EditedSite"); _googleAnalyticsService.TrackAppEvent("EditedSite");
} }
else if(saveTask.Result.Errors.Count() > 0) else if(saveTask.Errors.Count() > 0)
{ {
await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred); await _userDialogs.AlertAsync(saveTask.Errors.First().Message, AppResources.AnErrorHasOccurred);
} }
else else
{ {
@ -209,7 +210,8 @@ namespace Bit.App.Pages
private async void GenerateCell_Tapped(object sender, EventArgs e) private async void GenerateCell_Tapped(object sender, EventArgs e)
{ {
if(!string.IsNullOrWhiteSpace(PasswordCell.Entry.Text) if(!string.IsNullOrWhiteSpace(PasswordCell.Entry.Text)
&& !await _userDialogs.ConfirmAsync("Are you sure you want to overwrite the current password?", null, AppResources.Yes, AppResources.No)) && !await _userDialogs.ConfirmAsync("Are you sure you want to overwrite the current password?", null,
AppResources.Yes, AppResources.No))
{ {
return; return;
} }
@ -235,20 +237,19 @@ namespace Bit.App.Pages
return; return;
} }
var deleteTask = _siteService.DeleteAsync(_siteId);
_userDialogs.ShowLoading("Deleting...", MaskType.Black); _userDialogs.ShowLoading("Deleting...", MaskType.Black);
await deleteTask; var deleteTask = await _siteService.DeleteAsync(_siteId);
_userDialogs.HideLoading(); _userDialogs.HideLoading();
if((await deleteTask).Succeeded) if(deleteTask.Succeeded)
{ {
await Navigation.PopModalAsync(); await Navigation.PopModalAsync();
_userDialogs.Toast("Site deleted."); _userDialogs.Toast("Site deleted.");
_googleAnalyticsService.TrackAppEvent("DeletedSite"); _googleAnalyticsService.TrackAppEvent("DeletedSite");
} }
else if((await deleteTask).Errors.Count() > 0) else if(deleteTask.Errors.Count() > 0)
{ {
await _userDialogs.AlertAsync((await deleteTask).Errors.First().Message, AppResources.AnErrorHasOccurred); await _userDialogs.AlertAsync(deleteTask.Errors.First().Message, AppResources.AnErrorHasOccurred);
} }
else else
{ {
@ -258,7 +259,8 @@ namespace Bit.App.Pages
private void AlertNoConnection() private void AlertNoConnection()
{ {
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage, AppResources.Ok); DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage,
AppResources.Ok);
} }
} }
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -34,10 +34,10 @@ namespace Bit.App.Repositories
try try
{ {
var response = await client.SendAsync(requestMessage); var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode) if(!response.IsSuccessStatusCode)
{ {
return await HandleErrorAsync(response); return await HandleErrorAsync(response).ConfigureAwait(false);
} }
return ApiResult.Success(response.StatusCode); return ApiResult.Success(response.StatusCode);
@ -61,15 +61,16 @@ namespace Bit.App.Repositories
var requestMessage = new TokenHttpRequestMessage var requestMessage = new TokenHttpRequestMessage
{ {
Method = HttpMethod.Put, 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 try
{ {
var response = await client.SendAsync(requestMessage); var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode) if(!response.IsSuccessStatusCode)
{ {
return await HandleErrorAsync(response); return await HandleErrorAsync(response).ConfigureAwait(false);
} }
return ApiResult.Success(response.StatusCode); return ApiResult.Success(response.StatusCode);

View file

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