diff --git a/src/Android/Services/DeviceActionService.cs b/src/Android/Services/DeviceActionService.cs index 3b909a998..fd01b3e66 100644 --- a/src/Android/Services/DeviceActionService.cs +++ b/src/Android/Services/DeviceActionService.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using Android.App; using Bit.App.Abstractions; +using Bit.Core.Enums; using Plugin.CurrentActivity; namespace Bit.Droid.Services @@ -10,6 +11,8 @@ namespace Bit.Droid.Services private ProgressDialog _progressDialog; private Android.Widget.Toast _toast; + public DeviceType DeviceType => DeviceType.Android; + public void Toast(string text, bool longDuration = false) { if(_toast != null) diff --git a/src/App/Abstractions/IDeviceActionService.cs b/src/App/Abstractions/IDeviceActionService.cs index a2e6b30cc..7b4554ad1 100644 --- a/src/App/Abstractions/IDeviceActionService.cs +++ b/src/App/Abstractions/IDeviceActionService.cs @@ -1,9 +1,11 @@ -using System.Threading.Tasks; +using Bit.Core.Enums; +using System.Threading.Tasks; namespace Bit.App.Abstractions { public interface IDeviceActionService { + DeviceType DeviceType { get; } void Toast(string text, bool longDuration = false); bool LaunchApp(string appName); Task ShowLoadingAsync(string text); diff --git a/src/App/Services/MobilePlatformUtilsService.cs b/src/App/Services/MobilePlatformUtilsService.cs index 7dce994a6..3e5e30e52 100644 --- a/src/App/Services/MobilePlatformUtilsService.cs +++ b/src/App/Services/MobilePlatformUtilsService.cs @@ -64,7 +64,7 @@ namespace Bit.App.Services public Core.Enums.DeviceType GetDevice() { - return Device.RuntimePlatform == Device.iOS ? Core.Enums.DeviceType.iOS : Core.Enums.DeviceType.Android; + return _deviceActionService.DeviceType; } public string GetDeviceString() @@ -91,7 +91,7 @@ namespace Bit.App.Services else { var launched = false; - if(Device.RuntimePlatform == Device.Android && uri.StartsWith("androidapp://")) + if(GetDevice() == Core.Enums.DeviceType.Android && uri.StartsWith("androidapp://")) { launched = _deviceActionService.LaunchApp(uri); } diff --git a/src/Core/Models/Response/ErrorResponse.cs b/src/Core/Models/Response/ErrorResponse.cs index 188134dae..b8e1cafdf 100644 --- a/src/Core/Models/Response/ErrorResponse.cs +++ b/src/Core/Models/Response/ErrorResponse.cs @@ -8,6 +8,8 @@ namespace Bit.Core.Models.Response { public class ErrorResponse { + public ErrorResponse() { } + public ErrorResponse(JObject response, HttpStatusCode status, bool identityResponse = false) { JObject errorModel = null; diff --git a/src/Core/Services/ApiService.cs b/src/Core/Services/ApiService.cs index 077184122..ae73fe121 100644 --- a/src/Core/Services/ApiService.cs +++ b/src/Core/Services/ApiService.cs @@ -37,6 +37,8 @@ namespace Bit.Core.Services _tokenService = tokenService; _platformUtilsService = platformUtilsService; _logoutCallbackAsync = logoutCallbackAsync; + var device = _platformUtilsService.GetDevice(); + _deviceType = device.ToString(); } public bool UrlsSet { get; private set; } @@ -81,7 +83,15 @@ namespace Bit.Core.Services requestMessage.Headers.Add("Accept", "application/json"); requestMessage.Headers.Add("Device-Type", _deviceType); - var response = await _httpClient.SendAsync(requestMessage); + HttpResponseMessage response; + try + { + response = await _httpClient.SendAsync(requestMessage); + } + catch + { + throw new ApiException(HandleWebError()); + } JObject responseJObject = null; if(IsJsonResponse(response)) { @@ -321,7 +331,15 @@ namespace Bit.Core.Services requestMessage.Headers.Add("Accept", "application/json"); } - var response = await _httpClient.SendAsync(requestMessage); + HttpResponseMessage response; + try + { + response = await _httpClient.SendAsync(requestMessage); + } + catch + { + throw new ApiException(HandleWebError()); + } if(hasResponse && response.IsSuccessStatusCode) { var responseJsonString = await response.Content.ReadAsStringAsync(); @@ -359,7 +377,15 @@ namespace Bit.Core.Services requestMessage.Headers.Add("Accept", "application/json"); requestMessage.Headers.Add("Device-Type", _deviceType); - var response = await _httpClient.SendAsync(requestMessage); + HttpResponseMessage response; + try + { + response = await _httpClient.SendAsync(requestMessage); + } + catch + { + throw new ApiException(HandleWebError()); + } if(response.IsSuccessStatusCode) { var responseJsonString = await response.Content.ReadAsStringAsync(); @@ -374,6 +400,15 @@ namespace Bit.Core.Services } } + private ErrorResponse HandleWebError() + { + return new ErrorResponse + { + StatusCode = HttpStatusCode.BadGateway, + Message = "There is a problem connecting to the server." + }; + } + private async Task HandleErrorAsync(HttpResponseMessage response, bool tokenError) { if((tokenError && response.StatusCode == HttpStatusCode.BadRequest) || @@ -393,8 +428,7 @@ namespace Bit.Core.Services private bool IsJsonResponse(HttpResponseMessage response) { - return response.Headers.Contains("content-type") && - response.Headers.GetValues("content-type").Any(h => h.Contains("application/json")); + return (response.Content?.Headers?.ContentType?.MediaType ?? string.Empty) == "application/json"; } #endregion diff --git a/src/iOS/Services/DeviceActionService.cs b/src/iOS/Services/DeviceActionService.cs index dc3ee67b8..a72c972b4 100644 --- a/src/iOS/Services/DeviceActionService.cs +++ b/src/iOS/Services/DeviceActionService.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Bit.App.Abstractions; +using Bit.Core.Enums; using Bit.iOS.Core.Views; using CoreGraphics; using Foundation; @@ -16,6 +17,8 @@ namespace Bit.iOS.Services private Toast _toast; private UIAlertController _progressAlert; + public DeviceType DeviceType => DeviceType.iOS; + public bool LaunchApp(string appName) { throw new NotImplementedException();