Beispiel #1
0
        public static Task <IEnumerable <DesignDocument> > GetAllAsync(this IViewManager viewManager, Action <GetAllViewIndexOptions> configureOptions)
        {
            var options = new GetAllViewIndexOptions();

            configureOptions(options);

            return(viewManager.GetAllAsync(options));
        }
Beispiel #2
0
        public async Task <IEnumerable <DesignDocument> > GetAllAsync(GetAllViewIndexOptions options)
        {
            var uri = new UriBuilder
            {
                Scheme = _scheme,
                Host   = _clusterOptions.Servers.GetRandom().Host,
                Port   = 8091,
                Path   = $"pools/default/buckets/{_bucketName}/ddocs"
            }.Uri;

            Logger.LogInformation($"Attempting to get all design documents for bucket {_bucketName} - {uri}");

            try
            {
                var result = await _client.GetAsync(uri, options.CancellationToken).ConfigureAwait(false);

                result.EnsureSuccessStatusCode();

                var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                var json = JObject.Parse(content);

                var designDocuments = new List <DesignDocument>();
                if (json.TryGetValue("rows", out var rows))
                {
                    foreach (var row in rows)
                    {
                        var designDoc = new DesignDocument
                        {
                            Name = row.SelectToken("doc.meta.id").Value <string>().Replace("_design/", string.Empty)
                        };

                        foreach (var view in row.SelectTokens("doc.json.views"))
                        {
                            var name   = view.First.Path.Substring(view.First.Path.LastIndexOf(".") + 1);
                            var map    = view.First.First.SelectToken("map");
                            var reduce = view.First.First.SelectToken("reduce");
                            designDoc.Views.Add(name, new View
                            {
                                Map    = map.Value <string>(),
                                Reduce = reduce?.Value <string>()
                            });
                        }

                        designDocuments.Add(designDoc);
                    }
                }

                return(designDocuments);
            }
            catch (Exception exception)
            {
                Logger.LogError(exception, $"Failed to get all design documents for bucket {_bucketName} - {uri}");
                throw;
            }
        }