WIP: Criado conexão com API do GLPI
This commit is contained in:
parent
1bf3f744ab
commit
30be46bbd3
54
src/infra/api/glpi.auth.js
Normal file
54
src/infra/api/glpi.auth.js
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
// src/infra/api/glpi.auth.js
|
||||||
|
|
||||||
|
const axios = require('axios')
|
||||||
|
const qs = require('qs')
|
||||||
|
const { glpi } = require('./glpi.config')
|
||||||
|
const { logError } = require('../../shared/utils/logger')
|
||||||
|
|
||||||
|
let cachedToken = null
|
||||||
|
let tokenExpiresAt = null
|
||||||
|
|
||||||
|
const getAuthToken = async () => {
|
||||||
|
try {
|
||||||
|
// reaproveita token válido
|
||||||
|
if (cachedToken && tokenExpiresAt && Date.now() < tokenExpiresAt) {
|
||||||
|
return cachedToken
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
grant_type: 'password',
|
||||||
|
client_id: glpi.clientId,
|
||||||
|
client_secret: glpi.clientSecret,
|
||||||
|
username: glpi.username,
|
||||||
|
password: glpi.password,
|
||||||
|
scope: 'api'
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await axios.post(
|
||||||
|
glpi.authUrl,
|
||||||
|
qs.stringify(payload),
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
cachedToken = response.data.access_token
|
||||||
|
|
||||||
|
const expiresIn = response.data.expires_in || 3600
|
||||||
|
tokenExpiresAt = Date.now() + (expiresIn - 60) * 1000
|
||||||
|
|
||||||
|
return cachedToken
|
||||||
|
} catch (error) {
|
||||||
|
logError('Erro ao obter token de autenticação GLPI', {
|
||||||
|
message: error.message,
|
||||||
|
response: error.response?.data
|
||||||
|
})
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getAuthToken
|
||||||
|
}
|
||||||
21
src/infra/api/glpi.client.js
Normal file
21
src/infra/api/glpi.client.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// src/shared/infra/api/glpi.client.js
|
||||||
|
|
||||||
|
const { getAuthToken } = require('./glpi.auth')
|
||||||
|
|
||||||
|
async function glpiRequest({ method, url, data, params }) {
|
||||||
|
const token = await getAuthToken()
|
||||||
|
|
||||||
|
return axios({
|
||||||
|
method,
|
||||||
|
url: `${glpi.baseUrl}${url}`,
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
Accept: 'application/json'
|
||||||
|
},
|
||||||
|
data,
|
||||||
|
params
|
||||||
|
})
|
||||||
|
}
|
||||||
|
module.exports = {
|
||||||
|
glpiRequest
|
||||||
|
}
|
||||||
17
src/infra/api/glpi.config.js
Normal file
17
src/infra/api/glpi.config.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// src/shared/infra/api/glpi.config.js
|
||||||
|
module.exports = {
|
||||||
|
glpi: {
|
||||||
|
baseUrl: process.env.GLPI_BASE_URL,
|
||||||
|
|
||||||
|
authUrl: process.env.GLPI_AUTH_URL,
|
||||||
|
|
||||||
|
authPayload: {
|
||||||
|
grant_type: 'password',
|
||||||
|
client_id: process.env.GLPI_CLIENT_ID,
|
||||||
|
client_secret: process.env.GLPI_CLIENT_SECRET,
|
||||||
|
username: process.env.GLPI_USERNAME,
|
||||||
|
password: process.env.GLPI_PASSWORD,
|
||||||
|
scope: 'api'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -1,18 +1,4 @@
|
|||||||
// src/shared/infra/api/hubsoft.config.js
|
// src/shared/infra/api/hubsoft.config.js
|
||||||
|
|
||||||
var hubsoft = {
|
|
||||||
baseUrl: process.env.HUBSOFT_BASE_URL,
|
|
||||||
authUrl: process.env.HUBSOFT_AUTH_URL,
|
|
||||||
authPayload: {
|
|
||||||
grant_type: 'password',
|
|
||||||
client_id: process.env.HUBSOFT_CLIENT_ID,
|
|
||||||
client_secret: process.env.HUBSOFT_CLIENT_SECRET,
|
|
||||||
username: process.env.HUBSOFT_USERNAME,
|
|
||||||
password: process.env.HUBSOFT_PASSWORD
|
|
||||||
},
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
hubsoft: {
|
hubsoft: {
|
||||||
baseUrl: process.env.HUBSOFT_BASE_URL,
|
baseUrl: process.env.HUBSOFT_BASE_URL,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user