mirror of
https://github.com/bitwarden/android.git
synced 2024-12-24 18:08:26 +03:00
api error detection updates
This commit is contained in:
parent
0c93fc2662
commit
8c8fa8ae4c
6 changed files with 52 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Android.App;
|
using Android.App;
|
||||||
using Bit.App.Abstractions;
|
using Bit.App.Abstractions;
|
||||||
|
using Bit.Core.Enums;
|
||||||
using Plugin.CurrentActivity;
|
using Plugin.CurrentActivity;
|
||||||
|
|
||||||
namespace Bit.Droid.Services
|
namespace Bit.Droid.Services
|
||||||
|
@ -10,6 +11,8 @@ namespace Bit.Droid.Services
|
||||||
private ProgressDialog _progressDialog;
|
private ProgressDialog _progressDialog;
|
||||||
private Android.Widget.Toast _toast;
|
private Android.Widget.Toast _toast;
|
||||||
|
|
||||||
|
public DeviceType DeviceType => DeviceType.Android;
|
||||||
|
|
||||||
public void Toast(string text, bool longDuration = false)
|
public void Toast(string text, bool longDuration = false)
|
||||||
{
|
{
|
||||||
if(_toast != null)
|
if(_toast != null)
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
using System.Threading.Tasks;
|
using Bit.Core.Enums;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Bit.App.Abstractions
|
namespace Bit.App.Abstractions
|
||||||
{
|
{
|
||||||
public interface IDeviceActionService
|
public interface IDeviceActionService
|
||||||
{
|
{
|
||||||
|
DeviceType DeviceType { get; }
|
||||||
void Toast(string text, bool longDuration = false);
|
void Toast(string text, bool longDuration = false);
|
||||||
bool LaunchApp(string appName);
|
bool LaunchApp(string appName);
|
||||||
Task ShowLoadingAsync(string text);
|
Task ShowLoadingAsync(string text);
|
||||||
|
|
|
@ -64,7 +64,7 @@ namespace Bit.App.Services
|
||||||
|
|
||||||
public Core.Enums.DeviceType GetDevice()
|
public Core.Enums.DeviceType GetDevice()
|
||||||
{
|
{
|
||||||
return Device.RuntimePlatform == Device.iOS ? Core.Enums.DeviceType.iOS : Core.Enums.DeviceType.Android;
|
return _deviceActionService.DeviceType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetDeviceString()
|
public string GetDeviceString()
|
||||||
|
@ -91,7 +91,7 @@ namespace Bit.App.Services
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var launched = false;
|
var launched = false;
|
||||||
if(Device.RuntimePlatform == Device.Android && uri.StartsWith("androidapp://"))
|
if(GetDevice() == Core.Enums.DeviceType.Android && uri.StartsWith("androidapp://"))
|
||||||
{
|
{
|
||||||
launched = _deviceActionService.LaunchApp(uri);
|
launched = _deviceActionService.LaunchApp(uri);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ namespace Bit.Core.Models.Response
|
||||||
{
|
{
|
||||||
public class ErrorResponse
|
public class ErrorResponse
|
||||||
{
|
{
|
||||||
|
public ErrorResponse() { }
|
||||||
|
|
||||||
public ErrorResponse(JObject response, HttpStatusCode status, bool identityResponse = false)
|
public ErrorResponse(JObject response, HttpStatusCode status, bool identityResponse = false)
|
||||||
{
|
{
|
||||||
JObject errorModel = null;
|
JObject errorModel = null;
|
||||||
|
|
|
@ -37,6 +37,8 @@ namespace Bit.Core.Services
|
||||||
_tokenService = tokenService;
|
_tokenService = tokenService;
|
||||||
_platformUtilsService = platformUtilsService;
|
_platformUtilsService = platformUtilsService;
|
||||||
_logoutCallbackAsync = logoutCallbackAsync;
|
_logoutCallbackAsync = logoutCallbackAsync;
|
||||||
|
var device = _platformUtilsService.GetDevice();
|
||||||
|
_deviceType = device.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool UrlsSet { get; private set; }
|
public bool UrlsSet { get; private set; }
|
||||||
|
@ -81,7 +83,15 @@ namespace Bit.Core.Services
|
||||||
requestMessage.Headers.Add("Accept", "application/json");
|
requestMessage.Headers.Add("Accept", "application/json");
|
||||||
requestMessage.Headers.Add("Device-Type", _deviceType);
|
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;
|
JObject responseJObject = null;
|
||||||
if(IsJsonResponse(response))
|
if(IsJsonResponse(response))
|
||||||
{
|
{
|
||||||
|
@ -321,7 +331,15 @@ namespace Bit.Core.Services
|
||||||
requestMessage.Headers.Add("Accept", "application/json");
|
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)
|
if(hasResponse && response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
var responseJsonString = await response.Content.ReadAsStringAsync();
|
var responseJsonString = await response.Content.ReadAsStringAsync();
|
||||||
|
@ -359,7 +377,15 @@ namespace Bit.Core.Services
|
||||||
requestMessage.Headers.Add("Accept", "application/json");
|
requestMessage.Headers.Add("Accept", "application/json");
|
||||||
requestMessage.Headers.Add("Device-Type", _deviceType);
|
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)
|
if(response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
var responseJsonString = await response.Content.ReadAsStringAsync();
|
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<ErrorResponse> HandleErrorAsync(HttpResponseMessage response, bool tokenError)
|
private async Task<ErrorResponse> HandleErrorAsync(HttpResponseMessage response, bool tokenError)
|
||||||
{
|
{
|
||||||
if((tokenError && response.StatusCode == HttpStatusCode.BadRequest) ||
|
if((tokenError && response.StatusCode == HttpStatusCode.BadRequest) ||
|
||||||
|
@ -393,8 +428,7 @@ namespace Bit.Core.Services
|
||||||
|
|
||||||
private bool IsJsonResponse(HttpResponseMessage response)
|
private bool IsJsonResponse(HttpResponseMessage response)
|
||||||
{
|
{
|
||||||
return response.Headers.Contains("content-type") &&
|
return (response.Content?.Headers?.ContentType?.MediaType ?? string.Empty) == "application/json";
|
||||||
response.Headers.GetValues("content-type").Any(h => h.Contains("application/json"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Bit.App.Abstractions;
|
using Bit.App.Abstractions;
|
||||||
|
using Bit.Core.Enums;
|
||||||
using Bit.iOS.Core.Views;
|
using Bit.iOS.Core.Views;
|
||||||
using CoreGraphics;
|
using CoreGraphics;
|
||||||
using Foundation;
|
using Foundation;
|
||||||
|
@ -16,6 +17,8 @@ namespace Bit.iOS.Services
|
||||||
private Toast _toast;
|
private Toast _toast;
|
||||||
private UIAlertController _progressAlert;
|
private UIAlertController _progressAlert;
|
||||||
|
|
||||||
|
public DeviceType DeviceType => DeviceType.iOS;
|
||||||
|
|
||||||
public bool LaunchApp(string appName)
|
public bool LaunchApp(string appName)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
|
|
Loading…
Reference in a new issue