public static CosmosClient CreateCosmosClient(this ClientContextOptions options)
        {
            options.Validate();

            var clientBuilder = new CosmosClientBuilder(
                options.ConnectionString);

            if (options.UseThrottling)
            {
                clientBuilder = clientBuilder
                                .WithThrottlingRetryOptions(
                    options.MaxRetryWaitTimeOnThrottledRequests ?? new TimeSpan(0, 0, 0, 30),
                    options.MaxRetryAttemptsOnThrottledRequests ?? 3);
            }

            //if (Enum.TryParse<ConsistencyLevel>(options.ConsistencyLevel, out var consistencyLevel))
            //    clientBuilder = clientBuilder
            //        .WithConsistencyLevel(consistencyLevel);

            if (options.AllowBulkExecution.HasValue)
            {
                clientBuilder = clientBuilder
                                .WithBulkExecution(options.AllowBulkExecution.Value);
            }

            if (options.CamelCasePropertyNames)
            {
                clientBuilder = clientBuilder.WithSerializerOptions(new CosmosSerializationOptions
                {
                    PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
                });
            }

            return(clientBuilder.Build());
        }
        public IClientContext Create(ClientContextOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var key     = options.GetContextIdentifier();
            var context = _clientContexts.GetOrAdd(key, CreateContext(options));

            return(context);
        }
        private IClientContext CreateContext(ClientContextOptions options)
        {
            options.Validate();

            if (options.Database.Api.Equals(DatabaseApi.SqlApi))
            {
                return(new CosmosSqlApiClientContext(options, _loggerFactory.CreateLogger <CosmosSqlApiClientContext>()));
            }
            else if (options.Database.Api.Equals(DatabaseApi.TableApi))
            {
                return(new CosmosTableApiClientContext(options, _loggerFactory.CreateLogger <CosmosTableApiClientContext>()));
            }
            else
            {
                throw new NotSupportedException($"api '{options.Database.Api}' is not currently supported in {nameof(CosmosClientContextFactory)}");
            }
        }
        private async Task RunAsync(ClientContextOptions options, IApplicationArgs args)
        {
            options.Validate();

            var context = _factory.Create(options);

            if (context is CosmosSqlApiClientContext sqlApiClientContext)
            {
                if (args.CreateDatabase)
                {
                    _logger.LogInformation($"creating database {options.Database.Id}");
                    await sqlApiClientContext.CreateDatabaseIfNotExistsAsync(options.Database.Id,
                                                                             options.Database.Throughput);
                }

                if (args.CreateContainers)
                {
                    _logger.LogInformation("creating containers");

                    var containerTasks = options
                                         .Database.Containers
                                         .Select(container => sqlApiClientContext.CreateContainerIfNotExistsAsync(container.Id, container.PartitionKey));

                    await Task.WhenAll(containerTasks);
                }
            }
            else if (context is CosmosTableApiClientContext tableApiClientContext)
            {
                if (args.CreateContainers)
                {
                    _logger.LogInformation("creating tables");

                    var tableTasks = options
                                     .Database.Containers
                                     .Select(table => tableApiClientContext.CreateTableIfNotExistsAsync(table.Id, table.Throughput));

                    await Task.WhenAll(tableTasks);
                }
            }
            else
            {
                throw new NotSupportedException($"no valid client context could be found to perform this operation");
            }
        }
        public static void Validate(this ClientContextOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrEmpty(options.ConnectionString))
            {
                throw new ArgumentNullException(nameof(options.ConnectionString));
            }

            if (options.Database == null)
            {
                throw new ArgumentNullException(nameof(options.Database));
            }

            options.Database.Validate();
        }
 public CosmosTableApiClientContext(ClientContextOptions options, ILogger <CosmosTableApiClientContext> logger)
 {
     _options = options;
     _logger  = logger;
 }