-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenterDeepLAPIkey.js
More file actions
168 lines (156 loc) · 5.29 KB
/
enterDeepLAPIkey.js
File metadata and controls
168 lines (156 loc) · 5.29 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// translateSelectionAndAppendL use the function
function enterDeepLAPIkey(storage, translator) {
const translatorName = PROPERTY_NAMES[translator]['textName'];
const propertyName = PROPERTY_NAMES[translator]['propertyApiKeyName'];
let properties;
const storageDesc = storage == 'user' ? 'user / all documents' : 'document';
let apiKey = getValueFromUser(translatorName + ' API key', 'Please enter the ' + translatorName + ' API key for ' + storageDesc);
if (apiKey == null) {
return { status: 'error' };
}
apiKey = apiKey.trim();
if (apiKey != '') {
const result = PROPERTY_NAMES[translatorName].testKey(apiKey);
if (result.status == 'ok') {
if (storage == 'user') {
properties = PropertiesService.getUserProperties();
} else {
properties = PropertiesService.getDocumentProperties();
}
properties.setProperty(propertyName, apiKey);
onOpen();
return { status: 'ok', apiKey: apiKey };
} else {
alert(result.message);
}
}
return { status: 'error' };
}
// 2024 sidebar
function enterAPIkey(provider, storage, apiKey) {
const propertyName = PROPERTY_NAMES[provider]['propertyApiKeyName'];
let properties;
apiKey = apiKey.trim();
if (apiKey != '') {
const testResult = PROPERTY_NAMES[provider]['testKey'](apiKey);
if (testResult.status === 'ok') {
if (storage == 'user') {
properties = PropertiesService.getUserProperties();
} else {
properties = PropertiesService.getDocumentProperties();
}
properties.setProperty(propertyName, apiKey);
return { status: 'ok' };
} else {
return { status: 'error', message: `The ${provider} API key doesn't work. ` + testResult.message };
}
}
return { status: 'error', message: `Something went wrongt. The ${provider} API key wasn't saved.` };
}
// Checks DeepL API key
// Requests language sources, checks response code
function testDeeplKey(apiKey) {
try {
const url = 'https://api.deepl.com/v2/languages?auth_key=' + apiKey + '&type=source';
const response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
const responseCode = response.getResponseCode();
if (responseCode == 200) {
return { status: 'ok' };
} else if (responseCode == 403) {
return { status: 'wrong', message: 'Wrong API key' };
} else {
return { status: 'error', message: 'Error in testDeeplKey (1)' };
}
}
catch (error) {
return { status: 'error', message: 'Error in testDeeplKey (2)' };
}
}
// Checks ChatGPT API key
// Requests models
function testChatGPTKey(apiKey) {
try {
const url = 'https://api.openai.com/v1/models';
const options = {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + apiKey
},
muteHttpExceptions: true
};
const response = UrlFetchApp.fetch(url, options);
const json = JSON.parse(response.getContentText());
//Logger.log(json);
const responseCode = response.getResponseCode();
if (responseCode == 200) {
return { status: 'ok' };
} else {
const message = json.error?.message ? json.error.message : '';
if (json.error?.code === 'invalid_api_key') {
return { status: 'wrong', message: 'Wrong API key. ' + message };
} else {
return { status: 'error', message: 'Error in testChatGPTKey (1). ' + message };
}
}
}
catch (error) {
return { status: 'error', message: 'Error in testChatGPTKey (2)' + error };
}
}
function testAnthropicKey(apiKey) {
try {
const url = 'https://api.anthropic.com/v1/messages';
const headers = {
'content-type': 'application/json',
'x-api-key': apiKey,
'anthropic-version': '2023-06-01'
};
const payload = {
"model": "claude-3-haiku-20240307",
"max_tokens": 1,
"temperature": 0,
"messages": [
{ "role": "user", "content": "Do you speak English? Return Y or N." }
]
};
const options = {
method: 'post',
headers: headers,
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
const response = UrlFetchApp.fetch(url, options);
const data = JSON.parse(response.getContentText());
if (data.error) {
throw new Error(`API error: ${data.error.message}`);
}
return { status: 'ok' };
}
catch (error) {
return { status: 'error', message: 'Error in testAnthropicKey ' + error };
}
}
// translateSelectionAndAppendL uses the function
// propertyName: 'DeepLAPIkey' or 'ChatGPTAPIkey'
function getDeepLAPIkey(storage, propertyName) {
let properties;
if (storage == 'user') {
properties = PropertiesService.getUserProperties();
} else {
properties = PropertiesService.getDocumentProperties();
}
const key = properties.getProperty(propertyName);
return key;
}
// 2024
function copyUserApiKeyToDocStorage(translator) {
const translatorName = PROPERTY_NAMES[translator]['textName'];
const propertyName = PROPERTY_NAMES[translator]['propertyApiKeyName'];
let deeplApiKeyUser = getDeepLAPIkey('user', propertyName);
if (deeplApiKeyUser == null) {
return { status: 'error', message: "Error! " + translatorName + " API key for user/all documents doesn't exist. Add it." };
}
properties = PropertiesService.getDocumentProperties();
properties.setProperty(propertyName, deeplApiKeyUser);
return { status: 'ok' };
}