@@ -89,6 +89,10 @@ export class ApiClient {
8989 return ! ! ( this . oauth2Client && this . accessToken ) ;
9090 }
9191
92+ public async validateAccessToken ( ) : Promise < void > {
93+ await this . getAccessToken ( ) ;
94+ }
95+
9296 public async getIpInfo ( ) : Promise < {
9397 currentIpv4Address : string ;
9498 } > {
@@ -114,22 +118,59 @@ export class ApiClient {
114118 } > ;
115119 }
116120
117- async sendEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
118- let endpoint = "api/private/unauth/telemetry/events" ;
121+ public async sendEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
122+ if ( ! this . options . credentials ) {
123+ await this . sendUnauthEvents ( events ) ;
124+ return ;
125+ }
126+
127+ try {
128+ await this . sendAuthEvents ( events ) ;
129+ } catch ( error ) {
130+ if ( error instanceof ApiClientError ) {
131+ if ( error . response . status !== 401 ) {
132+ throw error ;
133+ }
134+ }
135+
136+ // send unauth events if any of the following are true:
137+ // 1: the token is not valid (not ApiClientError)
138+ // 2: if the api responded with 401 (ApiClientError with status 401)
139+ await this . sendUnauthEvents ( events ) ;
140+ }
141+ }
142+
143+ private async sendAuthEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
144+ const accessToken = await this . getAccessToken ( ) ;
145+ if ( ! accessToken ) {
146+ throw new Error ( "No access token available" ) ;
147+ }
148+ const authUrl = new URL ( "api/private/v1.0/telemetry/events" , this . options . baseUrl ) ;
149+ const response = await fetch ( authUrl , {
150+ method : "POST" ,
151+ headers : {
152+ Accept : "application/json" ,
153+ "Content-Type" : "application/json" ,
154+ "User-Agent" : this . options . userAgent ,
155+ Authorization : `Bearer ${ accessToken } ` ,
156+ } ,
157+ body : JSON . stringify ( events ) ,
158+ } ) ;
159+
160+ if ( ! response . ok ) {
161+ throw await ApiClientError . fromResponse ( response ) ;
162+ }
163+ }
164+
165+ private async sendUnauthEvents ( events : TelemetryEvent < CommonProperties > [ ] ) : Promise < void > {
119166 const headers : Record < string , string > = {
120167 Accept : "application/json" ,
121168 "Content-Type" : "application/json" ,
122169 "User-Agent" : this . options . userAgent ,
123170 } ;
124171
125- const accessToken = await this . getAccessToken ( ) ;
126- if ( accessToken ) {
127- endpoint = "api/private/v1.0/telemetry/events" ;
128- headers [ "Authorization" ] = `Bearer ${ accessToken } ` ;
129- }
130-
131- const url = new URL ( endpoint , this . options . baseUrl ) ;
132- const response = await fetch ( url , {
172+ const unauthUrl = new URL ( "api/private/unauth/telemetry/events" , this . options . baseUrl ) ;
173+ const response = await fetch ( unauthUrl , {
133174 method : "POST" ,
134175 headers,
135176 body : JSON . stringify ( events ) ,
@@ -245,6 +286,7 @@ export class ApiClient {
245286 "/api/atlas/v2/groups/{groupId}/clusters/{clusterName}" ,
246287 options
247288 ) ;
289+
248290 if ( error ) {
249291 throw ApiClientError . fromError ( response , error ) ;
250292 }
0 commit comments