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
- import static com .datastax .astra .client .admin . AstraDBAdmin .DEFAULT_KEYSPACE ;
27
+ import static com .datastax .astra .client .core . options . DataAPIClientOptions .DEFAULT_KEYSPACE ;
30
28
31
29
/**
32
30
* Provides utility methods for initializing and configuring clients to interact with the Data API. This class
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,76 +64,122 @@ 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 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 ) {
79
113
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 )
83
117
.enableFeatureFlagTables ()
84
- .logRequests ()
85
- .withObserver (new LoggingCommandObserver (DataAPIClient .class ))
86
- .build ());
118
+ .logRequests ());
87
119
}
88
120
89
121
/**
90
122
* 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.
95
126
*
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>
99
148
*/
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 );
104
152
return db ;
105
153
}
106
154
107
155
/**
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.
111
161
*
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.
121
166
*
122
167
* <p>Example usage:</p>
123
168
* <pre>
124
169
* {@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
127
172
* }
128
173
* </pre>
129
174
*/
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 ());
135
179
}
136
180
137
181
/**
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
139
183
* method simplifies the setup of a client specifically tailored for development purposes, where you might
140
184
* need different configurations or less stringent security measures compared to a production environment.
141
185
* 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) {
149
193
* <p>Example usage:</p>
150
194
* <pre>
151
195
* {@code
152
- * DataAPIClient devClient = DataAPIClients.astraDev ("your_astra_dev_token");
196
+ * DataAPIClient devClient = DataAPIClients.astra ("your_astra_dev_token");
153
197
* // Utilize devClient for development database operations
154
198
* }
155
199
* </pre>
156
200
*/
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 ());
163
205
}
164
206
207
+
208
+
165
209
/**
166
210
* 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.
171
223
*
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.
177
229
*
178
230
* <p>Example usage:</p>
179
231
* <pre>
180
232
* {@code
181
233
* 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());
183
235
* }
184
236
* </pre>
185
237
*/
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 ());
192
242
}
193
243
194
244
}
0 commit comments