public async Task <Categories> Get() { var client = _httpClientProvider.GetHttpClient(); try { var response = await client.GetAsync("categories"); if (!response.IsSuccessStatusCode) { throw new Exception(); } var json = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <Categories>(json)); } finally { if (client != null) { client.Dispose(); } } }
/// <summary> /// Gets the job matching the supplied id /// </summary> /// <param name="jobId"></param> /// <returns></returns> public async Task <Job> ReadJob(string jobId) { if (_jobsCache != null) { var cached = _jobsCache.ReadJob(jobId); if (cached != null) { return(cached); } } try { if (_httpClient == null) { _httpClient = _httpClientProvider.GetHttpClient(); } var responseJson = await _httpClient.GetStringAsync(new Uri($"{_apiBaseUrl.ToString().TrimEnd('/')}/umbraco/api/{_jobsSet}/job/{jobId}/?baseUrl={HttpUtility.UrlEncode(_jobAdvertBaseUrl.ToString())}")); var job = JsonConvert.DeserializeObject <Job>(responseJson, new[] { new IHtmlStringConverter() }); if (_jobsCache != null) { _jobsCache.CacheJob(job); } return(job); } catch (HttpRequestException ex) { ex.ToExceptionless().Submit(); return(null); } }
/// <summary> /// Gets the settings for where to display the web chat feature /// </summary> /// <returns></returns> public async Task <WebChatSettings> ReadWebChatSettings() { if (_apiUrl == null) { return(new WebChatSettings()); } try { var cachedSettings = _cache?.ReadFromCache("WebChatSettingsUrl"); if (cachedSettings != null) { return(cachedSettings); } if (_httpClient == null) { _httpClient = _httpClientProvider.GetHttpClient(); } var json = await _httpClient.GetStringAsync(_apiUrl).ConfigureAwait(false); 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 exception.Data.Add("URL", _apiUrl.ToString()); exception.ToExceptionless().Submit(); return(new WebChatSettings()); } }
public async Task <Contacts> Get() { var client = _httpClientProvider.GetHttpClient(); try { var response = await client.GetAsync("contacts"); if (!response.IsSuccessStatusCode) { throw new Exception(); } var json = await response.Content.ReadAsStringAsync(); var emailsJObject = JObject.Parse(json); var contacts = new Contacts() { Total = emailsJObject.Root.SelectToken("total").Value <int>() }; foreach (var emailJObject in emailsJObject.Root.SelectToken("contacts").Children()) { contacts.Data.Add(JsonConvert.DeserializeObject <Contact>(emailJObject.First.ToString())); } return(contacts); } finally { if (client != null) { client.Dispose(); } } }
/// <summary> /// Requests the remote HTML. /// </summary> /// <param name="applicationId">A string which identifies the application making the request</param> /// <param name="forUrl">The page to request the control for (usually the current page)</param> /// <param name="controlId">A key identifying the control to cache.</param> /// <param name="selectedSection">The selected section.</param> /// <param name="textSize">The current setting for the site's text size feature.</param> /// <param name="isLibraryCatalogueRequest"><c>true</c> if the request is from a public catalogue machine in a library</param> private async Task <string> RequestRemoteHtml(string applicationId, Uri forUrl, string controlId, string selectedSection, int textSize, bool isLibraryCatalogueRequest) { string html = string.Empty; try { // Get the URL to request the cached control from. // Include text size so that header knows which links to apply Uri urlToRequest = new Uri(forUrl, String.Format(CultureInfo.CurrentCulture, _masterPageControlUrl.ToString(), controlId)); applicationId = HttpUtility.UrlEncode(applicationId.ToLower(CultureInfo.CurrentCulture).TrimEnd('/')); var query = HttpUtility.ParseQueryString(urlToRequest.Query); query.Add("section", selectedSection); query.Add("host", forUrl.Host); query.Add("textsize", textSize.ToString(CultureInfo.InvariantCulture)); query.Add("path", applicationId); urlToRequest = new Uri(urlToRequest.Scheme + "://" + urlToRequest.Authority + urlToRequest.AbsolutePath + "?" + query); try { // Create the request. Pass current user-agent so that library catalogue PCs can be detected by the remote script. if (_httpClient == null) { _httpClient = _httpClientProvider.GetHttpClient(); } using (var request = new HttpRequestMessage(HttpMethod.Get, urlToRequest)) { if (!String.IsNullOrEmpty(_userAgent)) { try { request.Headers.UserAgent.ParseAdd(_userAgent); } catch (FormatException) { // some real-world User-Agent values fail .NET parsing. No workaround available to just treat as if there was no User-Agent. } } using (var response = await _httpClient.SendAsync(request).ConfigureAwait(false)) { if (!response.IsSuccessStatusCode) { // Report failure code as if it was an exception new HttpRequestException($"Request to URL {urlToRequest} returned {response.StatusCode} {response.ReasonPhrase}") .ToExceptionless().Submit(); } html = await response.Content.ReadAsStringAsync(); if (_cacheProvider != null) { _cacheProvider.SaveRemoteHtmlToCache(applicationId, forUrl.Host, controlId, selectedSection, textSize, isLibraryCatalogueRequest, html); } } } } catch (HttpRequestException ex) { // Publish exception, otherwise it just disappears as async method has no calling code to throw to. ex.Data.Add("URL which failed", urlToRequest.ToString()); ex.ToExceptionless().Submit(); } } catch (UriFormatException ex) { ex.Data.Add("URL which failed", String.Format(CultureInfo.CurrentCulture, _masterPageControlUrl.ToString(), controlId)); ex.ToExceptionless().Submit(); } return(html); }
public HttpService(IConfiguration config, IHttpClientProvider httpClient) { _configuration = config; _httpClient = httpClient; _sharedHttpClient = _httpClient.GetHttpClient(); }
private HttpClient GetHttpClient() { return(_httpClientProvider.GetHttpClient(KEY)); }
public HttpClientService(IHttpClientProvider httpClientProvider) { _httpClientProvider = httpClientProvider; HttpClient = httpClientProvider.GetHttpClient(); }