Skip to content
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

[AutoPR azure-ai-translation-text] [TypeSpec-Clean] Update for Azure.AI.TextTranslation #5473

Closed
wants to merge 1 commit into from
Closed
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
Expand Up @@ -7,13 +7,9 @@
import com.azure.ai.translation.text.implementation.TextTranslationClientImpl;
import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.client.traits.AzureKeyCredentialTrait;
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.TokenCredentialTrait;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.credential.TokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpPipeline;
Expand All @@ -22,8 +18,6 @@
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.AzureKeyCredentialPolicy;
import com.azure.core.http.policy.BearerTokenAuthenticationPolicy;
import com.azure.core.http.policy.CookiePolicy;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpLoggingPolicy;
Expand All @@ -38,8 +32,6 @@
import com.azure.core.util.CoreUtils;
import com.azure.core.util.builder.ClientBuilderUtil;
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 @@ -50,20 +42,11 @@
public final class TextTranslationClientBuilder
implements HttpTrait<TextTranslationClientBuilder>,
ConfigurationTrait<TextTranslationClientBuilder>,
EndpointTrait<TextTranslationClientBuilder>,
AzureKeyCredentialTrait<TextTranslationClientBuilder>,
TokenCredentialTrait<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 = "https://cognitiveservices.azure.com/.default";
private static final String OCP_APIM_SUBSCRIPTION_KEY = "Ocp-Apim-Subscription-Key";

private String region;
private AzureKeyCredential credential;
private TokenCredential tokenCredential;

@Generated
private static final Map<String, String> PROPERTIES =
CoreUtils.getProperties("azure-ai-translation-text.properties");
Expand Down Expand Up @@ -168,14 +151,11 @@ 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 Down Expand Up @@ -213,47 +193,6 @@ public TextTranslationClientBuilder retryPolicy(RetryPolicy retryPolicy) {
return this;
}

/**
* Sets the {@link AzureKeyCredential} used to authorize requests sent to the service.
*
* @param credential {@link AzureKeyCredential} used to authorize requests sent to the service.
* @return The updated {@link TextTranslationClientBuilder} object.
* @throws NullPointerException If {@code credential} is null.
*/
public TextTranslationClientBuilder credential(AzureKeyCredential 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 tokenCredential} is null.
*/
public TextTranslationClientBuilder region(String region) {
Objects.requireNonNull(region, "'region' cannot be null.");
this.region = region;
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.
*
Expand All @@ -264,25 +203,9 @@ private TextTranslationClientImpl buildInnerClient() {
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;
}

TextTranslationClientImpl client =
new TextTranslationClientImpl(
localPipeline, JacksonAdapter.createDefaultSerializerAdapter(), serviceEndpoint, localServiceVersion);
localPipeline, JacksonAdapter.createDefaultSerializerAdapter(), endpoint, localServiceVersion);
return client;
}

Expand Down Expand Up @@ -311,19 +234,6 @@ private HttpPipeline createHttpPipeline() {
policies.add(ClientBuilderUtil.validateAndGetRetryPolicy(retryPolicy, retryOptions, new RetryPolicy()));
policies.add(new AddDatePolicy());
policies.add(new CookiePolicy());

if (tokenCredential != null) {
policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, DEFAULT_SCOPE));
}

if (this.credential != null) {
policies.add(new AzureKeyCredentialPolicy(OCP_APIM_SUBSCRIPTION_KEY, credential));

if (this.region != null) {
policies.add(new TranslatorRegionAuthenticationPolicy(this.region));
}
}

this.pipelinePolicies.stream()
.filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_RETRY)
.forEach(p -> policies.add(p));
Expand Down
Loading