HttpClient.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const axios = require('axios');
  2. const axiosRetry = require('axios-retry').default;
  3. const {HttpsProxyAgent} = require("https-proxy-agent");
  4. const ProxyService = require('./ProxyService');
  5. class HttpClient {
  6. constructor(options = {}) {
  7. this.client = axios.create({
  8. timeout: options.timeout || 5000,
  9. headers: options.headers || {},
  10. validateStatus: function (status) {
  11. return status >= 200 && status < 300;
  12. }
  13. });
  14. this.setupRetry();
  15. }
  16. setupRetry() {
  17. axiosRetry(this.client, {
  18. retries: 3,
  19. retryDelay: (retryCount) => {
  20. return retryCount * 1000;
  21. },
  22. retryCondition: (error) => {
  23. return axiosRetry.isNetworkOrIdempotentRequestError(error)
  24. || error.code === 'ECONNRESET';
  25. }
  26. });
  27. }
  28. // POST 方法
  29. async post(url, data = {}, config = {}) {
  30. try {
  31. const response = await this.client.post(url, data, {
  32. headers: {
  33. 'Content-Type': 'application/json',
  34. ...config.headers
  35. },
  36. ...config
  37. });
  38. return response.data;
  39. } catch (error) {
  40. this.handleError(error);
  41. throw error;
  42. }
  43. }
  44. // POST 表单数据
  45. async postForm(url, formData, config = {}) {
  46. try {
  47. const response = await this.client.post(url, formData, {
  48. headers: {
  49. 'Content-Type': 'multipart/form-data',
  50. ...config.headers
  51. },
  52. ...config
  53. });
  54. return response.data;
  55. } catch (error) {
  56. this.handleError(error);
  57. throw error;
  58. }
  59. }
  60. async request(url, options = {}) {
  61. try {
  62. const response = await this.client.request({
  63. url,
  64. ...options
  65. });
  66. return response.data;
  67. } catch (error) {
  68. this.handleError(error);
  69. throw error;
  70. }
  71. }
  72. // POST 方法
  73. async otherPost(options,data) {
  74. try {
  75. const response = await this.client.post(options.url, data, {
  76. headers:options.headers
  77. });
  78. return response.data;
  79. } catch (error) {
  80. this.handleError(error);
  81. throw error;
  82. }
  83. }
  84. async proxyRequest(options){
  85. try{
  86. const proxyService = new ProxyService();
  87. const result = await proxyService.proxyRequestWithRetry({
  88. url: options.url,
  89. method: "post",
  90. headers: options.headers
  91. })
  92. return result
  93. // let tunnelHost = 'd528.kdltps.com'
  94. // let tunnelPort = '15818'
  95. // // 配置用户名和密码
  96. // let username = 't13322192973984'
  97. // let password = '7ntreedr'
  98. // return await axios({
  99. // url: options.url,
  100. // method: "post",
  101. // headers: options.headers,
  102. // httpAgent: new HttpsProxyAgent(`http://${username}:${password}@${tunnelHost}:${tunnelPort}`),
  103. // httpsAgent: new HttpsProxyAgent(`http://${username}:${password}@${tunnelHost}:${tunnelPort}`),
  104. // }).then(
  105. // res => {
  106. // return res.data
  107. // }
  108. // ).catch(err => {
  109. // console.log(err);
  110. // })
  111. }catch(error){
  112. this.handleError(error);
  113. throw error;
  114. }
  115. }
  116. handleError(error) {
  117. if (error.code === 'ECONNRESET') {
  118. console.error('连接被重置');
  119. } else if (error.code === 'ETIMEDOUT') {
  120. console.error('请求超时');
  121. } else if (error.response) {
  122. console.error(`服务器响应错误: ${error.response.status}`);
  123. } else if (error.request) {
  124. console.error('未收到响应');
  125. } else {
  126. console.error('请求配置错误');
  127. }
  128. }
  129. }
  130. module.exports = HttpClient;