Skip to content

Commit

Permalink
Merge pull request #130 from st1cky/master
Browse files Browse the repository at this point in the history
Add auth headers to getRequest
  • Loading branch information
nitaybz authored Mar 3, 2024
2 parents d2e95f6 + 1f7272c commit 8d271d9
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions tado/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let axios = axiosLib.create();
const qs = require('qs')

const baseURL = 'https://my.tado.com/api/v2'
let log, storage, token, settings, homeId
let log, storage, token, settings, homeId, username, password

module.exports = async function (platform) {
log = platform.log
Expand All @@ -17,11 +17,9 @@ module.exports = async function (platform) {
settings = {}
}


axios.defaults.params = {
username: platform.username,
password: platform.password
}
// make available for getToken
username = platform.username
password = platform.username

axios.defaults.baseURL = baseURL

Expand Down Expand Up @@ -131,12 +129,23 @@ module.exports = async function (platform) {


function getRequest(url) {
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {

let headers
try {
const tokenResponse = await getToken()
headers = {
'Authorization': 'Bearer ' + tokenResponse
}
} catch (err) {
log('[GET] The plugin was NOT able to find stored token or acquire one from tado° API')
reject(err)
}

log.easyDebug(`Creating GET request to tado° API --->`)
log.easyDebug(baseURL + url)

axios.get(url)
axios.get(url, { headers })
.then(response => {
const json = response.data
log.easyDebug(`Successful GET response:`)
Expand All @@ -163,7 +172,7 @@ function setRequest(method, url, data) {
'Authorization': 'Bearer ' + tokenResponse
}
} catch (err) {
log('The plugin was NOT able to find stored token or acquire one from tado° API ---> it will not be able to set the state !!')
log('[SET] The plugin was NOT able to find stored token or acquire one from tado° API ---> it will not be able to set the state !!')
reject(err)
}

Expand Down Expand Up @@ -202,6 +211,8 @@ function getToken() {
grant_type: 'password',
client_id: 'tado-web-app',
client_secret: 'wZaRN7rpjn3FoNyF5IFuxg9uMzYJcvOoQ8QWiIqS3hfk6gLhVlG57j5YNoZL2Rtc',
username: username,
password: password,
scope: 'home.user'
}
data = qs.stringify(data, { encode: false })
Expand Down Expand Up @@ -376,4 +387,4 @@ const get = {
throw err
}
}
}
}

0 comments on commit 8d271d9

Please sign in to comment.