Skip to content

Commit 040d39f

Browse files
authored
Merge pull request #30 from datastax/2.0-beta
2.0 beta
2 parents 242fa57 + 43a338a commit 040d39f

File tree

149 files changed

+5265
-3222
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+5265
-3222
lines changed

astra-db-java/src/main/java/com/datastax/astra/client/DataAPIClient.java

Lines changed: 219 additions & 239 deletions
Large diffs are not rendered by default.

astra-db-java/src/main/java/com/datastax/astra/client/DataAPIClients.java

Lines changed: 123 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
* #L%
2121
*/
2222

23-
import com.datastax.astra.client.admin.DataAPIDatabaseAdmin;
2423
import com.datastax.astra.client.core.auth.UsernamePasswordTokenProvider;
2524
import com.datastax.astra.client.core.options.DataAPIClientOptions;
2625
import com.datastax.astra.client.databases.Database;
27-
import com.datastax.astra.internal.command.LoggingCommandObserver;
2826

29-
import static com.datastax.astra.client.admin.AstraDBAdmin.DEFAULT_KEYSPACE;
27+
import static com.datastax.astra.client.core.options.DataAPIClientOptions.DEFAULT_KEYSPACE;
3028

3129
/**
3230
* Provides utility methods for initializing and configuring clients to interact with the Data API. This class
@@ -42,10 +40,10 @@
4240
* <pre>
4341
* {@code
4442
* // Get you the client for a local deployment of Data API
45-
* DataAPIClient devClient = DataAPIClients.localClient();
43+
* DataAPIClient devClient = DataAPIClients.local();
4644
*
4745
* // Get you the database for a local deployment of Data API
48-
* DataAPIClient devClient = DataAPIClients.localDatabase();
46+
* DataAPIClient devClient = DataAPIClients.astraDev("token");
4947
*
5048
* // Default target environment Astra Production
5149
* DataAPIClient devClient = DataAPIClients.astra("token");
@@ -66,76 +64,122 @@ public class DataAPIClients {
6664
private DataAPIClients() {}
6765

6866
/**
69-
* Creates and configures a {@link DataAPIClient} for interaction with a local instance of Stargate, a
70-
* data gateway for working with Apache Cassandra®. This method is specifically designed for scenarios
71-
* where the application is intended to communicate with a Stargate instance running locally, facilitating
72-
* development and testing workflows by providing easy access to local database resources.
67+
* Creates and configures a {@link DataAPIClient} for interaction with a local instance of DataAPI,
68+
* a data gateway that facilitates working with Apache Cassandra®. This method is tailored for
69+
* development and testing workflows, enabling simplified and efficient access to local database
70+
* resources without the need for extensive configuration.
7371
*
74-
* @return A fully configured {@link DataAPIClient} ready for interacting with the local Stargate instance, equipped
75-
* with the necessary authentication token and targeting options for Cassandra. This client abstracts away
76-
* the complexities of direct database communication, providing a simplified interface for data operations.
72+
* <p>The returned {@link DataAPIClient} is preconfigured with:
73+
* <ul>
74+
* <li>An authentication token from {@link UsernamePasswordTokenProvider}.</li>
75+
* <li>A destination set to {@code DataAPIDestination.CASSANDRA}.</li>
76+
* <li>Feature flags for tables enabled.</li>
77+
* <li>Request logging enabled.</li>
78+
* </ul>
79+
*
80+
* @return A fully configured {@link DataAPIClient} ready for interacting with the local DataAPI instance.
81+
* This client provides a streamlined interface for executing data operations, abstracting away
82+
* the complexity of direct database interactions.
83+
*
84+
* <p>Example usage:</p>
85+
* <pre>
86+
* {@code
87+
* DataAPIClient client = DataAPIClients.local();
88+
* }
89+
* </pre>
7790
*/
78-
public static DataAPIClient createForLocal() {
91+
public static DataAPIClient clientCassandra() {
92+
return clientCassandra(
93+
UsernamePasswordTokenProvider.DEFAULT_USERNAME,
94+
UsernamePasswordTokenProvider.DEFAULT_CREDENTIALS);
95+
}
96+
97+
public static DataAPIClient clientCassandra(String username, String password) {
98+
return new DataAPIClient(
99+
new UsernamePasswordTokenProvider(username, password).getToken(),
100+
new DataAPIClientOptions()
101+
.destination(DataAPIDestination.CASSANDRA)
102+
.enableFeatureFlagTables()
103+
.logRequests());
104+
}
105+
106+
public static DataAPIClient clientHCD() {
107+
return clientHCD(
108+
UsernamePasswordTokenProvider.DEFAULT_USERNAME,
109+
UsernamePasswordTokenProvider.DEFAULT_CREDENTIALS);
110+
}
111+
112+
public static DataAPIClient clientHCD(String username, String password) {
79113
return new DataAPIClient(
80-
new UsernamePasswordTokenProvider().getToken(),
81-
DataAPIClientOptions.builder()
82-
.withDestination(DataAPIDestination.CASSANDRA)
114+
new UsernamePasswordTokenProvider(username, password).getToken(),
115+
new DataAPIClientOptions()
116+
.destination(DataAPIDestination.HCD)
83117
.enableFeatureFlagTables()
84-
.logRequests()
85-
.withObserver(new LoggingCommandObserver(DataAPIClient.class))
86-
.build());
118+
.logRequests());
87119
}
88120

89121
/**
90122
* Creates and configures a {@link Database} client specifically designed for interaction with a local instance
91-
* of Stargate. This method streamlines the process of setting up a client for local database interactions,
92-
* encapsulating both the creation of a {@link DataAPIClient} and its integration within a {@link Database}
93-
* abstraction. This setup is ideal for local development and testing, providing a straightforward path to
94-
* interact with Cassandra through Stargate with minimal setup.
123+
* of the Data API and Cassandra. This method simplifies the setup process by combining the creation of a {@link DataAPIClient}
124+
* with the integration of a {@link Database} abstraction. It is tailored for local development and testing,
125+
* enabling seamless interaction with Apache Cassandra® through Stargate with minimal configuration.
95126
*
96-
* @return A {@link Database} client ready for use with a local Stargate instance, fully configured for immediate
97-
* interaction with the database. This client enables developers to focus on their application logic rather
98-
* than the intricacies of database connectivity and command execution.
127+
* <p>Upon creation, this method ensures that a default keyspace is available in the local Stargate instance
128+
* by automatically invoking {@link com.datastax.astra.client.admin.DatabaseAdmin#createKeyspace(String)}. This guarantees that developers
129+
* have a ready-to-use environment for executing database operations during their development or testing workflows.
130+
*
131+
* <p>The returned {@link Database} client is preconfigured with:
132+
* <ul>
133+
* <li>A connection to the default local Stargate endpoint.</li>
134+
* <li>An automatically created keyspace, identified by {@code DEFAULT_KEYSPACE}.</li>
135+
* </ul>
136+
* This setup allows developers to focus on application logic rather than database configuration or connectivity.
137+
*
138+
* @return A {@link Database} client configured for use with a local Stargate instance, including a default
139+
* keyspace for immediate interaction. This client abstracts database connectivity and administrative tasks,
140+
* streamlining development workflows.
141+
*
142+
* <p>Example usage:</p>
143+
* <pre>
144+
* {@code
145+
* Database db = localDbWithDefaultKeyspace();
146+
* }
147+
* </pre>
99148
*/
100-
public static Database defaultLocalDatabase() {
101-
Database db = createForLocal().getDatabase(DEFAULT_ENDPOINT_LOCAL, DEFAULT_KEYSPACE);
102-
DataAPIDatabaseAdmin dbAdmin = (DataAPIDatabaseAdmin) db.getDatabaseAdmin();
103-
dbAdmin.createKeyspace(DEFAULT_KEYSPACE);
149+
public static Database localDbWithDefaultKeyspace() {
150+
Database db = clientCassandra().getDatabase(DEFAULT_ENDPOINT_LOCAL);
151+
db.getDatabaseAdmin().createKeyspace(DEFAULT_KEYSPACE);
104152
return db;
105153
}
106154

107155
/**
108-
* Creates a {@link DataAPIClient} configured for interaction with Astra, DataStax's cloud-native database
109-
* as a service. This method streamlines the client setup by requiring only an authentication token, handling
110-
* the other configuration details internally to ensure compatibility with Astra's API and endpoints.
156+
* Creates a {@link DataAPIClient} configured for interacting with Astra in a development environment. This
157+
* method simplifies the setup of a client specifically tailored for development purposes, where you might
158+
* need different configurations or less stringent security measures compared to a production environment.
159+
* The client is configured to target Astra's development environment, ensuring that operations do not
160+
* affect production data.
111161
*
112-
* <p>By specifying the destination as Astra in the {@link DataAPIClientOptions}, this method ensures that the
113-
* client is properly configured to communicate with Astra's infrastructure, leveraging the provided token
114-
* for authentication. This approach enables developers to quickly establish a connection to Astra for
115-
* database operations without manually setting up connection parameters and authentication details.</p>
116-
**
117-
* @param token The authentication token required for accessing Astra. This token should be treated
118-
* securely and not exposed in public code repositories or unsecured locations.
119-
* @return A {@link DataAPIClient} instance ready for use with Astra, fully configured with the provided
120-
* authentication token and set to target Astra as its destination.
162+
* @param token The authentication token required for accessing Astra's development environment. This token
163+
* should have the necessary permissions for development activities and be protected accordingly.
164+
* @return A {@link DataAPIClient} instance ready for development activities with Astra, configured with the
165+
* provided authentication token and targeting Astra's development environment.
121166
*
122167
* <p>Example usage:</p>
123168
* <pre>
124169
* {@code
125-
* DataAPIClient astraClient = DataAPIClients.astra("my_astra_auth_token");
126-
* // Use astraClient for database operations
170+
* DataAPIClient devClient = DataAPIClients.astraDev("your_astra_dev_token");
171+
* // Utilize devClient for development database operations
127172
* }
128173
* </pre>
129174
*/
130-
public static DataAPIClient create(String token) {
131-
return new DataAPIClient(token, DataAPIClientOptions
132-
.builder()
133-
.withDestination(DataAPIDestination.ASTRA)
134-
.build());
175+
public static DataAPIClient astraDev(String token) {
176+
return new DataAPIClient(token, new DataAPIClientOptions()
177+
.destination(DataAPIDestination.ASTRA_DEV)
178+
.logRequests());
135179
}
136180

137181
/**
138-
* Creates a {@link DataAPIClient} configured for interacting with Astra in a development environment. This
182+
* Creates a {@link DataAPIClient} configured for interacting with Astra in a production environment. This
139183
* method simplifies the setup of a client specifically tailored for development purposes, where you might
140184
* need different configurations or less stringent security measures compared to a production environment.
141185
* The client is configured to target Astra's development environment, ensuring that operations do not
@@ -149,46 +193,52 @@ public static DataAPIClient create(String token) {
149193
* <p>Example usage:</p>
150194
* <pre>
151195
* {@code
152-
* DataAPIClient devClient = DataAPIClients.astraDev("your_astra_dev_token");
196+
* DataAPIClient devClient = DataAPIClients.astra("your_astra_dev_token");
153197
* // Utilize devClient for development database operations
154198
* }
155199
* </pre>
156200
*/
157-
public static DataAPIClient createForAstraDev(String token) {
158-
return new DataAPIClient(token, DataAPIClientOptions
159-
.builder()
160-
.withDestination(DataAPIDestination.ASTRA_DEV)
161-
.withObserver(new LoggingCommandObserver(DataAPIClient.class))
162-
.build());
201+
public static DataAPIClient astra(String token) {
202+
return new DataAPIClient(token, new DataAPIClientOptions()
203+
.destination(DataAPIDestination.ASTRA)
204+
.logRequests());
163205
}
164206

207+
208+
165209
/**
166210
* Creates a {@link DataAPIClient} specifically configured for interacting with Astra in a test environment.
167-
* This setup is ideal for testing scenarios, where isolation from development and production environments
168-
* is critical to ensure the integrity and stability of test results. By directing the client to Astra's
169-
* test environment, it facilitates safe, isolated testing of database interactions without risking the
170-
* alteration of development or production data.
211+
* This method is designed for testing scenarios, providing an isolated environment to safely execute
212+
* database operations without impacting development or production data.
213+
*
214+
* <p>The returned {@link DataAPIClient} is preconfigured to:
215+
* <ul>
216+
* <li>Authenticate using the provided test-specific token.</li>
217+
* <li>Target the {@code DataAPIDestination.ASTRA_TEST} environment.</li>
218+
* <li>Enable request logging for better visibility during test operations.</li>
219+
* </ul>
220+
*
221+
* This setup ensures that all database interactions are restricted to Astra's test environment,
222+
* preserving the integrity of other environments while facilitating thorough testing.
171223
*
172-
* @param token The authentication token required for accessing Astra's test environment. Ensure that this
173-
* token is designated for testing purposes to prevent unintended access to or effects on
174-
* non-test data and resources.
175-
* @return A {@link DataAPIClient} instance specifically for use in testing scenarios with Astra, equipped
176-
* with the necessary authentication token and configured to target the test environment.
224+
* @param token The authentication token required for accessing Astra's test environment. It is important
225+
* to use a token that is explicitly designated for testing purposes to avoid unintended
226+
* access to production or development resources.
227+
* @return A {@link DataAPIClient} instance configured for testing with Astra, equipped with the provided
228+
* authentication token and targeting the test environment.
177229
*
178230
* <p>Example usage:</p>
179231
* <pre>
180232
* {@code
181233
* DataAPIClient testClient = DataAPIClients.astraTest("your_astra_test_token");
182-
* // Execute test database operations with testClient
234+
* testClient.execute(query -> query.cql("SELECT * FROM test_table").execute());
183235
* }
184236
* </pre>
185237
*/
186-
public static DataAPIClient createForAstraTest(String token) {
187-
return new DataAPIClient(token, DataAPIClientOptions
188-
.builder()
189-
.withDestination(DataAPIDestination.ASTRA_TEST)
190-
.withObserver(new LoggingCommandObserver(DataAPIClient.class))
191-
.build());
238+
public static DataAPIClient astraTest(String token) {
239+
return new DataAPIClient(token, new DataAPIClientOptions()
240+
.destination(DataAPIDestination.ASTRA_TEST)
241+
.logRequests());
192242
}
193243

194244
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.datastax.astra.client.admin;
2+
3+
/*-
4+
* #%L
5+
* Data API Java Client
6+
* --
7+
* Copyright (C) 2024 DataStax
8+
* --
9+
* Licensed under the Apache License, Version 2.0
10+
* You may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import com.datastax.astra.client.core.commands.BaseOptions;
24+
import com.datastax.astra.client.core.commands.CommandType;
25+
import com.datastax.astra.client.core.options.DataAPIClientOptions;
26+
import com.datastax.astra.internal.serdes.DataAPISerializer;
27+
import com.datastax.astra.internal.serdes.DatabaseSerializer;
28+
import lombok.Setter;
29+
import lombok.experimental.Accessors;
30+
31+
@Setter
32+
@Accessors(fluent = true, chain = true)
33+
public class AdminOptions extends BaseOptions<AdminOptions> {
34+
35+
/** Serializer for the Collections. */
36+
private static final DataAPISerializer DEFAULT_SERIALIZER = new DatabaseSerializer();
37+
38+
public AdminOptions() {
39+
this(null, null);
40+
}
41+
42+
public AdminOptions(String token, DataAPIClientOptions options) {
43+
super(token, CommandType.DATABASE_ADMIN, DEFAULT_SERIALIZER, options);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)