Skip to content

Commit 39c2506

Browse files
authored
add readme for proxy usage (#844)
1 parent bd612ac commit 39c2506

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,78 @@ async function main() {
206206
main();
207207
```
208208

209+
### Configure proxy
210+
211+
You can provide custom `HttpLibrary` implementation with proxy support to `configuration` object. See example below:
212+
213+
```typescript
214+
import pako from "pako";
215+
import bufferFrom from "buffer-from";
216+
import fetch from "node-fetch";
217+
import { HttpsProxyAgent } from "https-proxy-agent";
218+
import { v1, client } from "@datadog/datadog-api-client";
219+
220+
const proxyAgent = new HttpsProxyAgent('http://127.0.0.11:3128');
221+
222+
class HttpLibraryWithProxy implements client.HttpLibrary {
223+
public debug = false;
224+
225+
public send(request: client.RequestContext): Promise<client.ResponseContext> {
226+
const method = request.getHttpMethod().toString();
227+
let body = request.getBody();
228+
229+
let compress = request.getHttpConfig().compress;
230+
if (compress === undefined) {
231+
compress = true;
232+
}
233+
234+
const headers = request.getHeaders();
235+
if (typeof body === "string") {
236+
if (headers["Content-Encoding"] == "gzip") {
237+
body = bufferFrom(pako.gzip(body).buffer);
238+
} else if (headers["Content-Encoding"] == "deflate") {
239+
body = bufferFrom(pako.deflate(body).buffer);
240+
}
241+
}
242+
243+
const resultPromise = fetch(request.getUrl(), {
244+
method: method,
245+
body: body as any,
246+
headers: headers,
247+
signal: request.getHttpConfig().signal,
248+
compress: compress,
249+
agent: proxyAgent,
250+
}).then((resp: any) => {
251+
const headers: { [name: string]: string } = {};
252+
resp.headers.forEach((value: string, name: string) => {
253+
headers[name] = value;
254+
});
255+
256+
const body = {
257+
text: () => resp.text(),
258+
binary: () => resp.buffer(),
259+
};
260+
const response = new client.ResponseContext(resp.status, headers, body);
261+
return response;
262+
});
263+
264+
return resultPromise;
265+
}
266+
}
267+
268+
const configuration = client.createConfiguration({httpApi: new HttpLibraryWithProxy()});
269+
const apiInstance = new v1.DashboardsApi(configuration);
270+
271+
apiInstance
272+
.listDashboards()
273+
.then((data: v1.DashboardSummary) => {
274+
console.log(
275+
"API called successfully. Returned data: " + JSON.stringify(data)
276+
);
277+
})
278+
.catch((error: any) => console.error(error));
279+
```
280+
209281
## Documentation
210282

211283
Documentation for API endpoints can be found in [GitHub pages][github pages].

0 commit comments

Comments
 (0)