Beispiel #1
0
        /// <summary>
        /// Gets the settings for where to display the web chat feature
        /// </summary>
        /// <returns></returns>
        public WebChatSettings ReadWebChatSettings()
        {
            try
            {
                var cachedSettings = _cache?.ReadFromCache("WebChatSettingsUrl");
                if (cachedSettings != null)
                {
                    return(cachedSettings);
                }

                var client = new WebClient();
                if (_proxyProvider != null)
                {
                    client.Proxy = _proxyProvider.CreateProxy();
                }
                var json     = Task.Run(async() => await client.DownloadStringTaskAsync(_apiUrl)).Result;
                var settings = JsonConvert.DeserializeObject <WebChatSettings>(json);

                _cache?.AddToCache("WebChatSettingsUrl", settings);

                return(settings);
            }
            catch (Exception exception)
            {
                // catch, report and suppress errors because we never want a check for web chat support to stop a page from loading
                var data = new Dictionary <string, object> {
                    { "URL", _apiUrl.ToString() }
                };
                exception.ToExceptionless(true, data).Submit();
                return(new WebChatSettings());
            }
        }
Beispiel #2
0
        public async Task AddLibraries(DataTable table)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }

            if (_httpClient == null)
            {
                var handler = new HttpClientHandler();
                handler.Proxy = _proxyProvider?.CreateProxy();
                _httpClient   = new HttpClient(handler);
            }

            var json = await _httpClient.GetStringAsync(_libraryDataUrl);

            var libraries = JsonConvert.DeserializeObject <List <LocationApiResult> >(json);

            foreach (var library in libraries)
            {
                var row = table.NewRow();
                row["Name"]         = library.Name;
                row["URL"]          = library.Url;
                row["Latitude"]     = library.Latitude;
                row["Longitude"]    = library.Longitude;
                row["Description"]  = library.Description;
                row["Town"]         = library.Town;
                row["LocationType"] = library.LocationType;
                table.Rows.Add(row);
            }
        }
        /// <summary>
        /// Gets the coordinates at the centre of a postcode, based on the mean position of addresses rather than the geographic centre
        /// </summary>
        /// <param name="postcode">The postcode.</param>
        /// <returns></returns>
        public async Task <LatitudeLongitude> CoordinatesAtCentreOfPostcodeAsync(string postcode)
        {
            if (String.IsNullOrEmpty(postcode))
            {
                return(null);
            }

            try
            {
                if (_httpClient == null)
                {
                    var handler = new HttpClientHandler();
                    handler.Proxy = _proxyProvider?.CreateProxy();
                    _httpClient   = new HttpClient();
                    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _authenticationToken);
                }

                var queryUrl = String.Format(_locateApiUrl.ToString(), Regex.Replace(postcode, "[^A-Za-z0-9]", String.Empty));

                var json = await _httpClient.GetStringAsync(queryUrl);

                var result = JsonConvert.DeserializeObject <LocateApiResult>(json);
                return(new LatitudeLongitude(result.latitude, result.longitude));
            }
            catch (HttpRequestException exception)
            {
                if (!exception.Message.Contains("(422) Unprocessable Entity"))
                {
                    exception.ToExceptionless().Submit();
                }
                return(null);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Initiates an HTTP request and returns the HTML.
        /// </summary>
        /// <returns></returns>
        private static async Task <Stream> ReadHtml(Uri url, IProxyProvider proxy)
        {
            var handler = new HttpClientHandler()
            {
                Proxy = proxy?.CreateProxy()
            };
            var client = new HttpClient(handler);

            return(await client.GetStreamAsync(url).ConfigureAwait(false));
        }
        /// <summary>
        /// Gets the addresses that share a postcode
        /// </summary>
        /// <param name="postcode">The postcode.</param>
        /// <exception cref="WebException"></exception>
        /// <returns></returns>
        public IList <BS7666Address> AddressesFromPostcode(string postcode)
        {
            var query = Regex.Replace(postcode, "[^A-Za-z0-9]", String.Empty);

            if (String.IsNullOrEmpty(query))
            {
                return(new List <BS7666Address>());
            }

            try
            {
                using (var client = new WebClient())
                {
                    if (_proxyProvider != null)
                    {
                        client.Proxy = _proxyProvider.CreateProxy();
                    }
                    client.Headers.Add("Authorization", "Bearer " + _authenticationToken);

                    var queryUrl = String.Format(_locateApiUrl.ToString(), query);

                    using (var stream = new StreamReader(client.OpenRead(queryUrl)))
                    {
                        var json      = stream.ReadToEnd();
                        var results   = JsonConvert.DeserializeObject <LocateApiAddressResult[]>(json);
                        var addresses = new List <BS7666Address>();
                        foreach (var result in results)
                        {
                            var address = new BS7666Address(result.presentation.property, String.Empty, result.presentation.street, String.Empty, result.presentation.town, result.presentation.area, result.presentation.postcode)
                            {
                                Uprn          = result.uprn,
                                Usrn          = result.details?.usrn,
                                GeoCoordinate = new GeoCoordinate()
                                {
                                    Latitude  = result.location.latitude,
                                    Longitude = result.location.longitude
                                }
                            };
                            addresses.Add(address);
                        }
                        return(addresses);
                    }
                }
            }
            catch (WebException exception)
            {
                if (exception.Message.Contains("(422) Unprocessable Entity"))
                {
                    return(new List <BS7666Address>());
                }
                throw;
            }
        }
Beispiel #6
0
        /// <summary>
        /// Reads the closure information for a given service type.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <returns></returns>
        public ClosureInfo ReadClosureInfo(ServiceType serviceType)
        {
            using (SchoolsInformationService.SchoolsInformationWebService webService = new SchoolsInformationService.SchoolsInformationWebService())
            {
                _log?.Info("Requesting data using " + webService.Url);
                webService.Proxy       = _proxyProvider?.CreateProxy();
                webService.Credentials = _credentialsProvider?.CreateCredentials();

                var proxyObject = webService.ClosureInfoAllSchools();
                var converter   = new WebServiceProxyConverter <ClosureInfo, proxy.ClosureInfo>("http://czoneapps.eastsussex.gov.uk/Czone.WebService.SchoolsInformation/");
                return(converter.ConvertProxyToOriginalType(proxyObject));
            }
        }
Beispiel #7
0
        /// <summary>
        /// Runs a search query against Google Site Search
        /// </summary>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public async Task <ISearchResponse> SearchAsync(ISearchQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            // Try to get the response from cache first. Saves re-querying Google, which saves money.
            var cachedResponse = this.CacheStrategy?.FetchCachedResponse(query);

            if (cachedResponse != null)
            {
                return(new GoogleResponse(cachedResponse));
            }

            // Don't use gl=uk parameter as it affects the order of results, and not necessarily in a good way.
            // Its purpose is to boost UK results above those from other countries, but since all our results are UK results
            // we shouldn't need that. Example: "age well" returns a 2006 consultation above the current page with this option on.
            //
            // Google says "Google will only return spelling suggestions for queries where the gl parameter value is in lowercase letters",
            // but that turns out not to be true, as excluding the parameter still returns spelling suggestions.
            // http://code.google.com/intl/en/apis/customsearch/docs/xml_results.html#results_xml_tag_Spelling
            //
            string url = "https://www.googleapis.com/customsearch/v1?cx=" + HttpUtility.UrlEncode(SearchEngineId) +
                         "&key=" + HttpUtility.UrlEncode(ApiKey) +
                         "&q=" + HttpUtility.UrlEncode(query.QueryTerms) +
                         "&hq=" + HttpUtility.UrlEncode(query.QueryWithinResultsTerms) +
                         "&start=" + (((query.Page - 1) * query.PageSize) + 1) +
                         "&num=" + query.PageSize +
                         "&fields=queries(nextPage,previousPage),searchInformation,spelling(correctedQuery),items(title,link,htmlSnippet,htmlFormattedUrl)&hl=en";

            // Make a fresh request to Google for search results.
            if (_httpClient == null)
            {
                _httpClient = new HttpClient(new HttpClientHandler()
                {
                    Proxy = _proxyProvider?.CreateProxy()
                });
            }
            var response = new GoogleResponse(await _httpClient.GetStringAsync(url).ConfigureAwait(false));

            // Cache the response if possible before returning it
            this.CacheStrategy?.CacheResponse(query, response);

            return(response);
        }
Beispiel #8
0
        /// <summary>
        /// Creates a new <see cref="HttpWebRequest"/> for the specified URI, with proxy access configured.
        /// </summary>
        /// <param name="requestUri">The request URI.</param>
        /// <returns></returns>
        public HttpWebRequest CreateRequest(Uri requestUri, IProxyProvider proxyProvider)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(requestUri);

            webRequest.UserAgent = "Escc.Net.HttpRequestClient"; // Some apps require a user-agent to be present
            if (proxyProvider != null)
            {
                IWebProxy proxy = proxyProvider.CreateProxy();
                if (proxy != null)
                {
                    webRequest.Proxy       = proxy;
                    webRequest.Credentials = proxy.Credentials;
                }
                else
                {
                    webRequest.Credentials = CredentialCache.DefaultCredentials;
                }
            }
            else
            {
                webRequest.Credentials = CredentialCache.DefaultCredentials;
            }
            return(webRequest);
        }