Ejemplo n.º 1
0
        public async Task <JObject> GetEntityByUrl(string entityUrl, ContentHubConnectionSettings contentHubConnectionSettings)
        {
            var request = WebRequest.Create(entityUrl);

            request.Method = "GET";
            var token = await GetToken(contentHubConnectionSettings).ConfigureAwait(false);

            request.Headers.Add("X-Auth-Token", token);

            string responseContent = null;

            using (var response = request.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    using (var streamReader = new StreamReader(responseStream))
                    {
                        responseContent = streamReader.ReadToEnd();
                    }
                }
            }
            if (!string.IsNullOrEmpty(responseContent))
            {
                return(JObject.Parse(responseContent));
            }

            return(null);
        }
Ejemplo n.º 2
0
        private async Task <Dictionary <string, JObject> > GetRelatedEntitiesRecursively(JObject startEntity, List <string> relatedEntityPaths, ContentHubConnectionSettings contentHubConnectionSettings)
        {
            try
            {
                if (relatedEntityPaths != null && relatedEntityPaths.Count > 0)
                {
                    var relatedEntity  = startEntity;
                    var relatedObjects = new Dictionary <string, JObject>();
                    for (int i = 0; i < relatedEntityPaths.Count; i++)
                    {
                        var relatedEntityTokens = relatedEntity.SelectTokens(relatedEntityPaths[i]);

                        if (relatedEntityTokens != null && relatedEntityTokens.Count() > 0)
                        {
                            foreach (var relatedEntityToken in relatedEntityTokens)
                            {
                                var relatedEntityUrl = relatedEntityToken.Value <string>();
                                if (!string.IsNullOrEmpty(relatedEntityUrl) && !relatedObjects.ContainsKey(relatedEntityUrl))
                                {
                                    relatedEntity = await _contentHubHelper.GetEntityByUrl(relatedEntityUrl, contentHubConnectionSettings).ConfigureAwait(false);

                                    if (relatedEntity == null)
                                    {
                                        return(null);
                                    }
                                    else if (i == relatedEntityPaths.Count - 1)
                                    {
                                        relatedObjects.Add(relatedEntityUrl, relatedEntity);
                                    }
                                    else
                                    {
                                        var remainingPaths   = relatedEntityPaths.GetRange(i + 1, relatedEntityPaths.Count - i - 1);
                                        var recursiveResults = await GetRelatedEntitiesRecursively(relatedEntity, remainingPaths, contentHubConnectionSettings).ConfigureAwait(false);

                                        if (recursiveResults != null && recursiveResults.Values.Count > 0)
                                        {
                                            relatedObjects.AddRange(recursiveResults);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return(relatedObjects.Count() > 0 ? relatedObjects : null);
                }

                return(null);
            }
            catch (Exception ex)
            {
                Log.Error($"Cannot parse entity at one of the path provided: {string.Join(", ", relatedEntityPaths)}", ex, this);
                throw ex;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get security token to use for Content Hub API calls
        /// </summary>
        /// <param name="contentHubPolicy"></param>
        /// <returns></returns>
        public async Task <string> GetToken(ContentHubConnectionSettings contentHubConnectionSettings)
        {
            try
            {
                var cache = _cacheManager.GetCache(CACHE_NAME);
                if (cache == null)
                {
                    cache = _cacheManager.CreateCache(CACHE_NAME);
                }

                string securityToken = await cache.Get <string>(contentHubConnectionSettings.InstanceName).ConfigureAwait(false);

                if (string.IsNullOrEmpty(securityToken))
                {
                    var request = WebRequest.Create(string.Format("{0}/api/authenticate", contentHubConnectionSettings.ProtocolAndHost));
                    request.Method      = "POST";
                    request.ContentType = "application/json";

                    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                    {
                        var jsonObject = new
                        {
                            user_name        = contentHubConnectionSettings.UserName,
                            password         = contentHubConnectionSettings.Password,
                            discard_existing = false
                        };

                        streamWriter.Write(JsonConvert.SerializeObject(jsonObject));
                        streamWriter.Flush();
                        streamWriter.Close();
                    }

                    string responseContent = null;
                    using (var response = request.GetResponse())
                    {
                        using (var responseStream = response.GetResponseStream())
                        {
                            using (var streamReader = new StreamReader(responseStream))
                            {
                                responseContent = streamReader.ReadToEnd();
                            }
                        }
                    }

                    var o = JObject.Parse(responseContent);
                    securityToken = (string)o["token"];

                    var cacheEntryOptions = new CacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(3000)
                    };
                    await cache.SetString(contentHubConnectionSettings.InstanceName, securityToken, cacheEntryOptions).ConfigureAwait(false);
                }
                //Log.Warn($"retrieved CH token: {securityToken}", this);
                return(securityToken);
            }
            catch (Exception ex)
            {
                Log.Error("Error retrieving Content Hub token", ex, this);
                throw;
            }
        }
Ejemplo n.º 4
0
        public async Task <JObject> GetEntityById(string entityId, ContentHubConnectionSettings contentHubConnectionSettings)
        {
            var entityUrl = $"{contentHubConnectionSettings.ProtocolAndHost}/api/entities/{entityId}";

            return(await GetEntityByUrl(entityUrl, contentHubConnectionSettings));
        }