forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring cosmos client, options and adding user (Azure#139)
- 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
Showing
20 changed files
with
534 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
sdk/src/main/java/com/microsoft/azure/cosmos/CosmosClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.