public async Task <ApiResponseResult <ServerList> > GetServersAsync(TruncatedLocation location) { try { var uri = "vpn/logicals"; var ip = location.Value(); if (!string.IsNullOrEmpty(ip)) { // The following route is used to retrieve VPN server information, // including scores for the best server to connect to depending on // a user's proximity to a server and its load. To provide relevant // scores even when connected to VPN, we send a truncated version // of the user's public IP address. In keeping with our no-logs policy, // this partial IP address is not stored on the server and is only used // to fulfill this one-off API request. uri += $"?IP={ip}"; } var request = GetAuthorizedRequest(HttpMethod.Get, uri); using var response = await _client.SendAsync(request).ConfigureAwait(false); var stream = await response.Content.ReadAsStreamAsync(); return(Logged(GetResponseStreamResult <ServerList>(stream, response.StatusCode), "Get servers")); } catch (Exception e) when(e.IsApiCommunicationException()) { throw new HttpRequestException(e.Message, e); } }
public ApiServers( ILogger logger, IApiClient apiClient, TruncatedLocation location) { _logger = logger; _apiClient = apiClient; _location = location; }
public void Value_ShouldBeEmpty_WhenIpIsEmpty() { // Arrange var userLocation = new UserLocation(string.Empty, 10.0f, 10.0f, "ISP", "ZZ"); _userStorage.Location().Returns(userLocation); var location = new TruncatedLocation(_userStorage); // Act var result = location.Ip(); // Assert result.Should().Be(string.Empty); }
public void Value_ShouldBe_IpWithZeroedLastOctet(string ip) { // Arrange var userLocation = new UserLocation(ip, 10.0f, 10.0f, "ISP", "ZZ"); _userStorage.Location().Returns(userLocation); var location = new TruncatedLocation(_userStorage); // Act var result = location.Ip(); // Assert result.Split('.')[3].Should().Be("0"); }
public async Task <ICollection <LogicalServerContract> > GetServersAsync() { try { var location = new TruncatedLocation(_userStorage.Location().Ip); var response = await _apiClient.GetServersAsync(location); if (response.Success) { await SaveServersToCache(response.Value.Servers); return(response.Value.Servers); } return(_serversFileStorage.Get()); } catch (HttpRequestException ex) { _logger.Error("API: Get servers failed: " + ex.CombinedMessage()); return(_serversFileStorage.Get()); } }
public void ValueShouldBeEmptyIfIpIsEmpty() { var location = new TruncatedLocation(string.Empty); location.Value().Should().Be(string.Empty); }
public void ValueShouldReturnZeroAsLastIpNumber(string ip) { var location = new TruncatedLocation(ip); location.Value().Split('.')[3].Should().Be("0"); }