示例#1
0
        public static LinkHeader LinksFromHeader(string linkHeaderStr)
        {
            if (string.IsNullOrWhiteSpace(linkHeaderStr))
            {
                return(null);
            }

            var linkStrings = linkHeaderStr.Split(',');

            if (!linkStrings.Any())
            {
                return(null);
            }

            var linkHeader = new LinkHeader();

            foreach (var linkString in linkStrings)
            {
                var relMatch  = Regex.Match(linkString, "(?<=rel=\").+?(?=\")", RegexOptions.IgnoreCase);
                var linkMatch = Regex.Match(linkString, "(?<=<).+?(?=>)", RegexOptions.IgnoreCase);

                if (relMatch.Success && linkMatch.Success)
                {
                    var rel  = relMatch.Value.ToUpper();
                    var link = linkMatch.Value;

                    switch (rel)
                    {
                    case "FIRST":
                        linkHeader.FirstLink = link;
                        break;

                    case "PREV":
                        linkHeader.PrevLink = link;
                        break;

                    case "NEXT":
                        linkHeader.NextLink = link;
                        break;

                    case "LAST":
                        linkHeader.LastLink = link;
                        break;
                    }
                }
            }

            return(linkHeader);
        }
        /// <summary>
        /// Gets list of visitors who are in segment specified by <paramref name="codename"/>.
        /// </summary>
        /// <param name="codename">Codename of segment.</param>
        /// <returns></returns>
        public async Task <List <string> > GetVisitorsInSegmentAsync(string codename)
        {
            if (string.IsNullOrEmpty(codename))
            {
                throw new ArgumentException("Codename must be set.", nameof(codename));
            }

            var uids     = new List <string>();
            var nextLink = $"{_segmentApiRoutePrefix}/{codename}/visitors";

            while (nextLink != null)
            {
                using (var response = await _httpClient.GetAsync(nextLink))
                {
                    nextLink = response.Headers.TryGetValues("Link", out var links) ? LinkHeader.LinksFromHeader(links.FirstOrDefault())?.NextLink : null;

                    var content = await DeserializeContent <VisitorsResponse>(response);

                    uids.AddRange(content.Visitors.Select(visitor => visitor.Uid));
                }
            }
            return(uids);
        }