Skip to content

Commit

Permalink
feat(GlpiRestClient): create function to killSession
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianfranco97 authored and ajsb85 committed Jan 16, 2018
1 parent d4df7c7 commit f18f282
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
15 changes: 10 additions & 5 deletions src/prepareRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ function prepareRequest (data) {
if (config.appToken) {
myHeaders.append('App-Token', config.appToken)
}

if (config.sessionToken) {
myHeaders.append('Authorization', `user_token ${config.sessionToken}`)
}


switch (data.function) {
case 'initSessionByCredentials':
myHeaders.append('Authorization', `Basic ${Buffer.from(`${data.userName}:${data.userPassword}`).toString('base64')}`)
Expand All @@ -26,10 +22,19 @@ function prepareRequest (data) {
url = `${url}/initSession?user_token=${data.userToken}`
myInit = { method: 'GET' }
break
case 'killSession':
myHeaders.append('Accept', 'text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8')
url = `${url}/killSession`
myInit = { method: 'GET' }
break
default:
break
}

if (config.sessionToken) {
url = `${url}?session_token=${config.sessionToken}`
}

myInit = {
...myInit,
headers: myHeaders
Expand Down
71 changes: 56 additions & 15 deletions src/restclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,30 @@ class GlpiRestClient {
config.appToken = appToken
}

_makeRequest (myRequest, responseHandler) {
_makeRequest (myRequest, funct, responseHandler) {
fetch (myRequest)
.then((resp) => {
if (resp.headers.get('Content-Type').indexOf("application/json") >= 0) {
responseHandler(resp.json())
} else {
responseHandler(resp.text())

switch (funct) {

case 'initSessionByCredentials':
case 'initSessionByUserToken':
responseHandler(resp.json())
break

case 'killSession':
if (resp.ok) {
responseHandler(resp.text())
} else {
responseHandler(resp.json())
}
break

default:
responseHandler(resp.text())
break
}

})
.catch((err) => {
responseHandler(err)
Expand All @@ -54,11 +70,15 @@ class GlpiRestClient {
userName,
userPassword
}
this._makeRequest( prepareRequest(data), (response) => {
if (response.session_token) {
config.sessionToken = response.session_token
}
resolve ( response )
this._makeRequest( prepareRequest(data), 'initSessionByCredentials', (promise) => {
promise.then(response => {
console.log('response')
console.log(response)
if (response.session_token) {
config.sessionToken = response.session_token
}
resolve ( response )
})
})
}
catch (err) {
Expand All @@ -74,11 +94,32 @@ class GlpiRestClient {
function: 'initSessionByUserToken',
userToken
}
this._makeRequest( prepareRequest(data), (response) => {
if (response.session_token) {
config.sessionToken = response.session_token
}
resolve ( response )
this._makeRequest( prepareRequest(data), 'initSessionByUserToken', (promise) => {
promise.then(response => {
if (response.session_token) {
config.sessionToken = response.session_token
}
resolve ( response )
})
})
}
catch (err) {
reject(err)
}
})
}

killSession () {
return new Promise((resolve, reject) => {
try {
const data = {
function: 'killSession'
}
this._makeRequest( prepareRequest(data), 'killSession', (promise) => {
promise.then(response => {
config.sessionToken = ''
resolve ( response )
})
})
}
catch (err) {
Expand Down

0 comments on commit f18f282

Please sign in to comment.