@tmlmobilidade/external 20260608.1209.6 → 20260609.1117.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/clients/ml/auth.js +37 -11
- package/dist/clients/ml/index.js +35 -8
- package/package.json +1 -1
package/dist/clients/ml/auth.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* * */
|
|
2
2
|
import { Logger } from '@tmlmobilidade/logger';
|
|
3
3
|
import { asyncSingletonProxy } from '@tmlmobilidade/utils';
|
|
4
|
+
import https from 'node:https';
|
|
4
5
|
/* * */
|
|
5
6
|
export class MLAuthClient {
|
|
6
7
|
//
|
|
@@ -55,18 +56,43 @@ export class MLAuthClient {
|
|
|
55
56
|
password: process.env.ML_PASSWORD,
|
|
56
57
|
username: process.env.ML_USERNAME,
|
|
57
58
|
}).toString();
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
const data = await new Promise((resolve, reject) => {
|
|
60
|
+
//
|
|
61
|
+
const requestOptions = {
|
|
62
|
+
allowPartialTrustChain: true,
|
|
63
|
+
headers: {
|
|
64
|
+
'Authorization': `Basic ${Buffer.from(`${process.env.ML_CLIENT_ID}:${process.env.ML_CLIENT_SECRET}`).toString('base64')}`,
|
|
65
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
66
|
+
},
|
|
67
|
+
hostname: 'api.metrolisboa.pt',
|
|
68
|
+
method: 'POST',
|
|
69
|
+
path: '/token',
|
|
70
|
+
port: 8243,
|
|
71
|
+
rejectUnauthorized: false,
|
|
72
|
+
servername: 'api.metrolisboa.pt',
|
|
73
|
+
};
|
|
74
|
+
const callback = (response) => {
|
|
75
|
+
const chunks = [];
|
|
76
|
+
response.on('data', chunk => chunks.push(chunk));
|
|
77
|
+
response.on('end', () => {
|
|
78
|
+
const responseText = Buffer.concat(chunks).toString('utf8');
|
|
79
|
+
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
80
|
+
reject(new Error(`[MlClient] Request failed (${response.statusCode}): ${responseText.slice(0, 500)}`));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
resolve(JSON.parse(responseText));
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
reject(new Error(`[MlClient] Response is not JSON: ${responseText.slice(0, 500)}`));
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
const request = https.request(requestOptions, callback);
|
|
92
|
+
request.on('error', reject);
|
|
93
|
+
request.write(requestBody);
|
|
94
|
+
request.end();
|
|
65
95
|
});
|
|
66
|
-
if (!response.ok) {
|
|
67
|
-
throw new Error(`[MLAuthClient] Token request failed (${response.status}): ${response.statusText}`);
|
|
68
|
-
}
|
|
69
|
-
const data = await response.json();
|
|
70
96
|
//
|
|
71
97
|
// With the response data, set the token and calculate the expiration time
|
|
72
98
|
// based on the current time and the expires_in value from the response.
|
package/dist/clients/ml/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/* * */
|
|
2
|
+
import https from 'node:https';
|
|
2
3
|
import { mlAuthClient } from './auth.js';
|
|
3
4
|
import { DESTINATION_MAP } from './types.js';
|
|
4
5
|
/* * */
|
|
@@ -9,15 +10,41 @@ async function fetcher(endpoint) {
|
|
|
9
10
|
throw new Error('Missing ML_API_URL environment variable.');
|
|
10
11
|
}
|
|
11
12
|
const apiToken = await mlAuthClient.getToken();
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
return await new Promise((resolve, reject) => {
|
|
14
|
+
//
|
|
15
|
+
const requestOptions = {
|
|
16
|
+
allowPartialTrustChain: true,
|
|
17
|
+
headers: {
|
|
18
|
+
Authorization: `Bearer ${apiToken}`,
|
|
19
|
+
},
|
|
20
|
+
hostname: 'api.metrolisboa.pt',
|
|
21
|
+
method: 'GET',
|
|
22
|
+
path: `/estadoServicoML/1.0.1${endpoint}`,
|
|
23
|
+
port: 8243,
|
|
24
|
+
rejectUnauthorized: false,
|
|
25
|
+
servername: 'api.metrolisboa.pt',
|
|
26
|
+
};
|
|
27
|
+
const callback = (response) => {
|
|
28
|
+
const chunks = [];
|
|
29
|
+
response.on('data', chunk => chunks.push(chunk));
|
|
30
|
+
response.on('end', () => {
|
|
31
|
+
const responseText = Buffer.concat(chunks).toString('utf8');
|
|
32
|
+
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
33
|
+
reject(new Error(`[MlClient] Request failed (${response.statusCode}): ${responseText.slice(0, 500)}`));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
resolve(JSON.parse(responseText));
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
reject(new Error(`[MlClient] Response is not JSON: ${responseText.slice(0, 500)}`));
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
const request = https.request(requestOptions, callback);
|
|
45
|
+
request.on('error', reject);
|
|
46
|
+
request.end();
|
|
16
47
|
});
|
|
17
|
-
if (!response.ok) {
|
|
18
|
-
throw new Error(`Request failed (${response.status}): ${response.statusText}`);
|
|
19
|
-
}
|
|
20
|
-
return response.json();
|
|
21
48
|
}
|
|
22
49
|
/* * */
|
|
23
50
|
const endpoints = {
|