@@ -18,13 +18,73 @@ const X_API_KEY_HEADER_NAME = "x-api-key";
1818 * Credential used to authenticate and authorize with Metrics Advisor service
1919 */
2020export class MetricsAdvisorKeyCredential {
21+ /**
22+ * Subscription access key from the Azure portal
23+ */
24+ private _subscriptionKey : string ;
25+
26+ /**
27+ * API key from the Metrics Advisor web portal
28+ */
29+ private _apiKey : string ;
30+
2131 /**
2232 * Creates an instance of MetricsAdvisorKeyCredential
2333 *
2434 * @param subscriptionKey - Subscription access key from the Azure portal
2535 * @param apiKey - API key from the Metrics Advisor web portal
2636 */
27- constructor ( readonly subscriptionKey : string , readonly apiKey : string ) { }
37+ constructor ( subscriptionKey : string , apiKey : string ) {
38+ if ( ! subscriptionKey || ! apiKey ) {
39+ throw new RangeError ( "Subscription Key or Api Key doesn't have a valid value" ) ;
40+ }
41+ this . _subscriptionKey = subscriptionKey ;
42+ this . _apiKey = apiKey ;
43+ }
44+
45+ /**
46+ * Get Api Key
47+ */
48+ public get apiKey ( ) : string {
49+ return this . _apiKey ;
50+ }
51+
52+ /**
53+ * Get Subscription key
54+ */
55+ public get subscriptionKey ( ) : string {
56+ return this . _subscriptionKey ;
57+ }
58+
59+ /**
60+ * Change the value of the subscription key.
61+ *
62+ * Updates will take effect upon the next request after
63+ * updating the key value.
64+ *
65+ * @param subscriptionKey - The new subscription key value to be used
66+ */
67+ public updateSubscriptionKey ( subscriptionKey : string ) : void {
68+ if ( ! subscriptionKey ) {
69+ throw new RangeError ( "subscriptionKey must be a non-empty string" ) ;
70+ }
71+ this . _subscriptionKey = subscriptionKey ;
72+ }
73+
74+ /**
75+ * Change the value of the api key.
76+ *
77+ * Updates will take effect upon the next request after
78+ * updating the key value.
79+ *
80+ * @param apiKey - The new api key value to be used
81+ */
82+ public updateApiKey ( apiKey : string ) : void {
83+ if ( ! apiKey ) {
84+ throw new RangeError ( "apiKey must be a non-empty string" ) ;
85+ }
86+ this . _apiKey = apiKey ;
87+ }
2888}
2989
3090/**
@@ -46,26 +106,21 @@ export function createMetricsAdvisorKeyCredentialPolicy(
46106 * using the appropriate header
47107 */
48108class MetricsAdvisorKeyCredentialPolicy extends BaseRequestPolicy {
49- private subscriptionKey : string ;
50- private apiKey : string ;
51-
52109 constructor (
53110 nextPolicy : RequestPolicy ,
54111 options : RequestPolicyOptionsLike ,
55- credential : MetricsAdvisorKeyCredential
112+ private _credential : MetricsAdvisorKeyCredential
56113 ) {
57114 super ( nextPolicy , options ) ;
58- this . subscriptionKey = credential . subscriptionKey ;
59- this . apiKey = credential . apiKey ;
60115 }
61116
62117 public async sendRequest ( webResource : WebResourceLike ) : Promise < HttpOperationResponse > {
63118 if ( ! webResource ) {
64119 throw new Error ( "webResource cannot be null or undefined" ) ;
65120 }
66121
67- webResource . headers . set ( API_KEY_HEADER_NAME , this . subscriptionKey ) ;
68- webResource . headers . set ( X_API_KEY_HEADER_NAME , this . apiKey ) ;
122+ webResource . headers . set ( API_KEY_HEADER_NAME , this . _credential . subscriptionKey ) ;
123+ webResource . headers . set ( X_API_KEY_HEADER_NAME , this . _credential . apiKey ) ;
69124 return this . _nextPolicy . sendRequest ( webResource ) ;
70125 }
71126}
0 commit comments