DataDog / datadog-api-client-typescript

Typescript client for the Datadog API

Home Page:http://datadoghq.dev/datadog-api-client-typescript/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proxy support

tenjaa opened this issue · comments

Is your feature request related to a problem? Please describe.
We want to use this library behind a proxy. Unfortunately it only provides the option to pass a whole new http client. But we only need to add a proxy.

Describe the solution you'd like
Allow passing a proxy agent.

Describe alternatives you've considered
Use some library that magically reads the http_proxy environment variables. This would work too, but the first solution would be a bit less magic for the user.

Hi,

node-fetch does support passing an agent, but cross-fetch doesn't expose that I believe. We'll look into it, thanks.

It might be tough to add generic support. As you notice we allow passing a new client, which can work in that case. That's not a lot of code, here's one example:

import { v1 } from "@datadog/datadog-api-client";                                                                                                    
import pako from "pako";                                                                                                                             
import bufferFrom from "buffer-from";                                                                                                                
import fetch from "node-fetch";                                                                                                                      
import { HttpsProxyAgent } from "https-proxy-agent";                                                                                                 
                                                                                                                                                     
const proxyAgent = new HttpsProxyAgent('http://proxy:port');                                                                                         
                                                                                                                                                     
class HttpLibraryWithProxy implements v1.HttpLibrary {                                                                                               
  public debug = false;                                                                                                                              
                                                                                                                                                     
  public send(request: v1.RequestContext): Promise<v1.ResponseContext> {                                                                             
    const method = request.getHttpMethod().toString();                                                                                               
    let body = request.getBody();                                                                                                                    
                                                                                                                                                     
    let compress = request.getHttpConfig().compress;                                                                                                 
    if (compress === undefined) {                                                                                                                    
      compress = true;                                                                                                                               
    }                                                                                                                                                
                                                                                                                                                     
    const headers = request.getHeaders();                                                                                                            
    if (typeof body === "string") {                                                                                                                  
      if (headers["Content-Encoding"] == "gzip") {                                                                                                   
        body = bufferFrom(pako.gzip(body).buffer);                                                                                                   
      } else if (headers["Content-Encoding"] == "deflate") {                                                                                         
        body = bufferFrom(pako.deflate(body).buffer);                                                                                                
      }                                                                                                                                              
    }                                                                                                                                                
                                                                                                                                                     
    const resultPromise = fetch(request.getUrl(), {                                                                                                  
      method: method,                                                                                                                                
      body: body as any,                                                                                                                             
      headers: headers,                                                                                                                              
      signal: request.getHttpConfig().signal,                                                                                                        
      compress: compress,                                                                                                                            
      agent: proxyAgent,                                                                                                                             
    }).then((resp: any) => {                                                                                                                         
      const headers: { [name: string]: string } = {};                                                                                                
      resp.headers.forEach((value: string, name: string) => {                                                                                        
        headers[name] = value;                                                                                                                       
      });                                                                                                                                            
                                                                                                                                                     
      const body = {                                                                                                                                 
        text: () => resp.text(),                                                                                                                     
        binary: () => resp.buffer(),                                                                                                                 
      };                                                                                                                                             
      const response = new v1.ResponseContext(resp.status, headers, body);                                                                           
      return response;                                                                                                                               
    });                                                                                                                                              
                                                                                                                                                     
    return resultPromise;                                                                                                                            
  }                                                                                                                                                  
}                                                                                                                                                    
                                                                                                                                                     
const configuration = v1.createConfiguration({httpApi: new HttpLibraryWithProxy()});                                                                 
const apiInstance = new v1.MonitorsApi(configuration);                                                                                               
                                                                                                                                                     
apiInstance                                                                                                                                          
  .listMonitors()                                                                                                                                    
  .then((data: v1.Monitor[]) => {                                                                                                                    
    console.log(                                                                                                                                     
      "API called successfully. Returned data: " + JSON.stringify(data)                                                                              
    );                                                                                                                                               
  })                                                                                                                                                 
  .catch((error: any) => console.error(error)); 

Does that work for your purpose? If so we can document that solution officially. Thanks.

That looks good. 👍
We will try this out next week and can give some feedback.

Thanks for your contribution!

This issue has been automatically marked as stale because it has not had activity in the last 30 days. Note that the issue will not be automatically closed, but this notification will remind us to investigate why there's been inactivity. Thank you for participating in the Datadog open source community.

If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of this project.

  2. Comment that the issue is still reproducible and include updated details requested in the issue template.