Guest User

Untitled

a guest
Jan 14th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. const axios = require('axios')
  2. const cryptoJs = require('crypto-js')
  3. const Base64 = require('js-base64').Base64
  4.  
  5. const apiToken = 'fwaf'
  6. const apiSecret = 'fwaf'
  7. const projectsUrl = 'fwaf'
  8.  
  9. class Api {
  10. constructor(apiToken, apiSecret, projectsUrl) {
  11. this.updateInterval
  12.  
  13. this.apiToken = apiToken
  14. this.apiSecret = apiSecret
  15. this.projectsUrl = projectsUrl
  16.  
  17. this.signature = this.genereteSignature()
  18. console.log(this.signature)
  19. }
  20.  
  21. genereteSignature() {
  22. const postParams = ''
  23. const method = 'GET'
  24. return Base64.encode(cryptoJs.HmacSHA256(`${this.projectsUrl}${method}${postParams}`, this.apiSecret))
  25. }
  26.  
  27. start() {
  28. this.updateInterval = setInterval(this.getUpdate, 5000)
  29. }
  30.  
  31. async getUpdate() {
  32. let projects
  33. try {
  34. projects = (await axios.get(projectsUrl, {
  35. auth: {
  36. username: this.apiToken,
  37. password: this.signature
  38. },
  39. })).data
  40. } catch (err) {
  41. console.error(err.message)
  42. }
  43. console.log(`Last projects: ${projects}`)
  44. }
  45. }
  46.  
  47. const api = new Api(apiToken, apiSecret, projectsUrl)
  48.  
  49. api.start()
Add Comment
Please, Sign In to add comment