Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change sync-fetch to node-fetch #311

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
const fs = require('fs')
const path = require('path')
const fetch = require('sync-fetch')
const fetch = require('node-fetch')
const URLSearchParams = require('@ungap/url-search-params')

module.exports = {
Expand All @@ -20,7 +20,7 @@ module.exports = {
console.log('Netatmo helper started ...')
this.token = null
},
authenticate: function (config) {
authenticate: async function (config) {
const self = this
self.config = config

Expand All @@ -31,20 +31,22 @@ module.exports = {
params.append('client_secret', self.config.clientSecret)

try {
const result = fetch('https://' + self.config.apiBase + self.config.authEndpoint, {
const result = await fetch('https://' + self.config.apiBase + self.config.authEndpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: params,
}).json()
})

if (result.error) {
throw new Error(result.error + ': ' + result.error_description)
}

console.log('UPDATING TOKEN ' + result.access_token)
self.token = result.access_token
self.token_expires_in = result.expires_in
self.refresh_token = result.refresh_token
const authData = await result.json()

console.log('UPDATING TOKEN ' + authData.access_token)
self.token = authData.access_token
self.token_expires_in = authData.expires_in
self.refresh_token = authData.refresh_token
// we got a new token, save it to main file to allow it to request the datas
self.sendSocketNotification(self.notifications.AUTH_RESPONSE, {
status: 'OK',
Expand All @@ -58,7 +60,7 @@ module.exports = {
})
}
},
loadData: function (config) {
loadData: async function (config) {
const self = this
self.config = config

Expand All @@ -79,7 +81,7 @@ module.exports = {
}

try {
let result = fetch('https://' + self.config.apiBase + self.config.dataEndpoint, {
let result = await fetch('https://' + self.config.apiBase + self.config.dataEndpoint, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${self.token}`,
Expand All @@ -96,7 +98,7 @@ module.exports = {
return
}

result = result.json()
result = await result.json()

if (result.error) {
throw new Error(result.error.message)
Expand Down
13 changes: 1 addition & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@
"snyk": true,
"dependencies": {
"@ungap/url-search-params": "0.2.2",
"sync-fetch": "0.5.2"
"node-fetch": "^2.7.0"
}
}
Loading