1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
3
// Code generated by Microsoft (R) TypeSpec Code Generator.
4
+
4
5
package com .azure .ai .translation .text ;
5
6
6
7
import com .azure .ai .translation .text .implementation .TextTranslationClientImpl ;
7
- import com .azure .ai .translation .text .models .TextTranslationAudience ;
8
8
import com .azure .core .annotation .Generated ;
9
9
import com .azure .core .annotation .ServiceClientBuilder ;
10
10
import com .azure .core .client .traits .ConfigurationTrait ;
11
11
import com .azure .core .client .traits .EndpointTrait ;
12
12
import com .azure .core .client .traits .HttpTrait ;
13
- import com .azure .core .client .traits .KeyCredentialTrait ;
14
- import com .azure .core .client .traits .TokenCredentialTrait ;
15
- import com .azure .core .credential .KeyCredential ;
16
- import com .azure .core .credential .TokenCredential ;
17
13
import com .azure .core .http .HttpClient ;
18
- import com .azure .core .http .HttpHeaderName ;
19
14
import com .azure .core .http .HttpHeaders ;
20
15
import com .azure .core .http .HttpPipeline ;
21
16
import com .azure .core .http .HttpPipelineBuilder ;
22
17
import com .azure .core .http .HttpPipelinePosition ;
23
18
import com .azure .core .http .policy .AddDatePolicy ;
24
19
import com .azure .core .http .policy .AddHeadersFromContextPolicy ;
25
20
import com .azure .core .http .policy .AddHeadersPolicy ;
26
- import com .azure .core .http .policy .BearerTokenAuthenticationPolicy ;
27
21
import com .azure .core .http .policy .HttpLogOptions ;
28
22
import com .azure .core .http .policy .HttpLoggingPolicy ;
29
23
import com .azure .core .http .policy .HttpPipelinePolicy ;
30
24
import com .azure .core .http .policy .HttpPolicyProviders ;
31
- import com .azure .core .http .policy .KeyCredentialPolicy ;
32
25
import com .azure .core .http .policy .RequestIdPolicy ;
33
26
import com .azure .core .http .policy .RetryOptions ;
34
27
import com .azure .core .http .policy .RetryPolicy ;
39
32
import com .azure .core .util .builder .ClientBuilderUtil ;
40
33
import com .azure .core .util .logging .ClientLogger ;
41
34
import com .azure .core .util .serializer .JacksonAdapter ;
42
- import java .net .MalformedURLException ;
43
- import java .net .URL ;
44
35
import java .util .ArrayList ;
45
36
import java .util .List ;
46
37
import java .util .Map ;
51
42
*/
52
43
@ ServiceClientBuilder (serviceClients = { TextTranslationClient .class , TextTranslationAsyncClient .class })
53
44
public final class TextTranslationClientBuilder implements HttpTrait <TextTranslationClientBuilder >,
54
- ConfigurationTrait <TextTranslationClientBuilder >, EndpointTrait <TextTranslationClientBuilder >,
55
- KeyCredentialTrait <TextTranslationClientBuilder >, TokenCredentialTrait <TextTranslationClientBuilder > {
56
-
45
+ ConfigurationTrait <TextTranslationClientBuilder >, EndpointTrait <TextTranslationClientBuilder > {
57
46
@ Generated
58
47
private static final String SDK_NAME = "name" ;
59
48
60
49
@ Generated
61
50
private static final String SDK_VERSION = "version" ;
62
51
63
- private static final String DEFAULT_SCOPE = "/.default" ;
64
-
65
- private static final String OCP_APIM_SUBSCRIPTION_KEY = "Ocp-Apim-Subscription-Key" ;
66
-
67
- private static final String OCP_APIM_SUBSCRIPTION_REGION = "Ocp-Apim-Subscription-Region" ;
68
-
69
- private static final String OCP_APIM_RESOURCE_ID_KEY = "Ocp-Apim-ResourceId" ;
70
-
71
- private String region ;
72
-
73
- private String resourceId ;
74
-
75
- private TextTranslationAudience audience ;
76
-
77
- private KeyCredential credential ;
78
-
79
- private TokenCredential tokenCredential ;
80
-
81
52
@ Generated
82
53
private static final Map <String , String > PROPERTIES
83
54
= CoreUtils .getProperties ("azure-ai-translation-text.properties" );
@@ -94,37 +65,37 @@ public TextTranslationClientBuilder() {
94
65
}
95
66
96
67
/*
97
- * The HTTP pipeline to send requests through .
68
+ * The HTTP client used to send the request .
98
69
*/
99
70
@ Generated
100
- private HttpPipeline pipeline ;
71
+ private HttpClient httpClient ;
101
72
102
73
/**
103
74
* {@inheritDoc}.
104
75
*/
105
76
@ Generated
106
77
@ Override
107
- public TextTranslationClientBuilder pipeline (HttpPipeline pipeline ) {
108
- if (this .pipeline != null && pipeline == null ) {
109
- LOGGER .atInfo ().log ("HttpPipeline is being set to 'null' when it was previously configured." );
110
- }
111
- this .pipeline = pipeline ;
78
+ public TextTranslationClientBuilder httpClient (HttpClient httpClient ) {
79
+ this .httpClient = httpClient ;
112
80
return this ;
113
81
}
114
82
115
83
/*
116
- * The HTTP client used to send the request .
84
+ * The HTTP pipeline to send requests through .
117
85
*/
118
86
@ Generated
119
- private HttpClient httpClient ;
87
+ private HttpPipeline pipeline ;
120
88
121
89
/**
122
90
* {@inheritDoc}.
123
91
*/
124
92
@ Generated
125
93
@ Override
126
- public TextTranslationClientBuilder httpClient (HttpClient httpClient ) {
127
- this .httpClient = httpClient ;
94
+ public TextTranslationClientBuilder pipeline (HttpPipeline pipeline ) {
95
+ if (this .pipeline != null && pipeline == null ) {
96
+ LOGGER .atInfo ().log ("HttpPipeline is being set to 'null' when it was previously configured." );
97
+ }
98
+ this .pipeline = pipeline ;
128
99
return this ;
129
100
}
130
101
@@ -209,15 +180,13 @@ public TextTranslationClientBuilder configuration(Configuration configuration) {
209
180
@ Generated
210
181
private String endpoint ;
211
182
212
- private Boolean isCustomEndpoint = false ;
213
-
214
183
/**
215
184
* {@inheritDoc}.
216
185
*/
186
+ @ Generated
217
187
@ Override
218
188
public TextTranslationClientBuilder endpoint (String endpoint ) {
219
189
this .endpoint = endpoint ;
220
- this .isCustomEndpoint = CustomEndpointUtils .isPlatformHost (endpoint );
221
190
return this ;
222
191
}
223
192
@@ -229,7 +198,7 @@ public TextTranslationClientBuilder endpoint(String endpoint) {
229
198
230
199
/**
231
200
* Sets Service version.
232
- *
201
+ *
233
202
* @param serviceVersion the serviceVersion value.
234
203
* @return the TextTranslationClientBuilder.
235
204
*/
@@ -247,7 +216,7 @@ public TextTranslationClientBuilder serviceVersion(TextTranslationServiceVersion
247
216
248
217
/**
249
218
* Sets The retry policy that will attempt to retry failed requests, if applicable.
250
- *
219
+ *
251
220
* @param retryPolicy the retryPolicy value.
252
221
* @return the TextTranslationClientBuilder.
253
222
*/
@@ -257,113 +226,30 @@ public TextTranslationClientBuilder retryPolicy(RetryPolicy retryPolicy) {
257
226
return this ;
258
227
}
259
228
260
- /**
261
- * Sets the {@link KeyCredential} used to authorize requests sent to the service.
262
- *
263
- * @param credential {@link KeyCredential} used to authorize requests sent to the service.
264
- * @return The updated {@link TextTranslationClientBuilder} object.
265
- * @throws NullPointerException If {@code credential} is null.
266
- */
267
- public TextTranslationClientBuilder credential (KeyCredential credential ) {
268
- Objects .requireNonNull (credential , "'credential' cannot be null." );
269
- this .credential = credential ;
270
- return this ;
271
- }
272
-
273
- /**
274
- * Sets the region used to authorize requests sent to the service.
275
- *
276
- * @param region where the Translator resource is created.
277
- * @return The updated {@link TextTranslationClientBuilder} object.
278
- * @throws NullPointerException If {@code region} is null.
279
- */
280
- public TextTranslationClientBuilder region (String region ) {
281
- Objects .requireNonNull (region , "'region' cannot be null." );
282
- this .region = region ;
283
- return this ;
284
- }
285
-
286
- /**
287
- * Sets the Azure Resource Id used to authorize requests sent to the service.
288
- *
289
- * @param resourceId Id of the Translator Resource.
290
- * @return The updated {@link TextTranslationClientBuilder} object.
291
- * @throws NullPointerException If {@code resourceId} is null.
292
- */
293
- public TextTranslationClientBuilder resourceId (String resourceId ) {
294
- Objects .requireNonNull (resourceId , "'resourceId' cannot be null." );
295
- this .resourceId = resourceId ;
296
- return this ;
297
- }
298
-
299
- /**
300
- * Sets the Authentication audience used to authorize requests sent to the service.
301
- *
302
- * @param audience Token Audience.
303
- * @return The updated {@link TextTranslationClientBuilder} object.
304
- * @throws NullPointerException If {@code audience} is null.
305
- */
306
- public TextTranslationClientBuilder audience (TextTranslationAudience audience ) {
307
- Objects .requireNonNull (audience , "'audience' cannot be null." );
308
- this .audience = audience ;
309
- return this ;
310
- }
311
-
312
- /**
313
- * Sets the {@link TokenCredential} used to authorize requests sent to the service. Refer to the Azure SDK for Java
314
- * <a href="https://aka.ms/azsdk/java/docs/identity">identity and authentication</a>
315
- * documentation for more details on proper usage of the {@link TokenCredential} type.
316
- *
317
- * @param tokenCredential {@link TokenCredential} used to authorize requests sent to the service.
318
- * @return The updated {@link TextTranslationClientBuilder} object.
319
- * @throws NullPointerException If {@code tokenCredential} is null.
320
- */
321
- public TextTranslationClientBuilder credential (TokenCredential tokenCredential ) {
322
- Objects .requireNonNull (tokenCredential , "'tokenCredential' cannot be null." );
323
- this .tokenCredential = tokenCredential ;
324
- return this ;
325
- }
326
-
327
229
/**
328
230
* Builds an instance of TextTranslationClientImpl with the provided parameters.
329
- *
231
+ *
330
232
* @return an instance of TextTranslationClientImpl.
331
233
*/
234
+ @ Generated
332
235
private TextTranslationClientImpl buildInnerClient () {
236
+ this .validateClient ();
333
237
HttpPipeline localPipeline = (pipeline != null ) ? pipeline : createHttpPipeline ();
334
238
TextTranslationServiceVersion localServiceVersion
335
239
= (serviceVersion != null ) ? serviceVersion : TextTranslationServiceVersion .getLatest ();
336
- String serviceEndpoint ;
337
- if (this .endpoint == null ) {
338
- serviceEndpoint = "https://api.cognitive.microsofttranslator.com" ;
339
- } else if (this .isCustomEndpoint ) {
340
- try {
341
- URL hostUri = new URL (endpoint );
342
- URL fullUri = new URL (hostUri , "/translator/text/v" + localServiceVersion .getVersion ());
343
- serviceEndpoint = fullUri .toString ();
344
- } catch (MalformedURLException ex ) {
345
- serviceEndpoint = endpoint ;
346
- }
347
- } else {
348
- serviceEndpoint = endpoint ;
349
- }
350
- if (tokenCredential != null && (this .region != null || this .resourceId != null )) {
351
- Objects .requireNonNull (this .region , "'region' cannot be null." );
352
- Objects .requireNonNull (this .resourceId , "'resourceId' cannot be null." );
353
- }
354
- if (this .credential != null && !CoreUtils .isNullOrEmpty (this .resourceId )) {
355
- throw LOGGER .logExceptionAsError (
356
- new IllegalStateException ("Resource Id cannot be used with key credential. Set resourceId to null." ));
357
- }
358
- if (tokenCredential != null && this .credential != null ) {
359
- throw LOGGER .logExceptionAsError (
360
- new IllegalStateException ("Both token credential and key credential cannot be set." ));
361
- }
362
240
TextTranslationClientImpl client = new TextTranslationClientImpl (localPipeline ,
363
- JacksonAdapter .createDefaultSerializerAdapter (), serviceEndpoint , localServiceVersion );
241
+ JacksonAdapter .createDefaultSerializerAdapter (), this . endpoint , localServiceVersion );
364
242
return client ;
365
243
}
366
244
245
+ @ Generated
246
+ private void validateClient () {
247
+ // This method is invoked from 'buildInnerClient'/'buildClient' method.
248
+ // Developer can customize this method, to validate that the necessary conditions are met for the new client.
249
+ Objects .requireNonNull (endpoint , "'endpoint' cannot be null." );
250
+ }
251
+
252
+ @ Generated
367
253
private HttpPipeline createHttpPipeline () {
368
254
Configuration buildConfiguration
369
255
= (configuration == null ) ? Configuration .getGlobalConfiguration () : configuration ;
@@ -376,10 +262,8 @@ private HttpPipeline createHttpPipeline() {
376
262
policies .add (new UserAgentPolicy (applicationId , clientName , clientVersion , buildConfiguration ));
377
263
policies .add (new RequestIdPolicy ());
378
264
policies .add (new AddHeadersFromContextPolicy ());
379
- HttpHeaders headers = new HttpHeaders ();
380
- localClientOptions .getHeaders ()
381
- .forEach (header -> headers .set (HttpHeaderName .fromString (header .getName ()), header .getValue ()));
382
- if (headers .getSize () > 0 ) {
265
+ HttpHeaders headers = CoreUtils .createHttpHeadersFromClientOptions (localClientOptions );
266
+ if (headers != null ) {
383
267
policies .add (new AddHeadersPolicy (headers ));
384
268
}
385
269
this .pipelinePolicies .stream ()
@@ -388,27 +272,6 @@ private HttpPipeline createHttpPipeline() {
388
272
HttpPolicyProviders .addBeforeRetryPolicies (policies );
389
273
policies .add (ClientBuilderUtil .validateAndGetRetryPolicy (retryPolicy , retryOptions , new RetryPolicy ()));
390
274
policies .add (new AddDatePolicy ());
391
- if (tokenCredential != null ) {
392
- TextTranslationAudience authAudience = TextTranslationAudience .AZURE_PUBLIC_CLOUD ;
393
- if (this .audience != null ) {
394
- authAudience = this .audience ;
395
- }
396
- policies .add (new BearerTokenAuthenticationPolicy (tokenCredential , authAudience + DEFAULT_SCOPE ));
397
- if (this .region != null || this .resourceId != null ) {
398
- HttpHeaders aadHeaders = new HttpHeaders ();
399
- aadHeaders .put (OCP_APIM_RESOURCE_ID_KEY , this .resourceId );
400
- aadHeaders .put (OCP_APIM_SUBSCRIPTION_REGION , this .region );
401
- policies .add (new AddHeadersPolicy (aadHeaders ));
402
- }
403
- }
404
- if (this .credential != null ) {
405
- policies .add (new KeyCredentialPolicy (OCP_APIM_SUBSCRIPTION_KEY , credential ));
406
- if (this .region != null ) {
407
- HttpHeaders regionHeaders = new HttpHeaders ();
408
- regionHeaders .put (OCP_APIM_SUBSCRIPTION_REGION , this .region );
409
- policies .add (new AddHeadersPolicy (regionHeaders ));
410
- }
411
- }
412
275
this .pipelinePolicies .stream ()
413
276
.filter (p -> p .getPipelinePosition () == HttpPipelinePosition .PER_RETRY )
414
277
.forEach (p -> policies .add (p ));
@@ -423,7 +286,7 @@ private HttpPipeline createHttpPipeline() {
423
286
424
287
/**
425
288
* Builds an instance of TextTranslationAsyncClient class.
426
- *
289
+ *
427
290
* @return an instance of TextTranslationAsyncClient.
428
291
*/
429
292
@ Generated
@@ -433,7 +296,7 @@ public TextTranslationAsyncClient buildAsyncClient() {
433
296
434
297
/**
435
298
* Builds an instance of TextTranslationClient class.
436
- *
299
+ *
437
300
* @return an instance of TextTranslationClient.
438
301
*/
439
302
@ Generated
@@ -442,11 +305,4 @@ public TextTranslationClient buildClient() {
442
305
}
443
306
444
307
private static final ClientLogger LOGGER = new ClientLogger (TextTranslationClientBuilder .class );
445
-
446
- @ Generated
447
- private void validateClient () {
448
- // This method is invoked from 'buildInnerClient'/'buildClient' method.
449
- // Developer can customize this method, to validate that the necessary conditions are met for the new client.
450
- Objects .requireNonNull (endpoint , "'endpoint' cannot be null." );
451
- }
452
308
}
0 commit comments