pinnacleClient.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import dotenv from 'dotenv';
  2. import https from 'https';
  3. import axios from "axios";
  4. import { HttpsProxyAgent } from "https-proxy-agent";
  5. import { Logs } from "./logs.js";
  6. import { getAccountInfo } from "./getAccount.js";
  7. dotenv.config();
  8. const axiosDefaultOptions = {
  9. baseURL: "",
  10. url: "",
  11. method: "GET",
  12. headers: {},
  13. params: {},
  14. data: {},
  15. timeout: 10000,
  16. };
  17. const BaseURL = {
  18. pinnacle: "https://api.pinnacle888.com",
  19. ps3838: "https://api.ps3838.com",
  20. pstery: "http://127.0.0.1:9055",
  21. }
  22. export const pinnacleRequest = async (options, channel) => {
  23. const { username, password, localAddress, platform } = getAccountInfo() ?? {};
  24. const { url, ...optionsRest } = options;
  25. if (!url || !channel && (!username || !password)) {
  26. throw new Error("url、username、password、channel is required");
  27. }
  28. Logs.outDev('pinnacle request', { url, channel, username, password, localAddress, platform });
  29. const authHeader = channel ? `Basic ${channel}` : `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
  30. const axiosConfig = { ...axiosDefaultOptions, ...optionsRest, url, baseURL: BaseURL[platform] ?? BaseURL.pinnacle };
  31. Object.assign(axiosConfig.headers, {
  32. "Authorization": authHeader,
  33. "Accept": "application/json",
  34. });
  35. const proxy = process.env.NODE_HTTP_PROXY;
  36. if (proxy) {
  37. axiosConfig.proxy = false;
  38. axiosConfig.httpsAgent = new HttpsProxyAgent(proxy);
  39. // Logs.outDev('pinnacle request using proxy', axiosConfig.httpsAgent);
  40. }
  41. else if (localAddress) {
  42. axiosConfig.httpsAgent = new https.Agent({ localAddress });
  43. }
  44. return axios(axiosConfig).then(res => {
  45. // Logs.out('pinnacle request', url, axiosConfig, res.data);
  46. return res.data;
  47. });
  48. }
  49. /**
  50. * Pinnacle API Get请求
  51. * @param {*} url
  52. * @param {*} params
  53. * @returns
  54. */
  55. export const pinnacleGet = async (url, params, channel) => {
  56. return pinnacleRequest({
  57. url,
  58. params
  59. }, channel)
  60. .catch(err => {
  61. const source = { url, params };
  62. if (err?.response?.data) {
  63. const data = err.response.data;
  64. Object.assign(source, { data });
  65. }
  66. err.source = source;
  67. return Promise.reject(err);
  68. });
  69. }
  70. /**
  71. * Pinnacle API Post请求
  72. * @param {*} url
  73. * @param {*} data
  74. * @returns
  75. */
  76. export const pinnaclePost = async (url, data, channel) => {
  77. return pinnacleRequest({
  78. url,
  79. method: 'POST',
  80. headers: {
  81. 'Content-Type': 'application/json',
  82. },
  83. data
  84. }, channel);
  85. }
  86. export const getPsteryRelations = async (mk=-1) => {
  87. const axiosConfig = {
  88. baseURL: BaseURL.pstery,
  89. url: '/api/pstery/get_games_relation',
  90. method: 'GET',
  91. params: {
  92. mk,
  93. },
  94. proxy: false,
  95. };
  96. return axios(axiosConfig).then(res => res.data);
  97. }
  98. export const updateBaseEvents = async (data) => {
  99. const axiosConfig = {
  100. baseURL: BaseURL.pstery,
  101. url: '/api/pstery/update_base_events',
  102. method: 'POST',
  103. headers: {
  104. 'Content-Type': 'application/json',
  105. },
  106. data: JSON.stringify(data),
  107. proxy: false,
  108. };
  109. axios(axiosConfig).then(res => res.data)
  110. .then(() => {
  111. Logs.outDev('update base events success', data);
  112. })
  113. .catch(err => {
  114. Logs.err('failed to update base events:', err);
  115. });
  116. }
  117. export const notifyException = async (message) => {
  118. const axiosConfig = {
  119. baseURL: BaseURL.pstery,
  120. url: '/api/pstery/notify_exception',
  121. method: 'POST',
  122. data: { message },
  123. proxy: false,
  124. };
  125. axios(axiosConfig).then(res => res.data)
  126. .then(() => {
  127. Logs.out('notify exception success');
  128. })
  129. .catch(err => {
  130. Logs.err('failed to notify exception:', err.message);
  131. });
  132. }