public async Task UpsertDesignDocumentAsync(DesignDocument designDocument, DesignDocumentNamespace @namespace, UpsertDesignDocumentOptions?options = null)
        {
            options ??= UpsertDesignDocumentOptions.Default;
            var json = JsonConvert.SerializeObject(designDocument);
            var uri  = GetUri(designDocument.Name, @namespace);

            _logger.LogInformation("Attempting to upsert design document {_bucketName}/{designDocument.Name} - {uri}",
                                   _redactor.MetaData(_bucketName), _redactor.MetaData(designDocument.Name), _redactor.SystemData(uri));
            _logger.LogDebug(json);

            try
            {
                var content = new StringContent(json, Encoding.UTF8, MediaType.Json);
                using var httpClient = _httpClientFactory.Create();
                var result = await httpClient.PutAsync(uri, content, options.TokenValue).ConfigureAwait(false);

                result.EnsureSuccessStatusCode();
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Failed to upsert design document {_bucketName}/{designDocument.Name} - {uri} - {json}",
                                 _redactor.MetaData(_bucketName), _redactor.MetaData(designDocument.Name), _redactor.SystemData(uri), _redactor.MetaData(json));
                throw;
            }
        }
Пример #2
0
        public async Task <IEnumerable <DesignDocument> > GetAllDesignDocumentsAsync(DesignDocumentNamespace @namespace, GetAllDesignDocumentsOptions?options = null)
        {
            options ??= GetAllDesignDocumentsOptions.Default;
            var uri = new UriBuilder(_serviceUriProvider.GetRandomManagementUri())
            {
                Path = $"pools/default/buckets/{_bucketName}/ddocs"
            }.Uri;

            _logger.LogInformation("Attempting to get all design documents for bucket {_bucketName} - {uri}",
                                   _redactor.MetaData(_bucketName), _redactor.SystemData(uri));

            try
            {
                var result = await _client.GetAsync(uri, options.TokenValue).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(".", StringComparison.Ordinal) + 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}",
                                 _redactor.MetaData(_bucketName), _redactor.SystemData(uri));
                throw;
            }
        }
Пример #3
0
        public async Task <IEnumerable <DesignDocument> > GetAllDesignDocumentsAsync(DesignDocumentNamespace @namespace, GetAllDesignDocumentsOptions options = null)
        {
            options = options ?? GetAllDesignDocumentsOptions.Default;
            var uri = new UriBuilder(_context.GetRandomNodeForService(ServiceType.KeyValue, _bucketName).ViewsUri)
            {
                Port = _context.ClusterOptions.MgmtPort,
                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;
            }
        }
        public async Task UpsertDesignDocumentAsync(DesignDocument designDocument, DesignDocumentNamespace @namespace, UpsertDesignDocumentOptions options)
        {
            var json = JsonConvert.SerializeObject(designDocument);
            var uri  = GetUri(designDocument.Name, @namespace);

            Logger.LogInformation($"Attempting to upsert design document {_bucketName}/{designDocument.Name} - {uri}");
            Logger.LogDebug(json);

            try
            {
                var content = new StringContent(json, Encoding.UTF8, MediaType.Json);
                var result  = await _client.PutAsync(uri, content, options.CancellationToken).ConfigureAwait(false);

                result.EnsureSuccessStatusCode();
            }
            catch (Exception exception)
            {
                Logger.LogError(exception, $"Failed to upsert design document {_bucketName}/{designDocument.Name} - {uri} - {json}");
                throw;
            }
        }
Пример #5
0
        public static Task UpsertDesignDocumentAsync(this IViewIndexManager viewManager, DesignDocument designDocument, DesignDocumentNamespace @namespace, Action <UpsertDesignDocumentOptions> configureOptions)
        {
            var options = new UpsertDesignDocumentOptions();

            configureOptions(options);

            return(viewManager.UpsertDesignDocumentAsync(designDocument, @namespace, options));
        }
Пример #6
0
 public static Task UpsertDesignDocumentAsync(this IViewIndexManager viewManager, DesignDocument designDocument, DesignDocumentNamespace @namespace)
 {
     return(viewManager.UpsertDesignDocumentAsync(designDocument, @namespace, UpsertDesignDocumentOptions.Default));
 }