Skip to content

Commit

Permalink
Refactoring cosmos client, options and adding user (Azure#139)
Browse files Browse the repository at this point in the history
- closes Azure#151 

* Refactoring CosmosClient adding a Builder
Removed cosmos configuration
Adding getters for scripts
Adding CosmosUser
Changing Options to composition

* Minor refactoring
Adding listUsers and queryUsers

* Implementing PR comments

* Fixing javadoc warnings

* Fixing typo in doc

* Merging V3

* Fixing doc issues
  • Loading branch information
mbhaskar authored and Christopher Anderson committed Jun 8, 2019
1 parent a39813a commit f410e4f
Show file tree
Hide file tree
Showing 20 changed files with 534 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public static void main(String[] args) {

private void start(){
// Get client
client = CosmosClient.create(AccountSettings.HOST, AccountSettings.MASTER_KEY);
client = CosmosClient.builder()
.endpoint(AccountSettings.HOST)
.key(AccountSettings.MASTER_KEY)
.build();

//Create a database and a container
createDbAndContainerBlocking();
Expand Down
91 changes: 65 additions & 26 deletions sdk/src/main/java/com/microsoft/azure/cosmos/CosmosClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
package com.microsoft.azure.cosmos;

import com.microsoft.azure.cosmosdb.BridgeInternal;
import com.microsoft.azure.cosmosdb.ConnectionPolicy;
import com.microsoft.azure.cosmosdb.ConsistencyLevel;
import com.microsoft.azure.cosmosdb.Database;
import com.microsoft.azure.cosmosdb.DocumentClientException;
import com.microsoft.azure.cosmosdb.FeedOptions;
import com.microsoft.azure.cosmosdb.FeedResponse;
import com.microsoft.azure.cosmosdb.Permission;
import com.microsoft.azure.cosmosdb.SqlQuerySpec;
import com.microsoft.azure.cosmosdb.internal.HttpConstants;
import com.microsoft.azure.cosmosdb.rx.AsyncDocumentClient;
Expand All @@ -35,49 +38,84 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
* Provides a client-side logical representation of the Azure Cosmos database service.
* This asynchronous client is used to configure and execute requests
* against the service.
*/
public class CosmosClient {
public class CosmosClient {

//Document client wrapper
private AsyncDocumentClient asyncDocumentClient;
private final AsyncDocumentClient asyncDocumentClient;
private final String serviceEndpoint;
private final String keyOrResourceToken;
private final ConnectionPolicy connectionPolicy;
private final ConsistencyLevel desiredConsistencyLevel;
private final List<Permission> permissions;


CosmosClient(CosmosClientBuilder builder) {
this.serviceEndpoint = builder.getServiceEndpoint();
this.keyOrResourceToken = builder.getKeyOrResourceToken();
this.connectionPolicy = builder.getConnectionPolicy();
this.desiredConsistencyLevel = builder.getDesiredConsistencyLevel();
this.permissions = builder.getPermissions();
this.asyncDocumentClient = new AsyncDocumentClient.Builder()
.withServiceEndpoint(this.serviceEndpoint)
.withMasterKeyOrResourceToken(this.keyOrResourceToken)
.withConnectionPolicy(this.connectionPolicy)
.withConsistencyLevel(this.desiredConsistencyLevel)
.build();
}

/**
* Creates a cosmos client with given cosmosConfiguration
* @param cosmosConfiguration the cosmosConfiguration
* @return cosmos client
* Instantiate the cosmos client builder to build cosmos client
* @return {@link CosmosClientBuilder}
*/
public static CosmosClient create(CosmosConfiguration cosmosConfiguration) {
return new CosmosClient(cosmosConfiguration);
public static CosmosClientBuilder builder(){
return new CosmosClientBuilder();
}

/**
* Creates a cosmos client with given endpoint and key
* @param endpoint the service end point
* @param key the key
* @return cosmos clients
* Get the service endpoint
* @return the service endpoint
*/
public static CosmosClient create(String endpoint, String key) {
CosmosConfiguration cosmosConfiguration = new CosmosConfiguration.Builder()
.withServiceEndpoint(endpoint)
.withKeyOrResourceToken(key).build();
return create(cosmosConfiguration);
public String getServiceEndpoint() {
return serviceEndpoint;
}

/**
* Creates a cosmos client with given cosmos configuration
* @param cosmosConfiguration the cosmos configuration
* Gets the key or resource token
* @return get the key or resource token
*/
private CosmosClient(CosmosConfiguration cosmosConfiguration) {
this.asyncDocumentClient = new AsyncDocumentClient.Builder()
.withServiceEndpoint(cosmosConfiguration.getServiceEndpoint().toString())
.withMasterKeyOrResourceToken(cosmosConfiguration.getKeyOrResourceToken())
.withConnectionPolicy(cosmosConfiguration.getConnectionPolicy())
.withConsistencyLevel(cosmosConfiguration.getDesiredConsistencyLevel())
.build();
String getKeyOrResourceToken() {
return keyOrResourceToken;
}

/**
* Get the connection policy
* @return {@link ConnectionPolicy}
*/
public ConnectionPolicy getConnectionPolicy() {
return connectionPolicy;
}

/**
* Gets the consistency level
* @return the (@link ConsistencyLevel)
*/
public ConsistencyLevel getDesiredConsistencyLevel() {
return desiredConsistencyLevel;
}

/**
* Gets the permission list
* @return the permission list
*/
public List<Permission> getPermissions() {
return permissions;
}

AsyncDocumentClient getDocClientWrapper(){
Expand Down Expand Up @@ -239,8 +277,9 @@ public Flux<FeedResponse<CosmosDatabaseSettings>> queryDatabases(SqlQuerySpec qu

/**
* Gets a database object without making a service call.
*
* @param id name of the database
* @return
* @return {@link CosmosDatabase}
*/
public CosmosDatabase getDatabase(String id) {
return new CosmosDatabase(id, this);
Expand Down
152 changes: 152 additions & 0 deletions sdk/src/main/java/com/microsoft/azure/cosmos/CosmosClientBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* The MIT License (MIT)
* Copyright (c) 2018 Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.microsoft.azure.cosmos;

import com.microsoft.azure.cosmosdb.ConnectionPolicy;
import com.microsoft.azure.cosmosdb.ConsistencyLevel;
import com.microsoft.azure.cosmosdb.Permission;

import java.util.List;

/**
* Helper class to build {@link CosmosClient} instances
* as logical representation of the Azure Cosmos database service.
*
* <pre>
* {@code
* ConnectionPolicy connectionPolicy = new ConnectionPolicy();
* connectionPolicy.setConnectionMode(ConnectionMode.Direct);
* CosmonsClient client = new CosmosClient.builder()
* .endpoint(serviceEndpoint)
* .key(key)
* .connectionPolicy(connectionPolicy)
* .consistencyLevel(ConsistencyLevel.Session)
* .build();
* }
* </pre>
*/
public class CosmosClientBuilder {

private String serviceEndpoint;
private String keyOrResourceToken;
private ConnectionPolicy connectionPolicy;
private ConsistencyLevel desiredConsistencyLevel;
private List<Permission> permissions;

CosmosClientBuilder() {
}

/**
* The service endpoint url
* @param serviceEndpoint the service endpoint
* @return current Builder
*/
public CosmosClientBuilder endpoint(String serviceEndpoint) {
this.serviceEndpoint = serviceEndpoint;
return this;
}

/**
* This method will take either key or resource token and perform authentication
* for accessing resource.
*
* @param keyOrResourceToken key or resourceToken for authentication .
* @return current Builder.
*/
public CosmosClientBuilder key(String keyOrResourceToken) {
this.keyOrResourceToken = keyOrResourceToken;
return this;
}

/**
* This method will accept the permission list , which contains the
* resource tokens needed to access resources.
*
* @param permissions Permission list for authentication.
* @return current Builder.
*/
public CosmosClientBuilder permissions(List<Permission> permissions) {
this.permissions = permissions;
return this;
}

/**
* This method accepts the (@link ConsistencyLevel) to be used
* @param desiredConsistencyLevel {@link ConsistencyLevel}
* @return current Builder
*/
public CosmosClientBuilder consistencyLevel(ConsistencyLevel desiredConsistencyLevel) {
this.desiredConsistencyLevel = desiredConsistencyLevel;
return this;
}

/**
* The (@link ConnectionPolicy) to be used
* @param connectionPolicy {@link ConnectionPolicy}
* @return current Builder
*/
public CosmosClientBuilder connectionPolicy(ConnectionPolicy connectionPolicy) {
this.connectionPolicy = connectionPolicy;
return this;
}

private void ifThrowIllegalArgException(boolean value, String error) {
if (value) {
throw new IllegalArgumentException(error);
}
}

/**
* Builds a cosmos configuration object with the provided settings
* @return CosmosClient
*/
public CosmosClient build() {

ifThrowIllegalArgException(this.serviceEndpoint == null, "cannot build client without service endpoint");
ifThrowIllegalArgException(
this.keyOrResourceToken == null && (permissions == null || permissions.isEmpty()),
"cannot build client without key or resource token");

return new CosmosClient(this);
}

String getServiceEndpoint() {
return serviceEndpoint;
}

String getKeyOrResourceToken() {
return keyOrResourceToken;
}

ConnectionPolicy getConnectionPolicy() {
return connectionPolicy;
}

ConsistencyLevel getDesiredConsistencyLevel() {
return desiredConsistencyLevel;
}

List<Permission> getPermissions() {
return permissions;
}
}
Loading

0 comments on commit f410e4f

Please sign in to comment.