Skip to content

Commit b8bfbf2

Browse files
skarimoNouemanKHALci.datadog-api-spec
authored
Add support for configuring zstdCompressorCallback (#811)
* add support for ZstdCompressorCallback * Update .generator/src/generator/templates/configuration.j2 Co-authored-by: NouemanKHAL <[email protected]> * include zstdCompressorCallback in test config * update licenses * pre-commit fixes * update readme * code review suggestion Co-authored-by: NouemanKHAL <[email protected]> Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent 93c7c87 commit b8bfbf2

File tree

11 files changed

+101
-6
lines changed

11 files changed

+101
-6
lines changed

.generator/src/generator/templates/configuration.j2

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpLibrary, HttpConfiguration, RequestContext } from "./http/http";
1+
import { HttpLibrary, HttpConfiguration, RequestContext, ZstdCompressorCallback } from "./http/http";
22
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "./http/isomorphic-fetch";
33
import { BaseServerConfiguration, server1, servers, operationServers } from "./servers";
44
import { configureAuthMethods, AuthMethods, AuthMethodsConfiguration } from "./auth";
@@ -47,6 +47,10 @@ export interface ConfigurationParameters {
4747
* Flag to enable requests tracing
4848
*/
4949
debug?: boolean
50+
/**
51+
* Callback method to compress string body with zstd
52+
*/
53+
zstdCompressorCallback?: ZstdCompressorCallback
5054
}
5155

5256
/**
@@ -101,6 +105,7 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
101105
httpConfig: conf.httpConfig || {},
102106
debug: conf.debug
103107
};
108+
configuration.httpApi.zstdCompressorCallback = conf.zstdCompressorCallback
104109
configuration.httpApi.debug = configuration.debug;
105110
return configuration;
106111
}

.generator/src/generator/templates/http/http.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ export class ResponseContext {
234234
}
235235
}
236236

237+
export type ZstdCompressorCallback = (body: string) => Buffer;
238+
237239
export interface HttpLibrary {
238240
debug?: boolean;
241+
zstdCompressorCallback?: ZstdCompressorCallback;
239242
send(request: RequestContext): Promise<ResponseContext>;
240243
}

.generator/src/generator/templates/http/isomorphic-fetch.j2

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { HttpLibrary, RequestContext, ResponseContext } from "./http";
1+
import { HttpLibrary, RequestContext, ResponseContext, ZstdCompressorCallback } from "./http";
22
import fetch from "cross-fetch";
33
import pako from "pako";
44
import bufferFrom from "buffer-from";
55

66
export class IsomorphicFetchHttpLibrary implements HttpLibrary {
77
public debug = false;
8+
public zstdCompressorCallback: ZstdCompressorCallback | undefined;
89

910
public send(request: RequestContext): Promise<ResponseContext> {
1011
if (this.debug) {
@@ -24,6 +25,12 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
2425
body = bufferFrom(pako.gzip(body).buffer);
2526
} else if (headers["Content-Encoding"] == "deflate") {
2627
body = bufferFrom(pako.deflate(body).buffer);
28+
} else if (headers["Content-Encoding"] == "zstd1") {
29+
if (this.zstdCompressorCallback) {
30+
body = this.zstdCompressorCallback(body);
31+
} else {
32+
throw new Error("zstdCompressorCallback method missing")
33+
}
2734
}
2835
}
2936

LICENSE-3rdparty.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ typedoc,Apache-2.0,Copyright (c) 2015 Sebastian Lenz Copyright (c) 2016-2021 Typ
3939
typescript,Apache-2.0,Copyright (c) Microsoft Corporation.
4040
url-parse,MIT,Copyright (c) 2015 Unshift.io, Arnout Kazemier, the Contributors.
4141
loglevel,MIT,Copyright (c) 2013 Tim Perry
42+
zstd.ts,BSD-2-Clause,Copyright (c) [2021] [Beeno Tung (Tung Cheung Leong)]

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,54 @@ async function main() {
161161
main();
162162
```
163163

164+
### Zstd compression
165+
166+
Zstd compression support requires users to supply their own zstd compressor callback function.
167+
The callback should accept string arg and return compressed Buffer data.
168+
Callback signature `(body: string) => Buffer`.
169+
For example, using `zstd.ts` package:
170+
171+
```typescript
172+
import { compressSync } from 'zstd.ts'
173+
import { client, v2 } from "@datadog/datadog-api-client";
174+
175+
async function main() {
176+
const configurationOpts = {
177+
zstdCompressorCallback: (body: string) => compressSync({input: Buffer.from(body, "utf8")})
178+
}
179+
const configuration = client.createConfiguration(configurationOpts);
180+
const apiInstance = new v2.MetricsApi(configuration);
181+
const params: v2.MetricsApiSubmitMetricsRequest = {
182+
body: {
183+
series: [
184+
{
185+
metric: "system.load.1",
186+
type: 0,
187+
points: [
188+
{
189+
timestamp: Math.round(new Date().getTime() / 1000),
190+
value: 0.7,
191+
},
192+
],
193+
},
194+
],
195+
},
196+
contentEncoding: "zstd1",
197+
};
198+
199+
apiInstance.submitMetrics(params).then((data: v2.IntakePayloadAccepted) => {
200+
console.log(
201+
"API called successfully. Returned data: " + JSON.stringify(data)
202+
);
203+
}).catch((error: any) => console.error(error));
204+
}
205+
206+
main();
207+
```
208+
164209
## Documentation
165210

166-
Documentation for API endpoints can be found in in [Github pages][github pages].
211+
Documentation for API endpoints can be found in [GitHub pages][github pages].
167212

168213
## Contributing
169214

features/support/given.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Given } from "@cucumber/cucumber";
2+
import { compressSync } from 'zstd.ts'
23

34
import fs from "fs";
45
import path from "path";
@@ -43,7 +44,9 @@ for (const apiVersion of Versions) {
4344
appKeyAuth: process.env.DD_TEST_CLIENT_APP_KEY,
4445
},
4546
httpConfig: { compress: false },
47+
zstdCompressorCallback: (body: string) => compressSync({input: Buffer.from(body, "utf8")}),
4648
};
49+
4750
if (process.env.DD_TEST_SITE) {
4851
const server = datadogApiClient.client.servers[2];
4952
const serverConf = server.getConfiguration();

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@
105105
"ts-jest": "^27.0.4",
106106
"ts-node": "^9.1.1",
107107
"typedoc": "^0.22.11",
108-
"typescript": "4.5.5"
108+
"typescript": "4.5.5",
109+
"zstd.ts": "^1.1.3"
109110
},
110111
"engines": {
111112
"node": ">=12.0.0"

packages/datadog-api-client-common/configuration.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { HttpLibrary, HttpConfiguration, RequestContext } from "./http/http";
1+
import {
2+
HttpLibrary,
3+
HttpConfiguration,
4+
RequestContext,
5+
ZstdCompressorCallback,
6+
} from "./http/http";
27
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "./http/isomorphic-fetch";
38
import {
49
BaseServerConfiguration,
@@ -55,6 +60,10 @@ export interface ConfigurationParameters {
5560
* Flag to enable requests tracing
5661
*/
5762
debug?: boolean;
63+
/**
64+
* Callback method to compress string body with zstd
65+
*/
66+
zstdCompressorCallback?: ZstdCompressorCallback;
5867
}
5968

6069
/**
@@ -129,6 +138,7 @@ export function createConfiguration(
129138
httpConfig: conf.httpConfig || {},
130139
debug: conf.debug,
131140
};
141+
configuration.httpApi.zstdCompressorCallback = conf.zstdCompressorCallback;
132142
configuration.httpApi.debug = configuration.debug;
133143
return configuration;
134144
}

packages/datadog-api-client-common/http/http.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ export class ResponseContext {
234234
}
235235
}
236236

237+
export type ZstdCompressorCallback = (body: string) => Buffer;
238+
237239
export interface HttpLibrary {
238240
debug?: boolean;
241+
zstdCompressorCallback?: ZstdCompressorCallback;
239242
send(request: RequestContext): Promise<ResponseContext>;
240243
}

packages/datadog-api-client-common/http/isomorphic-fetch.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import { HttpLibrary, RequestContext, ResponseContext } from "./http";
1+
import {
2+
HttpLibrary,
3+
RequestContext,
4+
ResponseContext,
5+
ZstdCompressorCallback,
6+
} from "./http";
27
import fetch from "cross-fetch";
38
import pako from "pako";
49
import bufferFrom from "buffer-from";
510

611
export class IsomorphicFetchHttpLibrary implements HttpLibrary {
712
public debug = false;
13+
public zstdCompressorCallback: ZstdCompressorCallback | undefined;
814

915
public send(request: RequestContext): Promise<ResponseContext> {
1016
if (this.debug) {
@@ -24,6 +30,12 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
2430
body = bufferFrom(pako.gzip(body).buffer);
2531
} else if (headers["Content-Encoding"] == "deflate") {
2632
body = bufferFrom(pako.deflate(body).buffer);
33+
} else if (headers["Content-Encoding"] == "zstd1") {
34+
if (this.zstdCompressorCallback) {
35+
body = this.zstdCompressorCallback(body);
36+
} else {
37+
throw new Error("zstdCompressorCallback method missing");
38+
}
2739
}
2840
}
2941

0 commit comments

Comments
 (0)