Skip to content

Commit f5337b6

Browse files
support customized authentication
1 parent d30773a commit f5337b6

4 files changed

Lines changed: 129 additions & 1 deletion

File tree

sdk/metricsadvisor/azure-ai-metricsadvisor/src/main/java/com/azure/ai/metricsadvisor/MetricsAdvisorClientBuilder.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.azure.ai.metricsadvisor;
55

66
import com.azure.ai.metricsadvisor.implementation.MetricsAdvisorClientImpl;
7+
import com.azure.ai.metricsadvisor.models.MetricsAdvisorKeyCredential;
78
import com.azure.core.annotation.Generated;
89
import com.azure.core.annotation.ServiceClientBuilder;
910
import com.azure.core.credential.TokenCredential;
@@ -24,10 +25,12 @@
2425
import com.azure.core.util.ClientOptions;
2526
import com.azure.core.util.Configuration;
2627
import com.azure.core.util.CoreUtils;
28+
import com.azure.core.util.logging.ClientLogger;
2729
import com.azure.core.util.serializer.JacksonAdapter;
2830
import java.util.ArrayList;
2931
import java.util.List;
3032
import java.util.Map;
33+
import java.util.Objects;
3134
import java.util.stream.Collectors;
3235

3336
/** A builder for creating a new instance of the MetricsAdvisorClient type. */
@@ -203,7 +206,6 @@ private MetricsAdvisorClientImpl buildInnerClient() {
203206
return client;
204207
}
205208

206-
@Generated
207209
private HttpPipeline createHttpPipeline() {
208210
Configuration buildConfiguration =
209211
(configuration == null) ? Configuration.getGlobalConfiguration() : configuration;
@@ -232,6 +234,16 @@ private HttpPipeline createHttpPipeline() {
232234
policies.add(new CookiePolicy());
233235
if (tokenCredential != null) {
234236
policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, DEFAULT_SCOPES));
237+
} else if (!CoreUtils.isNullOrEmpty(metricsAdvisorKeyCredential.getKeys().getSubscriptionKey())
238+
|| !CoreUtils.isNullOrEmpty(metricsAdvisorKeyCredential.getKeys().getApiKey())) {
239+
HttpHeaders apiKeyHeaders = new HttpHeaders();
240+
apiKeyHeaders.set(OCP_APIM_SUBSCRIPTION_KEY, metricsAdvisorKeyCredential.getKeys().getSubscriptionKey());
241+
apiKeyHeaders.set(API_KEY, metricsAdvisorKeyCredential.getKeys().getApiKey());
242+
policies.add(new AddHeadersPolicy(apiKeyHeaders));
243+
} else {
244+
// Throw exception that credential cannot be null
245+
throw LOGGER.logExceptionAsError(
246+
new IllegalArgumentException("Missing credential information while building a client."));
235247
}
236248
policies.addAll(
237249
this.pipelinePolicies.stream()
@@ -287,4 +299,25 @@ public MetricsAdvisorClientBuilder credential(TokenCredential tokenCredential) {
287299
this.tokenCredential = tokenCredential;
288300
return this;
289301
}
302+
303+
private static final ClientLogger LOGGER = new ClientLogger(MetricsAdvisorClientBuilder.class);
304+
private static final String OCP_APIM_SUBSCRIPTION_KEY = "Ocp-Apim-Subscription-Key";
305+
private static final String API_KEY = "x-api-key";
306+
307+
private MetricsAdvisorKeyCredential metricsAdvisorKeyCredential = null;
308+
309+
/**
310+
* Sets the {@link MetricsAdvisorKeyCredential} to use when authenticating HTTP requests for this
311+
* MetricsAdvisorClientBuilder.
312+
*
313+
* @param metricsAdvisorKeyCredential {@link MetricsAdvisorKeyCredential} API key credential
314+
*
315+
* @return The updated MetricsAdvisorClientBuilder object.
316+
* @throws NullPointerException If {@code metricsAdvisorKeyCredential} is null.
317+
*/
318+
public MetricsAdvisorClientBuilder credential(MetricsAdvisorKeyCredential metricsAdvisorKeyCredential) {
319+
this.metricsAdvisorKeyCredential = Objects.requireNonNull(metricsAdvisorKeyCredential,
320+
"'metricsAdvisorKeyCredential' cannot be null.");
321+
return this;
322+
}
290323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.ai.metricsadvisor.models;
5+
6+
import com.azure.core.annotation.Fluent;
7+
8+
/**
9+
* The MetricsAdvisorKeyCredential class.
10+
*/
11+
@Fluent
12+
public final class MetricsAdvisorKeyCredential {
13+
private volatile MetricsAdvisorKeys keys;
14+
private final Object updateLock = new Object();
15+
16+
/**
17+
* Creates a MetricsAdvisorKeyCredential credential that authorizes request with the given keys.
18+
*
19+
* @param subscriptionKey the subscription key used to authorize requests
20+
* @param apiKey the api key used to authorize requests
21+
*/
22+
public MetricsAdvisorKeyCredential(String subscriptionKey, String apiKey) {
23+
this.keys = new MetricsAdvisorKeys(subscriptionKey, apiKey);
24+
}
25+
26+
/**
27+
* Retrieves the {@link MetricsAdvisorKeys} containing the subscription key and api key
28+
* associated with this credential.
29+
*
30+
* @return The {@link MetricsAdvisorKeys} containing the subscription key and api key.
31+
*/
32+
public MetricsAdvisorKeys getKeys() {
33+
return this.keys;
34+
}
35+
36+
/**
37+
* Update the subscription and api key associated to this credential.
38+
* <p>
39+
* This is intended to be used when you've regenerated your subscription key and
40+
* api key want to update long lived clients.
41+
* </p>
42+
* @param subscriptionKey The new subscription key to associated with this credential.
43+
* @param apiKey The new api key to associated with this credential.
44+
* @return The updated {@code MetricsAdvisorKeyCredential} object.
45+
*/
46+
public MetricsAdvisorKeyCredential updateKey(String subscriptionKey, String apiKey) {
47+
synchronized (this.updateLock) {
48+
this.keys = new MetricsAdvisorKeys(subscriptionKey, apiKey);
49+
}
50+
return this;
51+
}
52+
}
53+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.ai.metricsadvisor.models;
5+
6+
import com.azure.core.annotation.Immutable;
7+
8+
/**
9+
* Represents a credential bag containing the subscription key and api key.
10+
*
11+
* @see MetricsAdvisorKeyCredential
12+
*/
13+
@Immutable
14+
public final class MetricsAdvisorKeys {
15+
private final String subscriptionKey;
16+
private final String apiKey;
17+
18+
MetricsAdvisorKeys(String subscriptionKey, String apiKey) {
19+
this.subscriptionKey = subscriptionKey;
20+
this.apiKey = apiKey;
21+
}
22+
23+
/**
24+
* Retrieves the subscription key.
25+
*
26+
* @return The subscription key.
27+
*/
28+
public String getSubscriptionKey() {
29+
return this.subscriptionKey;
30+
}
31+
32+
/**
33+
* Retrieves the api key.
34+
*
35+
* @return The api key.
36+
*/
37+
public String getApiKey() {
38+
return this.apiKey;
39+
}
40+
}
41+

sdk/metricsadvisor/azure-ai-metricsadvisor/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
requires transitive com.azure.core;
77

88
exports com.azure.ai.metricsadvisor;
9+
exports com.azure.ai.metricsadvisor.models;
910
}

0 commit comments

Comments
 (0)