1
- import createClient , { Client , FetchOptions , Middleware } from "openapi-fetch" ;
1
+ import createClient , { Client , Middleware } from "openapi-fetch" ;
2
+ import type { FetchOptions } from "openapi-fetch" ;
2
3
import { AccessToken , ClientCredentials } from "simple-oauth2" ;
3
4
import { ApiClientError } from "./apiClientError.js" ;
4
5
import { 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" ;
5
9
import { packageInfo } from "../../packageInfo.js" ;
6
10
7
11
const ATLAS_API_VERSION = "2025-03-12" ;
@@ -93,6 +97,15 @@ export class ApiClient {
93
97
this . client . use ( this . errorMiddleware ) ;
94
98
}
95
99
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
+
96
109
public async getIpInfo ( ) : Promise < {
97
110
currentIpv4Address : string ;
98
111
} > {
@@ -118,6 +131,32 @@ export class ApiClient {
118
131
} > ;
119
132
}
120
133
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
+
121
160
// DO NOT EDIT. This is auto-generated code.
122
161
async listClustersForAllProjects ( options ?: FetchOptions < operations [ "listClustersForAllProjects" ] > ) {
123
162
const { data } = await this . client . GET ( "/api/atlas/v2/clusters" , options ) ;
0 commit comments