-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathClient.ts
More file actions
134 lines (125 loc) · 5.56 KB
/
Client.ts
File metadata and controls
134 lines (125 loc) · 5.56 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
// 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 { toJson } from "../../../../core/json.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 MetricsClient {
export type Options = BaseClientOptions;
export interface RequestOptions extends BaseRequestOptions {}
}
export class MetricsClient {
protected readonly _options: NormalizedClientOptionsWithAuth<MetricsClient.Options>;
constructor(options: MetricsClient.Options = {}) {
this._options = normalizeClientOptionsWithAuth(options);
}
/**
* **CLI:**
* ```bash
* agentmail metrics list
* ```
*
* @param {AgentMail.QueryMetricsRequest} request
* @param {MetricsClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link AgentMail.ValidationError}
*
* @example
* await client.metrics.query()
*/
public query(
request: AgentMail.QueryMetricsRequest = {},
requestOptions?: MetricsClient.RequestOptions,
): core.HttpResponsePromise<AgentMail.QueryMetricsResponse> {
return core.HttpResponsePromise.fromPromise(this.__query(request, requestOptions));
}
private async __query(
request: AgentMail.QueryMetricsRequest = {},
requestOptions?: MetricsClient.RequestOptions,
): Promise<core.WithRawResponse<AgentMail.QueryMetricsResponse>> {
const { eventTypes, start, end, period, limit, descending } = request;
const _queryParams: Record<string, unknown> = {
event_types:
eventTypes != null
? toJson(
serializers.MetricEventTypes.jsonOrThrow(eventTypes, {
unrecognizedObjectKeys: "strip",
omitUndefined: true,
}),
)
: undefined,
start:
start != null
? serializers.Start.jsonOrThrow(start, { unrecognizedObjectKeys: "strip", omitUndefined: true })
: undefined,
end:
end != null
? serializers.End.jsonOrThrow(end, { unrecognizedObjectKeys: "strip", omitUndefined: true })
: undefined,
period,
limit,
descending,
};
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/metrics",
),
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.QueryMetricsResponse.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, "GET", "/v0/metrics");
}
}