Skip to content

[AutoPR azure-ai-translation-text] [test] Scgree/translation 2025 05 01 test java #16352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) TypeSpec Code Generator.

package com.azure.ai.translation.text;

import com.azure.ai.translation.text.implementation.TextTranslationClientImpl;
import com.azure.ai.translation.text.models.TextTranslationAudience;
import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.client.traits.ConfigurationTrait;
import com.azure.core.client.traits.EndpointTrait;
import com.azure.core.client.traits.HttpTrait;
import com.azure.core.client.traits.KeyCredentialTrait;
import com.azure.core.client.traits.TokenCredentialTrait;
import com.azure.core.credential.KeyCredential;
import com.azure.core.credential.TokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpHeaderName;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.HttpPipelinePosition;
import com.azure.core.http.policy.AddDatePolicy;
import com.azure.core.http.policy.AddHeadersFromContextPolicy;
import com.azure.core.http.policy.AddHeadersPolicy;
import com.azure.core.http.policy.BearerTokenAuthenticationPolicy;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpLoggingPolicy;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.http.policy.HttpPolicyProviders;
import com.azure.core.http.policy.KeyCredentialPolicy;
import com.azure.core.http.policy.RequestIdPolicy;
import com.azure.core.http.policy.RetryOptions;
import com.azure.core.http.policy.RetryPolicy;
Expand All @@ -39,8 +32,6 @@
import com.azure.core.util.builder.ClientBuilderUtil;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.serializer.JacksonAdapter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -51,33 +42,13 @@
*/
@ServiceClientBuilder(serviceClients = { TextTranslationClient.class, TextTranslationAsyncClient.class })
public final class TextTranslationClientBuilder implements HttpTrait<TextTranslationClientBuilder>,
ConfigurationTrait<TextTranslationClientBuilder>, EndpointTrait<TextTranslationClientBuilder>,
KeyCredentialTrait<TextTranslationClientBuilder>, TokenCredentialTrait<TextTranslationClientBuilder> {

ConfigurationTrait<TextTranslationClientBuilder>, EndpointTrait<TextTranslationClientBuilder> {
@Generated
private static final String SDK_NAME = "name";

@Generated
private static final String SDK_VERSION = "version";

private static final String DEFAULT_SCOPE = "/.default";

private static final String OCP_APIM_SUBSCRIPTION_KEY = "Ocp-Apim-Subscription-Key";

private static final String OCP_APIM_SUBSCRIPTION_REGION = "Ocp-Apim-Subscription-Region";

private static final String OCP_APIM_RESOURCE_ID_KEY = "Ocp-Apim-ResourceId";

private String region;

private String resourceId;

private TextTranslationAudience audience;

private KeyCredential credential;

private TokenCredential tokenCredential;

@Generated
private static final Map<String, String> PROPERTIES
= CoreUtils.getProperties("azure-ai-translation-text.properties");
Expand All @@ -94,37 +65,37 @@ public TextTranslationClientBuilder() {
}

/*
* The HTTP pipeline to send requests through.
* The HTTP client used to send the request.
*/
@Generated
private HttpPipeline pipeline;
private HttpClient httpClient;

/**
* {@inheritDoc}.
*/
@Generated
@Override
public TextTranslationClientBuilder pipeline(HttpPipeline pipeline) {
if (this.pipeline != null && pipeline == null) {
LOGGER.atInfo().log("HttpPipeline is being set to 'null' when it was previously configured.");
}
this.pipeline = pipeline;
public TextTranslationClientBuilder httpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}

/*
* The HTTP client used to send the request.
* The HTTP pipeline to send requests through.
*/
@Generated
private HttpClient httpClient;
private HttpPipeline pipeline;

/**
* {@inheritDoc}.
*/
@Generated
@Override
public TextTranslationClientBuilder httpClient(HttpClient httpClient) {
this.httpClient = httpClient;
public TextTranslationClientBuilder pipeline(HttpPipeline pipeline) {
if (this.pipeline != null && pipeline == null) {
LOGGER.atInfo().log("HttpPipeline is being set to 'null' when it was previously configured.");
}
this.pipeline = pipeline;
return this;
}

Expand Down Expand Up @@ -209,15 +180,13 @@ public TextTranslationClientBuilder configuration(Configuration configuration) {
@Generated
private String endpoint;

private Boolean isCustomEndpoint = false;

/**
* {@inheritDoc}.
*/
@Generated
@Override
public TextTranslationClientBuilder endpoint(String endpoint) {
this.endpoint = endpoint;
this.isCustomEndpoint = CustomEndpointUtils.isPlatformHost(endpoint);
return this;
}

Expand All @@ -229,7 +198,7 @@ public TextTranslationClientBuilder endpoint(String endpoint) {

/**
* Sets Service version.
*
*
* @param serviceVersion the serviceVersion value.
* @return the TextTranslationClientBuilder.
*/
Expand All @@ -247,7 +216,7 @@ public TextTranslationClientBuilder serviceVersion(TextTranslationServiceVersion

/**
* Sets The retry policy that will attempt to retry failed requests, if applicable.
*
*
* @param retryPolicy the retryPolicy value.
* @return the TextTranslationClientBuilder.
*/
Expand All @@ -257,113 +226,30 @@ public TextTranslationClientBuilder retryPolicy(RetryPolicy retryPolicy) {
return this;
}

/**
* Sets the {@link KeyCredential} used to authorize requests sent to the service.
*
* @param credential {@link KeyCredential} used to authorize requests sent to the service.
* @return The updated {@link TextTranslationClientBuilder} object.
* @throws NullPointerException If {@code credential} is null.
*/
public TextTranslationClientBuilder credential(KeyCredential credential) {
Objects.requireNonNull(credential, "'credential' cannot be null.");
this.credential = credential;
return this;
}

/**
* Sets the region used to authorize requests sent to the service.
*
* @param region where the Translator resource is created.
* @return The updated {@link TextTranslationClientBuilder} object.
* @throws NullPointerException If {@code region} is null.
*/
public TextTranslationClientBuilder region(String region) {
Objects.requireNonNull(region, "'region' cannot be null.");
this.region = region;
return this;
}

/**
* Sets the Azure Resource Id used to authorize requests sent to the service.
*
* @param resourceId Id of the Translator Resource.
* @return The updated {@link TextTranslationClientBuilder} object.
* @throws NullPointerException If {@code resourceId} is null.
*/
public TextTranslationClientBuilder resourceId(String resourceId) {
Objects.requireNonNull(resourceId, "'resourceId' cannot be null.");
this.resourceId = resourceId;
return this;
}

/**
* Sets the Authentication audience used to authorize requests sent to the service.
*
* @param audience Token Audience.
* @return The updated {@link TextTranslationClientBuilder} object.
* @throws NullPointerException If {@code audience} is null.
*/
public TextTranslationClientBuilder audience(TextTranslationAudience audience) {
Objects.requireNonNull(audience, "'audience' cannot be null.");
this.audience = audience;
return this;
}

/**
* Sets the {@link TokenCredential} used to authorize requests sent to the service. Refer to the Azure SDK for Java
* <a href="https://aka.ms/azsdk/java/docs/identity">identity and authentication</a>
* documentation for more details on proper usage of the {@link TokenCredential} type.
*
* @param tokenCredential {@link TokenCredential} used to authorize requests sent to the service.
* @return The updated {@link TextTranslationClientBuilder} object.
* @throws NullPointerException If {@code tokenCredential} is null.
*/
public TextTranslationClientBuilder credential(TokenCredential tokenCredential) {
Objects.requireNonNull(tokenCredential, "'tokenCredential' cannot be null.");
this.tokenCredential = tokenCredential;
return this;
}

/**
* Builds an instance of TextTranslationClientImpl with the provided parameters.
*
*
* @return an instance of TextTranslationClientImpl.
*/
@Generated
private TextTranslationClientImpl buildInnerClient() {
this.validateClient();
HttpPipeline localPipeline = (pipeline != null) ? pipeline : createHttpPipeline();
TextTranslationServiceVersion localServiceVersion
= (serviceVersion != null) ? serviceVersion : TextTranslationServiceVersion.getLatest();
String serviceEndpoint;
if (this.endpoint == null) {
serviceEndpoint = "https://api.cognitive.microsofttranslator.com";
} else if (this.isCustomEndpoint) {
try {
URL hostUri = new URL(endpoint);
URL fullUri = new URL(hostUri, "/translator/text/v" + localServiceVersion.getVersion());
serviceEndpoint = fullUri.toString();
} catch (MalformedURLException ex) {
serviceEndpoint = endpoint;
}
} else {
serviceEndpoint = endpoint;
}
if (tokenCredential != null && (this.region != null || this.resourceId != null)) {
Objects.requireNonNull(this.region, "'region' cannot be null.");
Objects.requireNonNull(this.resourceId, "'resourceId' cannot be null.");
}
if (this.credential != null && !CoreUtils.isNullOrEmpty(this.resourceId)) {
throw LOGGER.logExceptionAsError(
new IllegalStateException("Resource Id cannot be used with key credential. Set resourceId to null."));
}
if (tokenCredential != null && this.credential != null) {
throw LOGGER.logExceptionAsError(
new IllegalStateException("Both token credential and key credential cannot be set."));
}
TextTranslationClientImpl client = new TextTranslationClientImpl(localPipeline,
JacksonAdapter.createDefaultSerializerAdapter(), serviceEndpoint, localServiceVersion);
JacksonAdapter.createDefaultSerializerAdapter(), this.endpoint, localServiceVersion);
return client;
}

@Generated
private void validateClient() {
// This method is invoked from 'buildInnerClient'/'buildClient' method.
// Developer can customize this method, to validate that the necessary conditions are met for the new client.
Objects.requireNonNull(endpoint, "'endpoint' cannot be null.");
}

@Generated
private HttpPipeline createHttpPipeline() {
Configuration buildConfiguration
= (configuration == null) ? Configuration.getGlobalConfiguration() : configuration;
Expand All @@ -376,10 +262,8 @@ private HttpPipeline createHttpPipeline() {
policies.add(new UserAgentPolicy(applicationId, clientName, clientVersion, buildConfiguration));
policies.add(new RequestIdPolicy());
policies.add(new AddHeadersFromContextPolicy());
HttpHeaders headers = new HttpHeaders();
localClientOptions.getHeaders()
.forEach(header -> headers.set(HttpHeaderName.fromString(header.getName()), header.getValue()));
if (headers.getSize() > 0) {
HttpHeaders headers = CoreUtils.createHttpHeadersFromClientOptions(localClientOptions);
if (headers != null) {
policies.add(new AddHeadersPolicy(headers));
}
this.pipelinePolicies.stream()
Expand All @@ -388,27 +272,6 @@ private HttpPipeline createHttpPipeline() {
HttpPolicyProviders.addBeforeRetryPolicies(policies);
policies.add(ClientBuilderUtil.validateAndGetRetryPolicy(retryPolicy, retryOptions, new RetryPolicy()));
policies.add(new AddDatePolicy());
if (tokenCredential != null) {
TextTranslationAudience authAudience = TextTranslationAudience.AZURE_PUBLIC_CLOUD;
if (this.audience != null) {
authAudience = this.audience;
}
policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, authAudience + DEFAULT_SCOPE));
if (this.region != null || this.resourceId != null) {
HttpHeaders aadHeaders = new HttpHeaders();
aadHeaders.put(OCP_APIM_RESOURCE_ID_KEY, this.resourceId);
aadHeaders.put(OCP_APIM_SUBSCRIPTION_REGION, this.region);
policies.add(new AddHeadersPolicy(aadHeaders));
}
}
if (this.credential != null) {
policies.add(new KeyCredentialPolicy(OCP_APIM_SUBSCRIPTION_KEY, credential));
if (this.region != null) {
HttpHeaders regionHeaders = new HttpHeaders();
regionHeaders.put(OCP_APIM_SUBSCRIPTION_REGION, this.region);
policies.add(new AddHeadersPolicy(regionHeaders));
}
}
this.pipelinePolicies.stream()
.filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_RETRY)
.forEach(p -> policies.add(p));
Expand All @@ -423,7 +286,7 @@ private HttpPipeline createHttpPipeline() {

/**
* Builds an instance of TextTranslationAsyncClient class.
*
*
* @return an instance of TextTranslationAsyncClient.
*/
@Generated
Expand All @@ -433,7 +296,7 @@ public TextTranslationAsyncClient buildAsyncClient() {

/**
* Builds an instance of TextTranslationClient class.
*
*
* @return an instance of TextTranslationClient.
*/
@Generated
Expand All @@ -442,11 +305,4 @@ public TextTranslationClient buildClient() {
}

private static final ClientLogger LOGGER = new ClientLogger(TextTranslationClientBuilder.class);

@Generated
private void validateClient() {
// This method is invoked from 'buildInnerClient'/'buildClient' method.
// Developer can customize this method, to validate that the necessary conditions are met for the new client.
Objects.requireNonNull(endpoint, "'endpoint' cannot be null.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
*/
public enum TextTranslationServiceVersion implements ServiceVersion {
/**
* Enum value 3.0.
* Enum value v3.0.
*/
V3_0("3.0");
V3_0("v3.0"),

/**
* Enum value 2025-05-01-preview.
*/
V2025_05_01_PREVIEW("2025-05-01-preview");

private final String version;

Expand All @@ -35,6 +40,6 @@ public String getVersion() {
* @return The latest {@link TextTranslationServiceVersion}.
*/
public static TextTranslationServiceVersion getLatest() {
return V3_0;
return V2025_05_01_PREVIEW;
}
}
Loading