public void FailTestSession(Exception testError) { try { var reqString = "{\"status\":\"error\", \"reason\":\"\"}"; var requestData = Encoding.UTF8.GetBytes(reqString); var myUri = new Uri($"https://www.browserstack.com/automate/sessions/{FindSessionId()}.json"); var myWebRequest = WebRequest.Create(myUri); var myHttpWebRequest = (HttpWebRequest)myWebRequest; myWebRequest.ContentType = "application/json"; myWebRequest.Method = "PUT"; myWebRequest.ContentLength = requestData.Length; using (var st = myWebRequest.GetRequestStream()) { st.Write(requestData, 0, requestData.Length); } var myCredentialCache = new CredentialCache(); myCredentialCache.Add(myUri, "Basic", FindNetworkCredential()); myHttpWebRequest.PreAuthenticate = true; myHttpWebRequest.Credentials = myCredentialCache; _retryService.RetryWeb(() => myWebRequest.GetResponse().Close(), x => _logger.Error(string.Empty, x)); } catch (Exception ex) { _logger.Error("Failed to connect to browser stack api", ex); } }
private async Task <string> GetPostcodeCountry(string postcode) { try { var uri = new Uri(_applicationSettings.PostcodeUrl, postcode.Replace(" ", string.Empty)); var response = await _retryService.RetryWeb(() => MakeRequestAsync(uri.ToString()), CouldntConnect); if (response.IsSuccessStatusCode) { var value = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <PostCodeResponse>(value); return(result.Result.Country); } } catch (Exception ex) { _logger.Warn(string.Concat("Couldn't connect to postcode service", ex.Message)); } return("Error"); }
public async Task <CoordinateResponse> GetLatLongFromPostCode(string postcode) { var coordinates = new Coordinate(); var uri = new Uri(_applicationSettings.PostcodeUrl, postcode.Replace(" ", string.Empty)); try { var stopwatch = Stopwatch.StartNew(); var response = await _retryService.RetryWeb(() => MakeRequestAsync(uri.ToString()), CouldntConnect); stopwatch.Stop(); var responseTime = stopwatch.ElapsedMilliseconds; if (response.IsSuccessStatusCode) { var value = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <PostCodeResponse>(value); if (!result.Result.Latitude.HasValue || !result.Result.Longitude.HasValue) { return(new CoordinateResponse { Coordinate = null, ResponseCode = LocationLookupResponse.MissingCoordinates }); } coordinates.Lat = result.Result.Latitude.Value; coordinates.Lon = result.Result.Longitude.Value; SendDependencyLog(response.StatusCode, uri, responseTime); var coordinateResponse = new CoordinateResponse { Coordinate = coordinates, ResponseCode = LocationLookupResponse.Ok }; return(coordinateResponse); } if (response.StatusCode == HttpStatusCode.InternalServerError) { LogInformation(postcode, uri, response, responseTime, "Postcodes.IO-ServerError", "Server error trying to find postcode"); return(new CoordinateResponse { Coordinate = null, ResponseCode = LocationLookupResponse.ServerError }); } LogInformation(postcode, uri, response, responseTime, "Postcodes.IO-PostCodeNotFound", "Unable to find coordinates for postcode"); return(new CoordinateResponse { Coordinate = null, ResponseCode = LocationLookupResponse.WrongPostcode }); } catch (Exception ex) { _logger.Error(ex, $"Unable to connect to Postcode lookup servce. Url: {uri}"); throw new SearchException("Unable to connect to Post Code Lookup service", ex); } }