private async Task <RawContentResponse> ExecuteContentRequestAsync <TId>(TId contentIdentifier, string endpoint, bool bypassCache, TryFunc <string, RawContentResponse> tryGetFromCache, Action <string, RawContentResponse> addToCache) { if (bypassCache == false && cacheIsAvailable) { RawContentResponse cacheOut; if (tryGetFromCache(contentIdentifier.ToString(), out cacheOut)) { return(cacheOut); } } var requestWithPolicy = new HttpRequestWithPolicy(this); var response = await requestWithPolicy.Execute(() => httpClient.GetAsync(string.Format((Target.Url.AbsoluteUri + endpoint), contentIdentifier)) ).ConfigureAwait(false); if (response.Result?.IsSuccessStatusCode == false || response.Outcome == OutcomeType.Failure) { // We don't want to throw exceptions if we can't return any content. // Let the caller deal with the empty result if they deem it critical. return(RawContentResponse.Empty); } string jsonResult = await response.Result.Content.ReadAsStringAsync().ConfigureAwait(false); RawContentResponse rawContentResponse = RawContentResponseFactory.Build(jsonResult); if (cacheIsAvailable && !bypassCache && !string.IsNullOrWhiteSpace(rawContentResponse.RenderedContent)) { addToCache(rawContentResponse.Id.ToString(), rawContentResponse); } return(rawContentResponse); }
private static void GenerateMetas(RawContentResponse building) { if (building.PropertyCollection != null) { var pagetitle = building.PropertyCollection.AsQueryable().Where(kvp => kvp.Key.Equals("sEOTitle", StringComparison.OrdinalIgnoreCase)).SingleOrDefault(); var pageDescription = building.PropertyCollection.AsQueryable().Where(kvp => kvp.Key.Equals("sEODescription", StringComparison.OrdinalIgnoreCase)).SingleOrDefault(); var keywords = building.PropertyCollection.AsQueryable().Where(kvp => kvp.Key.Equals("sEOKeywords", StringComparison.OrdinalIgnoreCase)).SingleOrDefault(); var robots = building.PropertyCollection.AsQueryable().Where(kvp => kvp.Key.Equals("sEORobots", StringComparison.OrdinalIgnoreCase)).SingleOrDefault(); var metatags = building.PropertyCollection.AsQueryable().Where(kvp => kvp.Key.Equals("sEOMetaTags", StringComparison.OrdinalIgnoreCase)).SingleOrDefault(); var tagHtmlRepresentation = new StringBuilder(); var metas = new List <KeyValuePair <string, string> >(); ConfigureTitleTag(building.Name, pagetitle, tagHtmlRepresentation, metas); ConfigureDescriptionTag(pageDescription, tagHtmlRepresentation, metas); ConfigureKeywordsTag(keywords, tagHtmlRepresentation, metas); ConfigureRobotsTag(robots, tagHtmlRepresentation, metas); ConfigureMetaTags(metatags, tagHtmlRepresentation, metas); building.MetaTagCollection = metas; building.RenderedMetaTags = tagHtmlRepresentation.ToString(); } }
public IHtmlString MapResponse(string jsonResponse) { RawContentResponse rawContentResponse = JsonConvert.DeserializeObject <RawContentResponse>(jsonResponse); return(new HtmlString(HttpUtility.HtmlDecode(rawContentResponse.RenderedContent))); }
/// <summary> /// Requests published content from umbraco asynchronously. With the option to bypass the local cache; always going to the server /// </summary> /// <param name="id">The unique ID of the content, which can be found in the umbraco back office</param> /// <param name="bypassCache">If true - The request will bypass the cache, always going to the server to get the content</param> public async Task <IHtmlString> GetPublishedContentAsync(int id, bool bypassCache) { RawContentResponse contentResponse = await ExecuteContentRequestAsync(id, ApiPath.PublishedContentIdWithTemplate, bypassCache, ContentIdCacheSearch(), AddContentToCache()).ConfigureAwait(false); return(contentResponse.RenderedContentHtmlString); }