-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GlpiRestClient): create basic restclient
- Loading branch information
1 parent
a16ee18
commit 75168e3
Showing
4 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import itemtype from './itemtype.json' | ||
import restclient from './restclient' | ||
|
||
export default restclient | ||
export { | ||
itemtype | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
|
||
function prepareRequest (data) { | ||
|
||
let myHeaders = new Headers() | ||
let myInit = { method: data.method } | ||
let url = `${data.url}/${data.endpoint}` | ||
|
||
myHeaders.append('Content-Type', 'application/json') | ||
|
||
if (data.appToken) { | ||
myHeaders.append('App-Token', data.appToken) | ||
} | ||
|
||
if (data.sessionToken) { | ||
myHeaders.append('Authorization', `user_token ${data.sessionToken}`) | ||
} | ||
|
||
switch (data.function) { | ||
case 'initSessionByCredentials': | ||
myHeaders.append('Authorization', `Basic ${Buffer.from(`${data.userName}:${data.userPassword}`).toString('base64')}`) | ||
break | ||
default: | ||
break | ||
} | ||
|
||
myInit = { | ||
...myInit, | ||
headers: myHeaders | ||
} | ||
|
||
return new Request(url, myInit) | ||
} | ||
|
||
export default prepareRequest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import prepareRequest from './prepareRequest' | ||
|
||
class GlpiRestClient { | ||
constructor (url, appToken = '') { | ||
this._url = url | ||
this._sessionToken = '' | ||
this._appToken = appToken | ||
} | ||
|
||
get url () { | ||
return this._url | ||
} | ||
|
||
set url (url) { | ||
this._url = url | ||
} | ||
|
||
get sessionToken () { | ||
return this._sessionToken | ||
} | ||
|
||
set sessionToken (sessionToken) { | ||
if (sessionToken) this._sessionToken = sessionToken | ||
} | ||
|
||
get appToken () { | ||
return this._appToken | ||
} | ||
|
||
set appToken (appToken) { | ||
if (appToken) this._appToken = appToken | ||
} | ||
|
||
_makeRequest (myRequest, responseHandler) { | ||
fetch (myRequest) | ||
.then((resp) => { | ||
if (resp.headers.get('Content-Type').indexOf("application/json") >= 0) { | ||
responseHandler(resp.json()) | ||
} else { | ||
responseHandler(resp.text()) | ||
} | ||
}) | ||
.catch((err) => { | ||
responseHandler(err) | ||
}) | ||
} | ||
|
||
} | ||
|
||
export default GlpiRestClient |