mirror of
https://github.com/bitwarden/mobile
synced 2025-12-11 05:43:30 +00:00
Init HttpClient with each Api Request. Guarg agsint connectivity issues when making API calls.
This commit is contained in:
@@ -4,30 +4,43 @@ using System.Threading.Tasks;
|
||||
using Bit.App.Abstractions;
|
||||
using Bit.App.Models.Api;
|
||||
using Newtonsoft.Json;
|
||||
using Plugin.Connectivity.Abstractions;
|
||||
|
||||
namespace Bit.App.Repositories
|
||||
{
|
||||
public class DeviceApiRepository : ApiRepository<DeviceRequest, DeviceResponse, string>, IDeviceApiRepository
|
||||
{
|
||||
public DeviceApiRepository(IConnectivity connectivity)
|
||||
: base(connectivity)
|
||||
{ }
|
||||
|
||||
protected override string ApiRoute => "devices";
|
||||
|
||||
public virtual async Task<ApiResult<DeviceResponse>> PutTokenAsync(string identifier, DeviceTokenRequest request)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage(request)
|
||||
if(!Connectivity.IsConnected)
|
||||
{
|
||||
Method = HttpMethod.Put,
|
||||
RequestUri = new Uri(Client.BaseAddress, string.Concat(ApiRoute, "/identifier/", identifier, "/token")),
|
||||
};
|
||||
|
||||
var response = await Client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<DeviceResponse>(response);
|
||||
return HandledNotConnected<DeviceResponse>();
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<DeviceResponse>(responseContent);
|
||||
return ApiResult<DeviceResponse>.Success(responseObj, response.StatusCode);
|
||||
using(var client = new ApiHttpClient())
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage(request)
|
||||
{
|
||||
Method = HttpMethod.Put,
|
||||
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/identifier/", identifier, "/token")),
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await HandleErrorAsync<DeviceResponse>(response);
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var responseObj = JsonConvert.DeserializeObject<DeviceResponse>(responseContent);
|
||||
return ApiResult<DeviceResponse>.Success(responseObj, response.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user