forked from axiomhq/deno-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.ts
136 lines (113 loc) · 3.06 KB
/
client.ts
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
// deno-lint-ignore-file ban-types
const env = Deno.env.toObject();
const defaultAxiomEndpoint = "https://cloud.axiom.co";
interface IngestOptions {
timestampField?: string;
timestampFormat?: string;
csvDelimiter?: string;
}
interface IngestInput {
events: object[];
dataset: string;
// options?: IngestOptions;
}
interface IngestStatus {
ingested: number;
failed: number;
failuers: string[];
processedBytes: number;
blocksCreated: number;
walLength: number;
}
interface QueryInput {
startTime?: Date;
endTime?: Date;
apl: string;
option?: Map<string, string>;
//datasetMap?: Map<string, string>;
}
interface QueryOutput {
request: object;
status: object;
matches: object[];
buckets: {
series: object[];
totals: object[];
};
datasetNames: string[];
// fieldsMetaMap: object,
}
export class Client {
private url: string;
private apiToken?: string;
constructor(params?: { url?: string; apiToken?: string }) {
this.url = params?.url ?? env.AXIOM_URL ?? defaultAxiomEndpoint;
this.apiToken = params?.apiToken ?? env.AXIOM_TOKEN;
}
async ingest(params: IngestInput): Promise<IngestStatus> {
const url = new URL(this.url);
url.pathname = `/api/v1/datasets/${params.dataset}/ingest`;
const body = JSON.stringify(params.events);
const headers = {
"Content-Type": "application/json",
"User-Agent": "axiom-deno",
"Authorization": `Bearer ${this.apiToken}`,
};
const resp = await fetch(url.toString(), {
method: "POST",
headers: headers,
body,
});
if (resp.status != 200) {
throw new Error(`Ingest failed: ${resp.status}`);
}
const status = await resp.json();
return {
ingested: status.ingested,
failed: status.failed,
failuers: status.failuers,
processedBytes: status.processedBytes,
blocksCreated: status.blocksCreated,
walLength: status.walLength,
};
}
async query(params: QueryInput): Promise<QueryOutput> {
const url = new URL(this.url);
url.pathname = `/api/v1/datasets/_apl`;
url.searchParams.set("format", "legacy");
for (const [key, value] of params.option?.entries() ?? []) {
url.searchParams.set(key, value);
}
const headers = {
"Content-Type": "application/json",
"User-Agent": "axiom-deno",
"Authorization": `Bearer ${this.apiToken}`,
};
const body = JSON.stringify({
startTime: params.startTime?.toISOString(),
endTime: params.endTime?.toISOString(),
apl: params.apl,
});
const resp = await fetch(url.toString(), {
method: "POST",
headers: headers,
body,
});
if (resp.status != 200) {
try {
const error = await resp.json();
throw new Error(`Query failed: ${error.message}`);
} catch (_e) {
throw new Error(`Query failed: ${resp.status}`);
}
}
const res = await resp.json();
return {
request: res.request,
status: res.status,
matches: res.matches,
buckets: res.buckets,
datasetNames: res.datasetNames,
};
}
}