pinnacleClient.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 { username } = getAccountInfo() ?? {};
  62. const source = { url, params, username };
  63. if (err?.response?.data) {
  64. const data = err.response.data;
  65. Object.assign(source, { data });
  66. }
  67. err.source = source;
  68. return Promise.reject(err);
  69. });
  70. }
  71. /**
  72. * Pinnacle API Post请求
  73. * @param {*} url
  74. * @param {*} data
  75. * @returns
  76. */
  77. export const pinnaclePost = async (url, data, channel) => {
  78. return pinnacleRequest({
  79. url,
  80. method: 'POST',
  81. headers: {
  82. 'Content-Type': 'application/json',
  83. },
  84. data
  85. }, channel);
  86. }
  87. export const getPsteryRelations = async (mk=-1) => {
  88. const axiosConfig = {
  89. baseURL: BaseURL.pstery,
  90. url: '/api/pstery/get_games_relation',
  91. method: 'GET',
  92. params: {
  93. mk,
  94. },
  95. proxy: false,
  96. };
  97. return axios(axiosConfig).then(res => res.data);
  98. }
  99. export const updateBaseEvents = async (data) => {
  100. const axiosConfig = {
  101. baseURL: BaseURL.pstery,
  102. url: '/api/pstery/update_base_events',
  103. method: 'POST',
  104. headers: {
  105. 'Content-Type': 'application/json',
  106. },
  107. data: JSON.stringify(data),
  108. proxy: false,
  109. };
  110. axios(axiosConfig).then(res => res.data)
  111. .then(() => {
  112. Logs.outDev('update base events success', data);
  113. })
  114. .catch(err => {
  115. Logs.err('failed to update base events:', err.response?.data ?? err.message);
  116. });
  117. }
  118. export const notifyException = async (message) => {
  119. const axiosConfig = {
  120. baseURL: BaseURL.pstery,
  121. url: '/api/pstery/notify_exception',
  122. method: 'POST',
  123. data: { message },
  124. proxy: false,
  125. };
  126. axios(axiosConfig).then(res => res.data)
  127. .then(() => {
  128. Logs.out('notify exception success');
  129. })
  130. .catch(err => {
  131. Logs.err('failed to notify exception:', err.message);
  132. });
  133. }