Skip to content

Commit f676993

Browse files
committed
refactoring
1 parent 0fceb70 commit f676993

File tree

53 files changed

+1675
-1602
lines changed

Some content is hidden

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

53 files changed

+1675
-1602
lines changed

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

Lines changed: 68 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@
2121
*/
2222

2323
import com.datastax.astra.client.admin.AstraDBAdmin;
24-
import com.datastax.astra.client.admin.AstraDBDatabaseAdmin;
25-
import com.datastax.astra.client.admin.DatabaseAdmin;
24+
import com.datastax.astra.client.admin.AdminOptions;
2625
import com.datastax.astra.client.core.options.DataAPIClientOptions;
2726
import com.datastax.astra.client.databases.Database;
27+
import com.datastax.astra.client.databases.DatabaseOptions;
2828
import com.datastax.astra.internal.api.AstraApiEndpoint;
2929
import com.datastax.astra.internal.utils.Assert;
3030

3131
import java.util.UUID;
3232

33-
import static com.datastax.astra.client.admin.AstraDBAdmin.DEFAULT_KEYSPACE;
3433
import static com.datastax.astra.client.exception.InvalidEnvironmentException.throwErrorRestrictedAstra;
3534

3635
/**
@@ -71,17 +70,6 @@
7170
*/
7271
public class DataAPIClient {
7372

74-
/** parameters names. */
75-
private static final String ARG_KEYSPACE = "keyspace";
76-
/** parameters names. */
77-
private static final String ARG_OPTIONS = "options";
78-
/** parameters names. */
79-
private static final String ARG_TOKEN = "token";
80-
/** parameters names. */
81-
private static final String ARG_DATABASE_ID = "databaseId";
82-
/** parameters names. */
83-
private static final String ARG_REGION = "region";
84-
8573
/**
8674
* The authentication token used as credentials in HTTP requests, specifically as the Authorization bearer token.
8775
* This token is crucial for accessing and interacting with Astra environments, where it plays a role in determining
@@ -140,7 +128,7 @@ public class DataAPIClient {
140128
* by the server, typically starting with "AstraCS:.." for Astra environments.
141129
*/
142130
public DataAPIClient(String token) {
143-
this(token, DataAPIClientOptions.builder().build());
131+
this(token, new DataAPIClientOptions());
144132
}
145133

146134
/**
@@ -180,12 +168,40 @@ public DataAPIClient(String token) {
180168
* @throws IllegalArgumentException if the token is empty or null, or if the options are null.
181169
*/
182170
public DataAPIClient(String token, DataAPIClientOptions options) {
183-
Assert.hasLength(token, ARG_TOKEN);
184-
Assert.notNull(options, ARG_OPTIONS);
171+
Assert.hasLength(token, "token");
172+
Assert.notNull(options, "options");
185173
this.token = token;
186174
this.options = options;
187175
}
188176

177+
/**
178+
* Constructs a new instance of the {@link DataAPIClient} without a default token. The token will instead need to
179+
* be specified when calling `.getDatabase()` or `.getAdmin()`. Prefer this method when using a db-scoped token
180+
* instead of a more universal token.
181+
*
182+
* <p>Example usage:</p>
183+
* <pre>
184+
* {@code
185+
* DataAPIClientOptions options = new DataAPIClientOptions()
186+
* .timeoutOptions(new TimeoutOptions()
187+
* .connectTimeoutMillis(1000)
188+
* .requestTimeoutMillis(1000))
189+
* .httpClientOptions(new HttpClientOptions()
190+
* .httpVersion(HttpClient.Version.HTTP_2)
191+
* );
192+
* DataAPIClient myClient = new DataAPIClient(myOptions);
193+
* // The client is now ready to perform actions with custom configurations.
194+
* }
195+
* </pre>
196+
*
197+
* @param options - The default options to use when spawning new instances of {@link Database} or {@link AstraDBAdmin}.
198+
*/
199+
public DataAPIClient(DataAPIClientOptions options) {
200+
Assert.notNull(options, "options");
201+
this.token = null;
202+
this.options = options;
203+
}
204+
189205
// --------------------------------------------------
190206
// --- Access AstraDBAdmin ---
191207
// --------------------------------------------------
@@ -218,7 +234,7 @@ public DataAPIClient(String token, DataAPIClientOptions options) {
218234
* @throws SecurityException if the current token does not have the necessary administrative privileges.
219235
*/
220236
public AstraDBAdmin getAdmin() {
221-
return getAdmin(this.token);
237+
return getAdmin(token, new AdminOptions(options));
222238
}
223239

224240
/**
@@ -251,168 +267,23 @@ public AstraDBAdmin getAdmin() {
251267
* @return An instance of {@link AstraDBAdmin}, configured for administrative tasks with the provided user token.
252268
* @throws SecurityException if the provided {@code superUserToken} lacks the necessary privileges for administrative operations.
253269
*/
254-
public AstraDBAdmin getAdmin(String superUserToken) {
270+
public AstraDBAdmin getAdmin(String superUserToken, AdminOptions adminOptions) {
255271
if (!options.isAstra()) {
256272
throwErrorRestrictedAstra("getAdmin()", options.getDestination());
257273
}
258-
return new AstraDBAdmin(superUserToken, options);
274+
return new AstraDBAdmin(adminOptions.adminToken(superUserToken));
259275
}
260276

261277
// --------------------------------------------------
262278
// --- Access Database ---
263279
// --------------------------------------------------
264280

265-
/**
266-
* Retrieves a client for a specific database, enabling interactions with the Data API. This method allows for
267-
* operations such as querying, updating, and managing data within the specified database and keyspace.
268-
* <p>
269-
* The {@code databaseId} parameter is a unique identifier for the target database. This ID ensures that operations
270-
* performed through the returned client are executed against the correct database instance within the Astra
271-
* environment or an on-premise DataStax Enterprise setup.
272-
* </p>
273-
* <p>
274-
* The {@code keyspace} parameter specifies the keyspace (often synonymous with "keyspace" in Cassandra terminology)
275-
* within the database to which the client will have access. Keyspaces are used to organize and isolate data within
276-
* the database, providing a layer of abstraction and security.
277-
* </p>
278-
*
279-
* <p>Example usage:</p>
280-
* <pre>
281-
* {@code
282-
* UUID databaseId = UUID.fromString("your-database-uuid-here");
283-
* String keyspace = "your_keyspace_here";
284-
* DataAPIClient apiClient = new DataAPIClient("yourAuthTokenHere");
285-
* Database databaseClient = apiClient.getDatabase(databaseId, keyspace);
286-
* // Use databaseClient to perform data operations within the specified keyspace
287-
* }
288-
* </pre>
289-
*
290-
* @param databaseId The unique identifier of the database to interact with.
291-
* @param keyspace The keyspace within the specified database to which operations will be scoped.
292-
* @return A {@link Database} client configured to interact with the specified database and keyspace, allowing for
293-
* data manipulation and query operations.
294-
*/
295-
public Database getDatabase(UUID databaseId, String keyspace) {
296-
Assert.notNull(databaseId, ARG_DATABASE_ID);
297-
Assert.hasLength(keyspace, ARG_KEYSPACE);
298-
if (!options.isAstra()) {
299-
throwErrorRestrictedAstra("getDatabase(id,keyspace)", options.getDestination());
300-
}
301-
return new Database(new AstraApiEndpoint(databaseId,
302-
getAdmin().getDatabaseInfo(databaseId).getRegion(),
303-
options.getAstraEnvironment()).getApiEndPoint(),
304-
this.token, keyspace, options);
305-
}
306-
307-
/**
308-
* Retrieves a client for a specific database, enabling interactions with the Data API. This method allows for
309-
* operations such as querying, updating, and managing data within the specified database and keyspace.
310-
*
311-
* @param databaseId The unique identifier of the database to interact with.
312-
* @param keyspace The keyspace associated to this database
313-
* @param region The cloud provider region where the database is deployed.
314-
*
315-
* @return
316-
* A {@link Database} client configured to interact with the specified database and keyspace, allowing for
317-
* data manipulation and query operations.
318-
*/
319-
public Database getDatabase(UUID databaseId, String keyspace, String region) {
320-
Assert.notNull(databaseId, ARG_DATABASE_ID);
321-
Assert.hasLength(keyspace, ARG_KEYSPACE);
322-
Assert.hasLength(region, ARG_REGION);
323-
if (!options.isAstra()) {
324-
throwErrorRestrictedAstra("getDatabase(dbId,keyspace,region)", options.getDestination());
325-
}
326-
return new Database(new AstraApiEndpoint(databaseId, region,
327-
options.getAstraEnvironment()).getApiEndPoint(),
328-
this.token, keyspace, options);
281+
public Database getDatabase(String apiEndpoint) {
282+
return getDatabase(apiEndpoint, new DatabaseOptions(options).token(token));
329283
}
330284

331-
/**
332-
* Retrieves a client for interacting with a specified database using its unique identifier. This client facilitates
333-
* direct communication with the Data API, enabling a range of operations such as querying, inserting, updating, and
334-
* deleting data within the database. This streamlined method provides access to the default keyspace or keyspace.
335-
* <p>
336-
* The {@code databaseId} serves as a unique identifier for the database within the Astra environment or an on-premise
337-
* DataStax Enterprise setup, ensuring that all operations through the client are correctly routed to the intended
338-
* database instance.
339-
* </p>
340-
*
341-
* <p>Example usage:</p>
342-
* <pre>
343-
* {@code
344-
* UUID databaseId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
345-
* DataAPIClient apiClient = new DataAPIClient("yourAuthTokenHere");
346-
* Database databaseClient = apiClient.getDatabase(databaseId);
347-
* // Perform data operations using the databaseClient
348-
* }
349-
* </pre>
350-
*
351-
* @param databaseId The unique identifier of the database for which to retrieve the client.
352-
* @return A {@link Database} client that provides the ability to perform operations on the specified database.
353-
*/
354285
public Database getDatabase(UUID databaseId) {
355-
return getDatabase(databaseId, DEFAULT_KEYSPACE);
356-
}
357-
358-
/**
359-
* Creates and returns a database client configured to interact with the Data API at the specified API endpoint
360-
* and within the given keyspace. This method facilitates operations such as querying, inserting, updating, and
361-
* deleting data by directly communicating with the Data API, allowing for a wide range of data manipulation
362-
* tasks in a specified keyspace.
363-
* <p>
364-
* The {@code apiEndpoint} parameter should be the URL of the API endpoint where the Data API is hosted. This
365-
* is typically a fully qualified URL that points to the Astra service or an on-premise DataStax Enterprise
366-
* deployment hosting the Data API.
367-
* </p>
368-
* <p>
369-
* The {@code keyspace} parameter specifies the keyspace (or keyspace in Cassandra terms) within the database
370-
* that the client will interact with. Keyspaces help organize data within the database and provide a way to
371-
* isolate and manage access to data.
372-
* </p>
373-
*
374-
* <p>Example usage:</p>
375-
* <pre>
376-
* {@code
377-
* String apiEndpoint = "https://example-astra-data-api.com";
378-
* String keyspace = "my_keyspace";
379-
* DataAPIClient apiClient = new DataAPIClient("yourAuthTokenHere");
380-
* Database databaseClient = apiClient.getDatabase(apiEndpoint, keyspace);
381-
* // Now you can use the databaseClient to perform operations within "my_keyspace"
382-
* }
383-
* </pre>
384-
*
385-
* @param apiEndpoint The URL of the Data API endpoint. This specifies the location of the API the client will connect to.
386-
* @param keyspace The keyspace within the database that the client will access and perform operations in.
387-
* @return A {@link Database} client configured for the specified API endpoint and keyspace, enabling data manipulation
388-
* and query operations within the targeted keyspace.
389-
*/
390-
public Database getDatabase(String apiEndpoint, String keyspace) {
391-
return new Database(apiEndpoint, token, keyspace, options);
392-
}
393-
394-
/**
395-
* Retrieves a client for interacting with a specified database using its unique identifier.
396-
*
397-
* @param apiEndpoint
398-
* apiEndpoint for the database.
399-
* @return
400-
* an instance of Database admin
401-
*/
402-
public DatabaseAdmin getDatabaseAdmin(String apiEndpoint) {
403-
return getDatabase(apiEndpoint).getDatabaseAdmin();
404-
}
405-
406-
/**
407-
* Retrieves a client for interacting with a specified database using its unique identifier.
408-
*
409-
* @param databaseId
410-
* database identifier
411-
* @return
412-
* the database admin if exists
413-
*/
414-
public AstraDBDatabaseAdmin getDatabaseAdmin(UUID databaseId) {
415-
return (AstraDBDatabaseAdmin) getDatabase(databaseId).getDatabaseAdmin();
286+
return getDatabase(lookupEndpoint(databaseId, null), new DatabaseOptions(options).token(token));
416287
}
417288

418289
/**
@@ -444,7 +315,34 @@ public AstraDBDatabaseAdmin getDatabaseAdmin(UUID databaseId) {
444315
* @return A {@link Database} client tailored for interaction with the Data API at the provided API endpoint,
445316
* ready for executing data manipulation tasks.
446317
*/
447-
public Database getDatabase(String apiEndpoint) {
448-
return getDatabase(apiEndpoint, DEFAULT_KEYSPACE);
318+
public Database getDatabase(String apiEndpoint, DatabaseOptions dbOptions) {
319+
return new Database(apiEndpoint, dbOptions);
449320
}
321+
322+
public Database getDatabase(UUID databaseId, DatabaseOptions dbOptions) {
323+
return getDatabase(databaseId, null, dbOptions);
324+
}
325+
326+
public Database getDatabase(UUID databaseId, String region, DatabaseOptions dbOptions) {
327+
return getDatabase(lookupEndpoint(databaseId, region), dbOptions);
328+
}
329+
330+
/**
331+
* Compute the endpoint for the database based on its ID and region.
332+
* @param databaseId
333+
* database ID (mandoatory)
334+
* @param region
335+
* region (optional), if not provided the default region of the database is used.
336+
* @return
337+
* the endpoint for the database
338+
*/
339+
private String lookupEndpoint(UUID databaseId, String region) {
340+
Assert.notNull(databaseId, "databaseId");
341+
String dbRegion = region;
342+
if (dbRegion == null) {
343+
dbRegion = getAdmin().getDatabaseInfo(databaseId).getRegion();
344+
}
345+
return new AstraApiEndpoint(databaseId, dbRegion, options.getAstraEnvironment()).getApiEndPoint();
346+
}
347+
450348
}

0 commit comments

Comments
 (0)