-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathClient.ts
More file actions
254 lines (237 loc) · 10.6 KB
/
Client.ts
File metadata and controls
254 lines (237 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// This file was auto-generated by Fern from our API Definition.
import type { BaseClientOptions, BaseRequestOptions } from "../../../../BaseClient.js";
import { type NormalizedClientOptionsWithAuth, normalizeClientOptionsWithAuth } from "../../../../BaseClient.js";
import { mergeHeaders } from "../../../../core/headers.js";
import * as core from "../../../../core/index.js";
import * as environments from "../../../../environments.js";
import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.js";
import * as errors from "../../../../errors/index.js";
import * as serializers from "../../../../serialization/index.js";
import * as AgentMail from "../../../index.js";
export declare namespace ApiKeysClient {
export type Options = BaseClientOptions;
export interface RequestOptions extends BaseRequestOptions {}
}
export class ApiKeysClient {
protected readonly _options: NormalizedClientOptionsWithAuth<ApiKeysClient.Options>;
constructor(options: ApiKeysClient.Options = {}) {
this._options = normalizeClientOptionsWithAuth(options);
}
/**
* @param {AgentMail.ListApiKeysRequest} request
* @param {ApiKeysClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @example
* await client.apiKeys.list()
*/
public list(
request: AgentMail.ListApiKeysRequest = {},
requestOptions?: ApiKeysClient.RequestOptions,
): core.HttpResponsePromise<AgentMail.ListApiKeysResponse> {
return core.HttpResponsePromise.fromPromise(this.__list(request, requestOptions));
}
private async __list(
request: AgentMail.ListApiKeysRequest = {},
requestOptions?: ApiKeysClient.RequestOptions,
): Promise<core.WithRawResponse<AgentMail.ListApiKeysResponse>> {
const { limit, pageToken, ascending } = request;
const _queryParams: Record<string, unknown> = {
limit,
page_token: pageToken,
ascending,
};
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
requestOptions?.headers,
);
const _response = await core.fetcher({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
((await core.Supplier.get(this._options.environment)) ?? environments.AgentMailEnvironment.Prod)
.http,
"/v0/api-keys",
),
method: "GET",
headers: _headers,
queryParameters: { ..._queryParams, ...requestOptions?.queryParams },
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return {
data: serializers.ListApiKeysResponse.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
rawResponse: _response.rawResponse,
};
}
if (_response.error.reason === "status-code") {
throw new errors.AgentMailError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/v0/api-keys");
}
/**
* @param {AgentMail.CreateApiKeyRequest} request
* @param {ApiKeysClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link AgentMail.ValidationError}
*
* @example
* await client.apiKeys.create({
* name: "name"
* })
*/
public create(
request: AgentMail.CreateApiKeyRequest,
requestOptions?: ApiKeysClient.RequestOptions,
): core.HttpResponsePromise<AgentMail.CreateApiKeyResponse> {
return core.HttpResponsePromise.fromPromise(this.__create(request, requestOptions));
}
private async __create(
request: AgentMail.CreateApiKeyRequest,
requestOptions?: ApiKeysClient.RequestOptions,
): Promise<core.WithRawResponse<AgentMail.CreateApiKeyResponse>> {
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
requestOptions?.headers,
);
const _response = await core.fetcher({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
((await core.Supplier.get(this._options.environment)) ?? environments.AgentMailEnvironment.Prod)
.http,
"/v0/api-keys",
),
method: "POST",
headers: _headers,
contentType: "application/json",
queryParameters: requestOptions?.queryParams,
requestType: "json",
body: serializers.CreateApiKeyRequest.jsonOrThrow(request, {
unrecognizedObjectKeys: "strip",
omitUndefined: true,
}),
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return {
data: serializers.CreateApiKeyResponse.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
rawResponse: _response.rawResponse,
};
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new AgentMail.ValidationError(
serializers.ValidationErrorResponse.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
_response.rawResponse,
);
default:
throw new errors.AgentMailError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/v0/api-keys");
}
/**
* @param {AgentMail.ApiKeyId} api_key
* @param {ApiKeysClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link AgentMail.NotFoundError}
*
* @example
* await client.apiKeys.delete("api_key")
*/
public delete(
api_key: AgentMail.ApiKeyId,
requestOptions?: ApiKeysClient.RequestOptions,
): core.HttpResponsePromise<void> {
return core.HttpResponsePromise.fromPromise(this.__delete(api_key, requestOptions));
}
private async __delete(
api_key: AgentMail.ApiKeyId,
requestOptions?: ApiKeysClient.RequestOptions,
): Promise<core.WithRawResponse<void>> {
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
requestOptions?.headers,
);
const _response = await core.fetcher({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
((await core.Supplier.get(this._options.environment)) ?? environments.AgentMailEnvironment.Prod)
.http,
`/v0/api-keys/${core.url.encodePathParam(serializers.ApiKeyId.jsonOrThrow(api_key, { omitUndefined: true }))}`,
),
method: "DELETE",
headers: _headers,
queryParameters: requestOptions?.queryParams,
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return { data: undefined, rawResponse: _response.rawResponse };
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 404:
throw new AgentMail.NotFoundError(
serializers.ErrorResponse.parseOrThrow(_response.error.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
skipValidation: true,
breadcrumbsPrefix: ["response"],
}),
_response.rawResponse,
);
default:
throw new errors.AgentMailError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "DELETE", "/v0/api-keys/{api_key}");
}
}