Skip to content

Commit

Permalink
Rename subscription key to ApiKey (Azure#7869)
Browse files Browse the repository at this point in the history
* renaming subscription key to ApiKey
  • Loading branch information
mssfang authored Feb 6, 2020
1 parent 47be677 commit e8b664e
Show file tree
Hide file tree
Showing 47 changed files with 273 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
<suppress checks="com.azure.tools.checkstyle.checks.HttpPipelinePolicy" files="com.azure.security.keyvault.secrets.implementation.KeyVaultCredentialPolicy.java"/>
<suppress checks="com.azure.tools.checkstyle.checks.HttpPipelinePolicy" files="com.azure.security.keyvault.certificates.implementation.KeyVaultCredentialPolicy.java"/>
<suppress checks="com.azure.tools.checkstyle.checks.HttpPipelinePolicy" files="com.azure.security.keyvault.keys.implementation.KeyVaultCredentialPolicy.java"/>
<suppress checks="com.azure.tools.checkstyle.checks.HttpPipelinePolicy" files="com.azure.ai.textanalytics.implementation.SubscriptionKeyCredentialPolicy.java"/>
<suppress checks="com.azure.tools.checkstyle.checks.HttpPipelinePolicy" files="com.azure.ai.textanalytics.implementation.ApiKeyCredentialPolicy.java"/>

<!-- Empty while loop waiting for Reactor stream completion -->
<suppress checks="EmptyBlock" files="com.azure.storage.blob.batch.BlobBatch.java"/>
Expand Down
100 changes: 61 additions & 39 deletions sdk/textanalytics/azure-ai-textanalytics/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.azure.ai.textanalytics;

import com.azure.ai.textanalytics.implementation.SubscriptionKeyCredentialPolicy;
import com.azure.ai.textanalytics.implementation.ApiKeyCredentialPolicy;
import com.azure.ai.textanalytics.implementation.TextAnalyticsClientImpl;
import com.azure.ai.textanalytics.implementation.TextAnalyticsClientImplBuilder;
import com.azure.ai.textanalytics.models.TextAnalyticsApiKeyCredential;
Expand Down Expand Up @@ -44,8 +44,7 @@
*
* <p>
* The client needs the service endpoint of the Azure Text Analytics to access the resource service.
* {@link #subscriptionKey(TextAnalyticsApiKeyCredential)
* subscriptionKey(TextAnalyticsApiKeyCredential)} or
* {@link #apiKey(TextAnalyticsApiKeyCredential) apiKey(TextAnalyticsApiKeyCredential)} or
* {@link #credential(TokenCredential) credential(TokenCredential)} give the builder access credential.
* </p>
*
Expand Down Expand Up @@ -128,7 +127,7 @@ public TextAnalyticsClientBuilder() {
*
* @return A TextAnalyticsClient with the options set from the builder.
* @throws NullPointerException if {@link #endpoint(String) endpoint} or
* {@link #subscriptionKey(TextAnalyticsApiKeyCredential) subscriptionKey} has not been set.
* {@link #apiKey(TextAnalyticsApiKeyCredential) apiKey} has not been set.
* @throws IllegalArgumentException if {@link #endpoint(String) endpoint} cannot be parsed into a valid URL.
*/
public TextAnalyticsClient buildClient() {
Expand All @@ -147,7 +146,7 @@ public TextAnalyticsClient buildClient() {
*
* @return A TextAnalyticsAsyncClient with the options set from the builder.
* @throws NullPointerException if {@link #endpoint(String) endpoint} or
* {@link #subscriptionKey(TextAnalyticsApiKeyCredential) subscriptionKey} has not been set.
* {@link #apiKey(TextAnalyticsApiKeyCredential) apiKey} has not been set.
* @throws IllegalArgumentException if {@link #endpoint(String) endpoint} cannot be parsed into a valid URL.
*/
public TextAnalyticsAsyncClient buildAsyncClient() {
Expand All @@ -172,7 +171,7 @@ public TextAnalyticsAsyncClient buildAsyncClient() {
// User token based policy
policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, DEFAULT_SCOPE));
} else if (credential != null) {
policies.add(new SubscriptionKeyCredentialPolicy(credential));
policies.add(new ApiKeyCredentialPolicy(credential));
} else {
// Throw exception that credential and tokenCredential cannot be null
throw logger.logExceptionAsError(
Expand Down Expand Up @@ -234,7 +233,7 @@ public TextAnalyticsClientBuilder endpoint(String endpoint) {
try {
new URL(endpoint);
} catch (MalformedURLException ex) {
throw logger.logExceptionAsWarning(new IllegalArgumentException("'endpoint' must be a valid URL", ex));
throw logger.logExceptionAsWarning(new IllegalArgumentException("'endpoint' must be a valid URL.", ex));
}

if (endpoint.endsWith("/")) {
Expand All @@ -249,15 +248,13 @@ public TextAnalyticsClientBuilder endpoint(String endpoint) {
/**
* Sets the credential to use when authenticating HTTP requests for this TextAnalyticsClientBuilder.
*
* @param subscriptionKeyCredential subscription key credential
* @param apiKeyCredential API key credential
*
* @return The updated TextAnalyticsClientBuilder object.
* @throws NullPointerException If {@code subscriptionKeyCredential} is {@code null}
* @throws NullPointerException If {@code apiKeyCredential} is {@code null}
*/
public TextAnalyticsClientBuilder subscriptionKey(
TextAnalyticsApiKeyCredential subscriptionKeyCredential) {
Objects.requireNonNull(subscriptionKeyCredential, "'subscriptionKeyCredential' cannot be null.");
this.credential = subscriptionKeyCredential;
public TextAnalyticsClientBuilder apiKey(TextAnalyticsApiKeyCredential apiKeyCredential) {
this.credential = Objects.requireNonNull(apiKeyCredential, "'apiKeyCredential' cannot be null.");
return this;
}

Expand Down Expand Up @@ -297,8 +294,7 @@ public TextAnalyticsClientBuilder httpLogOptions(HttpLogOptions logOptions) {
* @throws NullPointerException If {@code policy} is {@code null}.
*/
public TextAnalyticsClientBuilder addPolicy(HttpPipelinePolicy policy) {
Objects.requireNonNull(policy, "'policy' cannot be null.");
policies.add(policy);
policies.add(Objects.requireNonNull(policy, "'policy' cannot be null."));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@
* Policy that adds the {@link TextAnalyticsApiKeyCredential} into the request's `Ocp-Apim-Subscription-Key`
* header.
*/
public final class SubscriptionKeyCredentialPolicy implements HttpPipelinePolicy {
public final class ApiKeyCredentialPolicy implements HttpPipelinePolicy {
private static final String OCP_APIM_SUBSCRIPTION_KEY = "Ocp-Apim-Subscription-Key";
private final TextAnalyticsApiKeyCredential credential;

/**
* Creates a {@link SubscriptionKeyCredentialPolicy} pipeline policy that adds the
* Creates a {@link ApiKeyCredentialPolicy} pipeline policy that adds the
* {@link TextAnalyticsApiKeyCredential} into the request's `Ocp-Apim-Subscription-Key` header.
*
* @param credential the {@link TextAnalyticsApiKeyCredential} credential used to create the policy.
*/
public SubscriptionKeyCredentialPolicy(TextAnalyticsApiKeyCredential credential) {
public ApiKeyCredentialPolicy(TextAnalyticsApiKeyCredential credential) {
this.credential = credential;
}

@Override
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
context.getHttpRequest().setHeader(OCP_APIM_SUBSCRIPTION_KEY, credential.getSubscriptionKey());
context.getHttpRequest().setHeader(OCP_APIM_SUBSCRIPTION_KEY, credential.getApiKey());
return next.process();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,49 @@

package com.azure.ai.textanalytics.models;

import com.azure.core.util.logging.ClientLogger;

import java.util.Objects;

/**
* Subscription key credential that shared across cognitive services, or restrict to single service.
* API key credential that shared across cognitive services, or restrict to single service.
*
* <p>Be able to rotate an existing subscription key</p>
* <p>Be able to rotate an existing API key</p>
* {@codesnippet com.azure.ai.textanalytics.models.TextAnalyticsApiKeyCredential}
*
*/
public final class TextAnalyticsApiKeyCredential {
private volatile String subscriptionKey;
private final ClientLogger logger = new ClientLogger(TextAnalyticsApiKeyCredential.class);
private volatile String apiKey;

/**
* Creates a {@link TextAnalyticsApiKeyCredential} model that describes subscription key for
* authentication.
* Creates a {@link TextAnalyticsApiKeyCredential} model that describes API key for authentication.
*
* @param subscriptionKey the subscription key for authentication
* @param apiKey the API key for authentication
*/
public TextAnalyticsApiKeyCredential(String subscriptionKey) {
this.subscriptionKey = Objects.requireNonNull(subscriptionKey, "`subscriptionKey` cannot be null.");
public TextAnalyticsApiKeyCredential(String apiKey) {
Objects.requireNonNull(apiKey, "`apiKey` cannot be null.");
if (apiKey.isEmpty()) {
throw logger.logExceptionAsError(new IllegalArgumentException("'apiKey' cannot be empty."));
}
this.apiKey = apiKey;
}

/**
* Get the subscription key.
* Get the API key.
*
* @return the subscription key
* @return the API key
*/
public String getSubscriptionKey() {
return this.subscriptionKey;
public String getApiKey() {
return this.apiKey;
}

/**
* Set the subscription key.
* Set the API key.
*
* @param subscriptionKey the subscription key for authentication
* @param apiKey the API key for authentication
*/
public void updateCredential(String subscriptionKey) {
this.subscriptionKey = Objects.requireNonNull(subscriptionKey, "`subscriptionKey` cannot be null.");
public void updateCredential(String apiKey) {
this.apiKey = Objects.requireNonNull(apiKey, "`apiKey` cannot be null.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ The following sections provide several code snippets covering some of the most c
- [Extract key phrases in text with asynchronous client][async_sample_key_phrases]
- [Analyze sentiment in text][sample_sentiment]
- [Analyze sentiment in text with asynchronous client][async_sample_sentiment]
- [Rotate subscription key][sample_rotate_key]
- [Rotate subscription key with asynchronous client][async_sample_rotate_key]
- [Rotate API key][sample_rotate_key]
- [Rotate API key with asynchronous client][async_sample_rotate_key]

Batch Samples:
- [Detect language for a batch of documents][sample_detect_language_batch]
Expand Down Expand Up @@ -79,7 +79,7 @@ Guidelines](../../CONTRIBUTING.md) for more information.
[async_sample_key_phrases_batch]: java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocumentsAsync.java
[async_sample_pii_entities]: java/com/azure/ai/textanalytics/RecognizePiiAsync.java
[async_sample_pii_entities_batch]: java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocumentsAsync.java
[async_sample_rotate_key]: java/com/azure/ai/textanalytics/RotateSubscriptionKeyAsync.java
[async_sample_rotate_key]: java/com/azure/ai/textanalytics/RotateApiKeyAsync.java
[async_sample_sentiment]: java/com/azure/ai/textanalytics/AnalyzeSentimentAsync.java
[async_sample_sentiment_batch]: java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocumentsAsync.java

Expand All @@ -93,7 +93,7 @@ Guidelines](../../CONTRIBUTING.md) for more information.
[sample_key_phrases_batch]: java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java
[sample_pii_entities]: java/com/azure/ai/textanalytics/RecognizePii.java
[sample_pii_entities_batch]: java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java
[sample_rotate_key]: java/com/azure/ai/textanalytics/RotateSubscriptionKey.java
[sample_rotate_key]: java/com/azure/ai/textanalytics/RotateApiKey.java
[sample_sentiment]: java/com/azure/ai/textanalytics/AnalyzeSentiment.java
[sample_sentiment_batch]: java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AnalyzeSentiment {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class AnalyzeSentimentAsync {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class DetectLanguage {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DetectLanguageAsync {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ExtractKeyPhrases {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ExtractKeyPhrasesAsync {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ public void configureHttpClient() {
}

/**
* Code snippet for getting sync client using subscription key authentication.
* Code snippet for getting sync client using the API key authentication.
*/
public void useSubscriptionKeySyncClient() {
public void useApiKeySyncClient() {
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildClient();
}

/**
* Code snippet for getting async client using subscription key authentication.
* Code snippet for getting async client using API key authentication.
*/
public void useSubscriptionKeyAsyncClient() {
public void useApiKeyAsyncClient() {
TextAnalyticsAsyncClient textAnalyticsClient = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildAsyncClient();
}
Expand Down Expand Up @@ -169,15 +169,15 @@ public void handlingException() {
}

/**
* Code snippet for rotating subscription key of the client
* Code snippet for rotating API key of the client
*/
public void rotatingSubscriptionKey() {
TextAnalyticsApiKeyCredential credential = new TextAnalyticsApiKeyCredential("{expired_subscription_key}");
public void rotatingApiKey() {
TextAnalyticsApiKeyCredential credential = new TextAnalyticsApiKeyCredential("{api_key}");
TextAnalyticsClient textAnalyticsClient = new TextAnalyticsClientBuilder()
.subscriptionKey(credential)
.apiKey(credential)
.endpoint("{endpoint}")
.buildClient();

credential.updateCredential("{new_subscription_key}");
credential.updateCredential("{new_api_key}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RecognizeEntities {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class RecognizeEntitiesAsync {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RecognizeLinkedEntities {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class RecognizeLinkedEntitiesAsync {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RecognizePii {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RecognizePiiAsync {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey(new TextAnalyticsApiKeyCredential("{subscription_key}"))
.apiKey(new TextAnalyticsApiKeyCredential("{api_key}"))
.endpoint("{endpoint}")
.buildAsyncClient();

Expand Down
Loading

0 comments on commit e8b664e

Please sign in to comment.