-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathClient.ts
More file actions
205 lines (192 loc) · 8.65 KB
/
Client.ts
File metadata and controls
205 lines (192 loc) · 8.65 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
// 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 AgentClient {
export type Options = BaseClientOptions;
export interface RequestOptions extends BaseRequestOptions {}
}
export class AgentClient {
protected readonly _options: NormalizedClientOptionsWithAuth<AgentClient.Options>;
constructor(options: AgentClient.Options = {}) {
this._options = normalizeClientOptionsWithAuth(options);
}
/**
* Create a new agent organization with an inbox and API key. This endpoint is for signing up for the first time. If you've already signed up, you're all set — just use your existing API key.
*
* A 6-digit OTP is sent to the human's email for verification.
*
* This endpoint is idempotent. Calling it again with the same `human_email` will rotate the API key and resend the OTP if expired.
*
* The returned API key has limited permissions until the organization is verified via the verify endpoint.
*
* **CLI:**
* ```bash
* agentmail agent sign-up --human-email user@example.com --username my-agent
* ```
*
* @param {AgentMail.AgentSignupRequest} request
* @param {AgentClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link AgentMail.ValidationError}
*
* @example
* await client.agent.signUp({
* humanEmail: "human_email",
* username: "username"
* })
*/
public signUp(
request: AgentMail.AgentSignupRequest,
requestOptions?: AgentClient.RequestOptions,
): core.HttpResponsePromise<AgentMail.AgentSignupResponse> {
return core.HttpResponsePromise.fromPromise(this.__signUp(request, requestOptions));
}
private async __signUp(
request: AgentMail.AgentSignupRequest,
requestOptions?: AgentClient.RequestOptions,
): Promise<core.WithRawResponse<AgentMail.AgentSignupResponse>> {
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(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/agent/sign-up",
),
method: "POST",
headers: _headers,
contentType: "application/json",
queryParameters: requestOptions?.queryParams,
requestType: "json",
body: serializers.AgentSignupRequest.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.AgentSignupResponse.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/agent/sign-up");
}
/**
* Verify an agent organization using the 6-digit OTP sent to the human's email during sign-up.
*
* On success, the organization is upgraded from `agent_unverified` to `agent_verified`, the send allowlist is removed, and free plan entitlements are applied.
*
* The OTP expires after 24 hours and allows a maximum of 10 attempts.
*
* **CLI:**
* ```bash
* agentmail agent verify --otp-code 123456
* ```
*
* @param {AgentMail.AgentVerifyRequest} request
* @param {AgentClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @example
* await client.agent.verify({
* otpCode: "otp_code"
* })
*/
public verify(
request: AgentMail.AgentVerifyRequest,
requestOptions?: AgentClient.RequestOptions,
): core.HttpResponsePromise<AgentMail.AgentVerifyResponse> {
return core.HttpResponsePromise.fromPromise(this.__verify(request, requestOptions));
}
private async __verify(
request: AgentMail.AgentVerifyRequest,
requestOptions?: AgentClient.RequestOptions,
): Promise<core.WithRawResponse<AgentMail.AgentVerifyResponse>> {
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/agent/verify",
),
method: "POST",
headers: _headers,
contentType: "application/json",
queryParameters: requestOptions?.queryParams,
requestType: "json",
body: serializers.AgentVerifyRequest.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.AgentVerifyResponse.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, "POST", "/v0/agent/verify");
}
}