test_read.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const axios = require('axios');
  2. const YAML = require('yaml');
  3. const https = require('node:https');
  4. const { parseArgs } = require('node:util');
  5. const url = require('node:url');
  6. const path_ = require('path');
  7. const fs = require('fs');
  8. let config;
  9. try {
  10. ({ values: {
  11. config,
  12. }, positionals: [id] } = parseArgs({
  13. options: {
  14. config: {
  15. type: 'string',
  16. },
  17. },
  18. allowPositionals: true,
  19. }));
  20. } catch (e) {
  21. if ( args.length < 1 ) {
  22. console.error(
  23. 'Usage: readdir_profile [OPTIONS]\n' +
  24. '\n' +
  25. 'Options:\n' +
  26. ' --config=<path> (required) Path to configuration file\n' +
  27. ''
  28. );
  29. process.exit(1);
  30. }
  31. }
  32. const conf = YAML.parse(fs.readFileSync(config).toString());
  33. const entry = `/${conf.username}/read_test.txt`;
  34. // process.on('SIGINT', async () => {
  35. // process.exit(0);
  36. // });
  37. const httpsAgent = new https.Agent({
  38. rejectUnauthorized: false
  39. })
  40. const getURL = (...path) => {
  41. const apiURL = new url.URL(conf.url);
  42. apiURL.pathname = path_.posix.join(
  43. apiURL.pathname,
  44. ...path
  45. );
  46. return apiURL.href;
  47. };
  48. const main = async () => {
  49. const resp = await axios.request({
  50. httpsAgent,
  51. method: 'get',
  52. url: getURL('read'),
  53. params: {
  54. file: entry,
  55. },
  56. headers: {
  57. 'Authorization': `Bearer ${conf.token}`,
  58. }
  59. })
  60. console.log(resp.data);
  61. }
  62. main();