Giter Club home page Giter Club logo

Comments (5)

therve avatar therve commented on May 27, 2024 1

Hi,

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

from datadog-api-client-typescript.

tenjaa avatar tenjaa commented on May 27, 2024 1

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

from datadog-api-client-typescript.

therve avatar therve commented on May 27, 2024

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.

from datadog-api-client-typescript.

github-actions avatar github-actions commented on May 27, 2024

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.

from datadog-api-client-typescript.

therve avatar therve commented on May 27, 2024

This is now documented here: https://github.com/DataDog/datadog-api-client-typescript#configure-proxy

from datadog-api-client-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.