1- import createClient , { Client , FetchOptions , Middleware } from "openapi-fetch" ;
1+ import createClient , { Client , Middleware } from "openapi-fetch" ;
2+ import type { FetchOptions } from "openapi-fetch" ;
23import { AccessToken , ClientCredentials } from "simple-oauth2" ;
34import { ApiClientError } from "./apiClientError.js" ;
45import { paths , operations } from "./openapi.js" ;
6+ import { BaseEvent } from "../../telemetry/types.js" ;
7+ import { mongoLogId } from "mongodb-log-writer" ;
8+ import logger from "../../logger.js" ;
59import { packageInfo } from "../../packageInfo.js" ;
610
711const ATLAS_API_VERSION = "2025-03-12" ;
@@ -93,6 +97,15 @@ export class ApiClient {
9397 this . client . use ( this . errorMiddleware ) ;
9498 }
9599
100+ public hasCredentials ( ) : boolean {
101+ logger . info (
102+ mongoLogId ( 1_000_000 ) ,
103+ "api-client" ,
104+ `Checking if API client has credentials: ${ ! ! ( this . oauth2Client && this . accessToken ) } `
105+ ) ;
106+ return ! ! ( this . oauth2Client && this . accessToken ) ;
107+ }
108+
96109 public async getIpInfo ( ) : Promise < {
97110 currentIpv4Address : string ;
98111 } > {
@@ -118,6 +131,32 @@ export class ApiClient {
118131 } > ;
119132 }
120133
134+ async sendEvents ( events : BaseEvent [ ] ) : Promise < void > {
135+ let endpoint = "api/private/unauth/telemetry/events" ;
136+ const headers : Record < string , string > = {
137+ Accept : "application/json" ,
138+ "Content-Type" : "application/json" ,
139+ "User-Agent" : this . options . userAgent ,
140+ } ;
141+
142+ const accessToken = await this . getAccessToken ( ) ;
143+ if ( accessToken ) {
144+ endpoint = "api/private/v1.0/telemetry/events" ;
145+ headers [ "Authorization" ] = `Bearer ${ accessToken } ` ;
146+ }
147+
148+ const url = new URL ( endpoint , this . options . baseUrl ) ;
149+ const response = await fetch ( url , {
150+ method : "POST" ,
151+ headers,
152+ body : JSON . stringify ( events ) ,
153+ } ) ;
154+
155+ if ( ! response . ok ) {
156+ throw await ApiClientError . fromResponse ( response ) ;
157+ }
158+ }
159+
121160 // DO NOT EDIT. This is auto-generated code.
122161 async listClustersForAllProjects ( options ?: FetchOptions < operations [ "listClustersForAllProjects" ] > ) {
123162 const { data } = await this . client . GET ( "/api/atlas/v2/clusters" , options ) ;
0 commit comments