Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.0.0-beta.2 (2020-05-06)
- Fixed Receipt type bug to select the valueString field via fieldValue.
- Rename `apiKey()` to `credential()` on FormRecognizerClientBuilder.

This package's
[documentation](https://github.com/Azure/azure-sdk-for-java/blob/azure-ai-formrecognizer_1.0.0-beta.2/sdk/formrecognizer/azure-ai-formrecognizer/README.md)
Expand Down
15 changes: 8 additions & 7 deletions sdk/formrecognizer/azure-ai-formrecognizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ az cognitiveservices account create \
```
### Authenticate the client
In order to interact with the Form Recognizer service, you will need to create an instance of the `FormRecognizerClient`
class. You will need an **endpoint** and an **API key** to instantiate a client object,
class. You will need an **endpoint** and an **key** to instantiate a client object,
they can be found in the [Azure Portal][azure_portal] under the "Quickstart" in your created
Form Recognizer resource. See the full details regarding [authentication][authentication] of Cognitive Services.

#### Get credentials
The `credential` parameter may be provided as a [`AzureKeyCredential`][azure_key_credential] from [azure-core][azure_core].

##### Create FormRecognizerClient with AzureKeyCredential
To use an [API key][api_key], provide the key as a string to the AzureKeyCredential. This can be found in the [Azure Portal][azure_portal]
To use AzureKeyCredential authentication, provide the [key][key] as a string to the [AzureKeyCredential][azure_key_credential]. This can be found in the [Azure Portal][azure_portal]
under the "Quickstart" section or by running the following Azure CLI command:

```bash
Expand All @@ -77,21 +77,21 @@ Use the API key as the credential parameter to authenticate the client:
<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L37-L40 -->
```java
FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.buildClient();
```
The Azure Form Recognizer client library provides a way to **rotate the existing API key**.

<!-- embedme ./src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java#L47-L53 -->
```java
AzureKeyCredential credential = new AzureKeyCredential("{api_key}");
AzureKeyCredential credential = new AzureKeyCredential("{key}");
FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
.apiKey(credential)
.credential(credential)
.endpoint("{endpoint}")
.buildClient();

credential.update("{new_api_key}");
credential.update("{new_key}");
```

## Key concepts
Expand Down Expand Up @@ -336,7 +336,8 @@ When you submit a pull request, a CLA-bot will automatically determine whether y
This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For more information see the [Code of Conduct FAQ][coc_faq] or contact [opencode@microsoft.com][coc_contact] with any additional questions or comments.

<!-- LINKS -->
[api_key]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account?tabs=multiservice%2Cwindows#get-the-keys-for-your-resource
[azure_key_credential]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/core/azure-core/src/main/java/com/azure/core/credential/AzureKeyCredential.java
[key]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account?tabs=multiservice%2Cwindows#get-the-keys-for-your-resource
[api_reference_doc]: https://aka.ms/azsdk-java-formrecognizer-ref-docs
[authentication]: https://docs.microsoft.com/azure/cognitive-services/authentication
[azure_cli]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account-cli?tabs=windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*
* <p>
* The client needs the service endpoint of the Azure Form Recognizer to access the resource service.
* {@link #apiKey(AzureKeyCredential)} gives
* {@link #credential(AzureKeyCredential)} gives
* the builder access credential.
* </p>
*
Expand Down Expand Up @@ -114,7 +114,7 @@ public FormRecognizerClientBuilder() {
*
* @return A FormRecognizerClient with the options set from the builder.
* @throws NullPointerException if {@link #endpoint(String) endpoint} or
* {@link #apiKey(AzureKeyCredential) apiKey} has not been set.
* {@link #credential(AzureKeyCredential)} has not been set.
* @throws IllegalArgumentException if {@link #endpoint(String) endpoint} cannot be parsed into a valid URL.
*/
public FormRecognizerClient buildClient() {
Expand All @@ -132,8 +132,8 @@ public FormRecognizerClient buildClient() {
* </p>
*
* @return A FormRecognizerAsyncClient with the options set from the builder.
* @throws NullPointerException if {@link #endpoint(String) endpoint} or
* {@link #apiKey(AzureKeyCredential) apiKey} has not been set.
* @throws NullPointerException if {@link #endpoint(String) endpoint} or {@link #credential(AzureKeyCredential)}
* has not been set.
* @throws IllegalArgumentException if {@link #endpoint(String) endpoint} cannot be parsed into a valid URL.
*/
public FormRecognizerAsyncClient buildAsyncClient() {
Expand Down Expand Up @@ -219,14 +219,15 @@ public FormRecognizerClientBuilder endpoint(String endpoint) {
}

/**
* Sets the credential to use when authenticating HTTP requests for this FormRecognizerClientBuilder.
* Sets the {@link AzureKeyCredential} to use when authenticating HTTP requests for this
* FormRecognizerClientBuilder.
*
* @param apiKeyCredential API key credential
* @param apiKeyCredential {@link AzureKeyCredential} API key credential
*
* @return The updated FormRecognizerClientBuilder object.
* @throws NullPointerException If {@code apiKeyCredential} is {@code null}
*/
public FormRecognizerClientBuilder apiKey(AzureKeyCredential apiKeyCredential) {
public FormRecognizerClientBuilder credential(AzureKeyCredential apiKeyCredential) {
this.credential = Objects.requireNonNull(apiKeyCredential, "'apiKeyCredential' cannot be null.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Search is validating apiKeyCredential.getKey empty or null case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover, if the credential is required, we can move the validation up to buildClient()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public SearchServiceClientBuilder credential(AzureKeyCredential keyCredential) {
        if (keyCredential == null) {
            throw logger.logExceptionAsError(new NullPointerException("'keyCredential' cannot be null."));
        }
        if (CoreUtils.isNullOrEmpty(keyCredential.getKey())) {
            throw logger.logExceptionAsError(
                new IllegalArgumentException("'keyCredential' cannot have a null or empty API key."));
        }
        this.keyCredential = keyCredential;
        return this;
    }

Here is how search do validation for your reference.

Copy link
Member Author

@samvaity samvaity May 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sima-zhu Could this

if (keyCredential == null) {
throw logger.logExceptionAsError(new NullPointerException("'keyCredential' cannot be null."));
}

be replaced with

   this.credential = Objects.requireNonNull(keyCredential, "'keyCredential' cannot be null.");

And also for the rest of the code, I see that we are already doing the empty checks here and here. It would be redundant?

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String[] args) throws IOException {
// Instantiate a client that will be used to call the service.

FormRecognizerClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void main(String[] args) throws IOException {
// Instantiate a client that will be used to call the service.

FormRecognizerAsyncClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class FormRecognizerAsyncClientJavaDocCodeSnippets {
public void createFormRecognizerAsyncClient() {
// BEGIN: com.azure.ai.formrecognizer.FormRecognizerAsyncClient.instantiation
FormRecognizerAsyncClient formRecognizerAsyncClient = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.buildAsyncClient();
// END: com.azure.ai.formrecognizer.FormRecognizerAsyncClient.instantiation
Expand All @@ -47,7 +47,7 @@ public void createFormRecognizerAsyncClientWithPipeline() {
.build();

FormRecognizerAsyncClient formRecognizerAsyncClient = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.pipeline(pipeline)
.buildAsyncClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class FormRecognizerClientJavaDocCodeSnippets {
public void createFormRecognizerClient() {
// BEGIN: com.azure.ai.formrecognizer.FormRecognizerClient.instantiation
FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.buildClient();
// END: com.azure.ai.formrecognizer.FormRecognizerClient.instantiation
Expand All @@ -44,7 +44,7 @@ public void createFormRecognizerClientWithPipeline() {
.build();

FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.pipeline(pipeline)
.buildClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class GetBoundingBoxes {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
FormRecognizerClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class GetBoundingBoxesAsync {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
FormRecognizerAsyncClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ManageCustomModels {
public static void main(final String[] args) {
// Instantiate a client that will be used to call the service.
FormTrainingClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient().getFormTrainingClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ManageCustomModelsAsync {
public static void main(final String[] args) {
// Instantiate a client that will be used to call the service.
FormTrainingAsyncClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildAsyncClient().getFormTrainingAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ public class ReadmeSamples {
private FormTrainingClient formTrainingClient = formRecognizerClient.getFormTrainingClient();

/**
* Code snippet for getting sync client using the API key authentication.
* Code snippet for getting sync client using the AzureKeyCredential authentication.
*/
public void useApiKeySyncClient() {
public void useAzureKeyCredentialSyncClient() {
FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.buildClient();
}

/**
* Code snippet for rotating API key of the client
* Code snippet for rotating AzureKeyCredential of the client
*/
public void rotatingApiKey() {
AzureKeyCredential credential = new AzureKeyCredential("{api_key}");
public void rotatingAzureKeyCredential() {
AzureKeyCredential credential = new AzureKeyCredential("{key}");
FormRecognizerClient formRecognizerClient = new FormRecognizerClientBuilder()
.apiKey(credential)
.credential(credential)
.endpoint("{endpoint}")
.buildClient();

credential.update("{new_api_key}");
credential.update("{new_key}");
}

public void recognizeCustomForm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void main(final String[] args) throws IOException {
// Instantiate a client that will be used to call the service.

FormRecognizerClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void main(final String[] args) {
// Instantiate a client that will be used to call the service.

FormRecognizerAsyncClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RecognizeCustomForms {
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
FormRecognizerClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class RecognizeCustomFormsAsync {
public static void main(String[] args) throws IOException {
// Instantiate a client that will be used to call the service.
FormRecognizerAsyncClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RecognizeReceipts {
public static void main(final String[] args) throws IOException {
// Instantiate a client that will be used to call the service.
FormRecognizerClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class RecognizeReceiptsAsync {
public static void main(final String[] args) throws IOException {
// Instantiate a client that will be used to call the service.
FormRecognizerAsyncClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class RecognizeReceiptsFromUrl {
public static void main(final String[] args) {
// Instantiate a client that will be used to call the service.
FormRecognizerClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class RecognizeReceiptsFromUrlAsync {
public static void main(final String[] args) {
// Instantiate a client that will be used to call the service.
FormRecognizerAsyncClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void main(String[] args) {
// Instantiate a client that will be used to call the service.

FormTrainingClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient().getFormTrainingClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void main(String[] args) {
// Instantiate a client that will be used to call the service.

FormTrainingAsyncClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_Key}"))
.credential(new AzureKeyCredential("{api_Key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildAsyncClient().getFormTrainingAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void main(String[] args) {
// Instantiate a client that will be used to call the service.

FormTrainingClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_Key}"))
.credential(new AzureKeyCredential("{api_Key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient().getFormTrainingClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void main(String[] args) {
// Instantiate a client that will be used to call the service.

FormTrainingAsyncClient client = new FormRecognizerClientBuilder()
.apiKey(new AzureKeyCredential("{api_key}"))
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildAsyncClient().getFormTrainingAsyncClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ String getEndpoint() {
*/
FormRecognizerClientBuilder createClientBuilder(String endpoint, AzureKeyCredential credential) {
final FormRecognizerClientBuilder clientBuilder = new FormRecognizerClientBuilder()
.apiKey(credential)
.credential(credential)
.endpoint(endpoint);

if (interceptorManager.isPlaybackMode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ public void invalidProtocol() {
}

/**
* Test for null API key
* Test for null AzureKeyCredential
*/
@Test
public void nullApiKey() {
public void nullAzureKeyCredential() {
assertThrows(NullPointerException.class, () -> {
final FormRecognizerClientBuilder builder = new FormRecognizerClientBuilder();
builder.endpoint(VALID_HTTPS_LOCALHOST).apiKey(null);
builder.endpoint(VALID_HTTPS_LOCALHOST).credential(null);
});
}

/**
* Test for empty Api Key without any other authentication
* Test for empty Key without any other authentication
*/
@Test
public void emptyApiKey() {
public void emptyKeyCredential() {
assertThrows(IllegalArgumentException.class, () -> new AzureKeyCredential(""));
}
}
2 changes: 1 addition & 1 deletion sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Release History

## 1.0.0-beta.5 (Unreleased)

- Rename `apiKey()` to `credential()` on TextAnalyticsClientBuilder.

## 1.0.0-beta.4 (2020-04-07)
- Throws an illegal argument exception when the given list of documents is an empty list.
Expand Down
Loading