-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_new_keys.js
66 lines (57 loc) · 1.88 KB
/
test_new_keys.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
const metadata = new grpc.Metadata();
// Test multiple potential keys
const keys = [
process.env.read_api,
process.env.write_api,
process.env.clarifai_api,
process.env.new_clarifai_key
];
async function testKey(key) {
if (!key) {
console.log("Skipping undefined key");
return false;
}
metadata.set("authorization", `Key ${key}`);
return new Promise((resolve) => {
stub.ListApps(
{},
metadata,
(err, response) => {
if (err) {
console.log(`Key test failed: ${err.details}`);
resolve(false);
} else {
console.log("Key test successful!");
console.log("Apps found:", response.apps.length);
resolve(true);
}
}
);
});
}
async function main() {
console.log("Testing available keys...");
for (const key of keys) {
console.log("\nTesting key...");
const success = await testKey(key);
if (success) {
console.log("Found working key!");
// Update .env file with working key
const fs = require('fs');
const envPath = '/home/ubuntu/cognimates-training/.env';
let envContent = fs.readFileSync(envPath, 'utf8');
// Update or add CLARIFAI_PAT
if (envContent.includes('CLARIFAI_PAT=')) {
envContent = envContent.replace(/CLARIFAI_PAT=.*\n/, `CLARIFAI_PAT=${key}\n`);
} else {
envContent += `\nCLARIFAI_PAT=${key}`;
}
fs.writeFileSync(envPath, envContent);
console.log("Updated .env file with working key");
break;
}
}
}
main().catch(console.error);