20
20
* #L%
21
21
*/
22
22
23
- import com .datastax .astra .client .admin .DataAPIDatabaseAdmin ;
24
23
import com .datastax .astra .client .core .auth .UsernamePasswordTokenProvider ;
25
24
import com .datastax .astra .client .core .options .DataAPIClientOptions ;
26
25
import com .datastax .astra .client .databases .Database ;
27
- import com .datastax .astra .internal .command .LoggingCommandObserver ;
28
26
29
27
import static com .datastax .astra .client .core .options .DataAPIClientOptions .DEFAULT_KEYSPACE ;
30
28
42
40
* <pre>
43
41
* {@code
44
42
* // Get you the client for a local deployment of Data API
45
- * DataAPIClient devClient = DataAPIClients.localClient ();
43
+ * DataAPIClient devClient = DataAPIClients.local ();
46
44
*
47
45
* // Get you the database for a local deployment of Data API
48
- * DataAPIClient devClient = DataAPIClients.localDatabase( );
46
+ * DataAPIClient devClient = DataAPIClients.astraDev("token" );
49
47
*
50
48
* // Default target environment Astra Production
51
49
* DataAPIClient devClient = DataAPIClients.astra("token");
@@ -66,23 +64,92 @@ public class DataAPIClients {
66
64
private DataAPIClients () {}
67
65
68
66
/**
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 .
73
71
*
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>
77
90
*/
78
- public static DataAPIClient local () {
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 ) {
79
98
return new DataAPIClient (
80
- new UsernamePasswordTokenProvider ().getToken (),
99
+ new UsernamePasswordTokenProvider (username , password ).getToken (),
81
100
new DataAPIClientOptions ()
82
101
.destination (DataAPIDestination .CASSANDRA )
83
102
.enableFeatureFlagTables ()
84
- .logRequests ()
85
- .addObserver (new LoggingCommandObserver (DataAPIClient .class )));
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 ) {
113
+ return new DataAPIClient (
114
+ new UsernamePasswordTokenProvider (username , password ).getToken (),
115
+ new DataAPIClientOptions ()
116
+ .destination (DataAPIDestination .HCD )
117
+ .enableFeatureFlagTables ()
118
+ .logRequests ());
119
+ }
120
+
121
+ /**
122
+ * Creates and configures a {@link Database} client specifically designed for interaction with a local instance
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.
126
+ *
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>
148
+ */
149
+ public static Database localDbWithDefaultKeyspace () {
150
+ Database db = clientCassandra ().getDatabase (DEFAULT_ENDPOINT_LOCAL );
151
+ db .getDatabaseAdmin ().createKeyspace (DEFAULT_KEYSPACE );
152
+ return db ;
86
153
}
87
154
88
155
/**
@@ -105,14 +172,14 @@ public static DataAPIClient local() {
105
172
* }
106
173
* </pre>
107
174
*/
108
- public static DataAPIClient astra (String token ) {
175
+ public static DataAPIClient astraDev (String token ) {
109
176
return new DataAPIClient (token , new DataAPIClientOptions ()
110
- .destination (DataAPIDestination .ASTRA )
111
- .addObserver ( new LoggingCommandObserver ( DataAPIClient . class ) ));
177
+ .destination (DataAPIDestination .ASTRA_DEV )
178
+ .logRequests ( ));
112
179
}
113
180
114
181
/**
115
- * 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
116
183
* method simplifies the setup of a client specifically tailored for development purposes, where you might
117
184
* need different configurations or less stringent security measures compared to a production environment.
118
185
* The client is configured to target Astra's development environment, ensuring that operations do not
@@ -126,59 +193,52 @@ public static DataAPIClient astra(String token) {
126
193
* <p>Example usage:</p>
127
194
* <pre>
128
195
* {@code
129
- * DataAPIClient devClient = DataAPIClients.astraDev ("your_astra_dev_token");
196
+ * DataAPIClient devClient = DataAPIClients.astra ("your_astra_dev_token");
130
197
* // Utilize devClient for development database operations
131
198
* }
132
199
* </pre>
133
200
*/
134
- public static DataAPIClient astraDev (String token ) {
201
+ public static DataAPIClient astra (String token ) {
135
202
return new DataAPIClient (token , new DataAPIClientOptions ()
136
- .destination (DataAPIDestination .ASTRA_DEV )
137
- .addObserver ( new LoggingCommandObserver ( DataAPIClient . class ) ));
203
+ .destination (DataAPIDestination .ASTRA )
204
+ .logRequests ( ));
138
205
}
139
206
207
+
208
+
140
209
/**
141
210
* Creates a {@link DataAPIClient} specifically configured for interacting with Astra in a test environment.
142
- * This setup is ideal for testing scenarios, where isolation from development and production environments
143
- * is critical to ensure the integrity and stability of test results. By directing the client to Astra's
144
- * test environment, it facilitates safe, isolated testing of database interactions without risking the
145
- * 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>
146
220
*
147
- * @param token The authentication token required for accessing Astra's test environment. Ensure that this
148
- * token is designated for testing purposes to prevent unintended access to or effects on
149
- * non-test data and resources.
150
- * @return A {@link DataAPIClient} instance specifically for use in testing scenarios with Astra, equipped
151
- * with the necessary authentication token and configured to target the test environment.
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.
223
+ *
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.
152
229
*
153
230
* <p>Example usage:</p>
154
231
* <pre>
155
232
* {@code
156
233
* DataAPIClient testClient = DataAPIClients.astraTest("your_astra_test_token");
157
- * // Execute test database operations with testClient
234
+ * testClient.execute(query -> query.cql("SELECT * FROM test_table").execute());
158
235
* }
159
236
* </pre>
160
237
*/
161
238
public static DataAPIClient astraTest (String token ) {
162
239
return new DataAPIClient (token , new DataAPIClientOptions ()
163
240
.destination (DataAPIDestination .ASTRA_TEST )
164
- .addObserver (new LoggingCommandObserver (DataAPIClient .class )));
165
- }
166
-
167
- /**
168
- * Creates and configures a {@link Database} client specifically designed for interaction with a local instance
169
- * of Stargate. This method streamlines the process of setting up a client for local database interactions,
170
- * encapsulating both the creation of a {@link DataAPIClient} and its integration within a {@link Database}
171
- * abstraction. This setup is ideal for local development and testing, providing a straightforward path to
172
- * interact with Cassandra through Stargate with minimal setup.
173
- *
174
- * @return A {@link Database} client ready for use with a local Stargate instance, fully configured for immediate
175
- * interaction with the database. This client enables developers to focus on their application logic rather
176
- * than the intricacies of database connectivity and command execution.
177
- */
178
- public static Database defaultLocalDatabase () {
179
- Database db = local ().getDatabase (DEFAULT_ENDPOINT_LOCAL );
180
- db .getDatabaseAdmin ().createKeyspace (DEFAULT_KEYSPACE );
181
- return db ;
241
+ .logRequests ());
182
242
}
183
243
184
244
}
0 commit comments