Пример #1
0
        public async Task CreateCollectionAsync(CollectionSpec spec, CreateCollectionOptions?options = null)
        {
            options ??= CreateCollectionOptions.Default;
            var uri = GetUri(spec.ScopeName);

            _logger.LogInformation($"Attempting create collection {spec.ScopeName}/{spec.Name} - {uri}");

            try
            {
                // create collection
                var keys = new Dictionary <string, string>
                {
                    { "name", spec.Name }
                };
                var content      = new FormUrlEncodedContent(keys);
                var createResult = await _client.PostAsync(uri, content, options.TokenValue).ConfigureAwait(false);

                createResult.EnsureSuccessStatusCode();
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, $"Failed to create collection {spec.ScopeName}/{spec.Name} - {uri}");
                throw;
            }
        }
        public async Task CreateCollectionAsync(CollectionSpec spec, CreateCollectionOptions?options = null)
        {
            options ??= CreateCollectionOptions.Default;
            var uri = GetUri(spec.ScopeName);

            _logger.LogInformation("Attempting create collection {spec.ScopeName}/{spec.Name} - {uri}", spec.ScopeName,
                                   spec.Name, _redactor.SystemData(uri));

            try
            {
                // create collection
                var keys = new Dictionary <string, string>
                {
                    { "name", spec.Name }
                };

                if (spec.MaxExpiry.HasValue)
                {
                    keys.Add("maxTTL", spec.MaxExpiry.Value.TotalSeconds.ToString(CultureInfo.InvariantCulture));
                }
                var content      = new FormUrlEncodedContent(keys);
                var createResult = await _client.PostAsync(uri, content, options.TokenValue).ConfigureAwait(false);

                createResult.EnsureSuccessStatusCode();
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Failed to create collection {spec.ScopeName}/{spec.Name} - {uri}", spec.ScopeName,
                                 spec.Name, _redactor.SystemData(uri));
                throw;
            }
        }
        public async Task CreateCollectionAsync(CollectionSpec spec, CreateCollectionOptions?options = null)
        {
            options ??= CreateCollectionOptions.Default;
            var uri = GetUri(RestApi.CreateCollections(_bucketName, spec.ScopeName));

            _logger.LogInformation("Attempting create collection {spec.ScopeName}/{spec.Name} - {uri}", spec.ScopeName,
                                   spec.Name, _redactor.SystemData(uri));

            try
            {
                // create collection
                var keys = new Dictionary <string, string>
                {
                    { "name", spec.Name }
                };

                if (spec.MaxExpiry.HasValue)
                {
                    keys.Add("maxTTL", spec.MaxExpiry.Value.TotalSeconds.ToString(CultureInfo.InvariantCulture));
                }
                var content      = new FormUrlEncodedContent(keys);
                var createResult = await _client.PostAsync(uri, content, options.TokenValue).ConfigureAwait(false);

                if (createResult.StatusCode != HttpStatusCode.OK)
                {
                    var contentBody = await createResult.Content.ReadAsStringAsync();

                    if (contentBody.Contains("already exists"))
                    {
                        throw new CollectionExistsException(spec.ScopeName, spec.Name);
                    }

                    if (contentBody.Contains("not found"))
                    {
                        throw new ScopeNotFoundException(spec.ScopeName);
                    }

                    throw new CouchbaseException(contentBody);
                }
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Failed to create collection {spec.ScopeName}/{spec.Name} - {uri}", spec.ScopeName,
                                 spec.Name, _redactor.SystemData(uri));
                throw;
            }
        }
Пример #4
0
        public async Task AllowQueryingAsync(string indexName, AllowQueryingSearchIndexOptions?options = null)
        {
            options ??= AllowQueryingSearchIndexOptions.Default;
            var baseUri = GetQueryControlUri(indexName, true);

            _logger.LogInformation("Trying to allow querying for index with name {indexName} - {baseUri}",
                                   _redactor.MetaData(indexName), _redactor.SystemData(baseUri));

            try
            {
                var result = await _client.PostAsync(baseUri, null, options.TokenValue).ConfigureAwait(false);

                if (result.StatusCode == HttpStatusCode.NotFound)
                {
                    throw new SearchIndexNotFound(indexName);
                }

                result.EnsureSuccessStatusCode();
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Failed to allow querying for index with name {indexName} - {baseUri}",
                                 _redactor.MetaData(indexName), _redactor.SystemData(baseUri));
                throw;
            }
        }
        public async Task CreateBucketAsync(BucketSettings settings, CreateBucketOptions?options = null)
        {
            options ??= new CreateBucketOptions();
            var uri = GetUri();

            _logger.LogInformation("Attempting to create bucket with name {settings.Name} - {uri}",
                                   _redactor.MetaData(settings.Name), _redactor.SystemData(uri));

            try
            {
                // create bucket
                var content = new FormUrlEncodedContent(GetBucketSettingAsFormValues(settings));
                var result  = await _client.PostAsync(uri, content, options.TokenValue).ConfigureAwait(false);

                if (result.StatusCode == HttpStatusCode.BadRequest)
                {
                    var json = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                    if (json.IndexOf("Bucket with given name already exists", StringComparison.InvariantCultureIgnoreCase) >= 0)
                    {
                        throw new BucketExistsException(settings.Name);
                    }
                }

                result.EnsureSuccessStatusCode();
            }
            catch (BucketExistsException)
            {
                _logger.LogError("Failed to create bucket with name {settings.Name} because it already exists",
                                 _redactor.MetaData(settings.Name));
                throw;
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Failed to create bucket with name {settings.Name} - {uri}",
                                 _redactor.MetaData(settings.Name), _redactor.SystemData(uri));
                throw;
            }
        }
Пример #6
0
 public async Task CreateLinkAsync(AnalyticsLink link, CreateAnalyticsLinkOptions?options = null)
 {
     link.ValidateForRequest();
     options ??= new();
     try
     {
         var builder = new UriBuilder(_serviceUriProvider.GetRandomAnalyticsUri());
         builder.Path = link.ManagementPath;
         var uri         = builder.Uri;
         var formContent = new FormUrlEncodedContent(link.FormData);
         var result      = await _couchbaseHttpClient.PostAsync(uri, formContent, options.CancellationToken).ConfigureAwait(false);
         await HandleLinkManagementResultErrors(result, link);
     }
     catch (Exception exception)
     {
         _logger.LogError(exception, "Failed to create link.");
         throw;
     }
 }