Skip to content

Commit

Permalink
feat(GlpiRestClient): create function to initSessionByUserToken
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianfranco97 authored and Hector Rondon committed Jan 16, 2018
1 parent ab4b1eb commit 2224b85
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/prepareRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ function prepareRequest (data) {

switch (data.function) {
case 'initSessionByCredentials':
myHeaders.append('Authorization', `Basic ${Buffer.from(`${data.userName}:${data.userPassword}`).toString('base64')}`)
myHeaders.append('Authorization', `Basic ${Buffer.from(`${data.userName}:${data.userPassword}`).toString('base64')}`)
url = `${url}/initSession`
myInit = { method: 'GET' }
break
case 'initSessionByUserToken':
url = `${url}/initSession?user_token=${data.userToken}`
myInit = { method: 'GET' }
break
default:
break
}
Expand All @@ -30,7 +34,7 @@ function prepareRequest (data) {
...myInit,
headers: myHeaders
}

return new Request(url, myInit)
}

Expand Down
22 changes: 21 additions & 1 deletion src/restclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GlpiRestClient {

_makeRequest (myRequest, responseHandler) {
fetch (myRequest)
.then((resp) => {
.then((resp) => {
if (resp.headers.get('Content-Type').indexOf("application/json") >= 0) {
responseHandler(resp.json())
} else {
Expand Down Expand Up @@ -66,6 +66,26 @@ class GlpiRestClient {
}
})
}

initSessionByUserToken (userToken) {
return new Promise((resolve, reject) => {
try {
const data = {
function: 'initSessionByUserToken',
userToken
}
this._makeRequest( prepareRequest(data), (response) => {
if (response.session_token) {
config.sessionToken = response.session_token
}
resolve ( response )
})
}
catch (err) {
reject(err)
}
})
}
}

export default GlpiRestClient

0 comments on commit 2224b85

Please sign in to comment.