-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAlgorithmiaClient.ts
More file actions
354 lines (322 loc) · 10.2 KB
/
Copy pathAlgorithmiaClient.ts
File metadata and controls
354 lines (322 loc) · 10.2 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import { HttpClient } from './HttpClient';
import { AlgorithmExecutable } from './AlgorithmExecutable';
import type { Input } from './ContentTypeHelper';
import { DataFile, DataDir } from './Data';
import { URLSearchParams } from 'url';
import { Organization, OrgType, OrgTypes } from './Algorithm';
import dotenv from 'dotenv';
dotenv.config();
class AlgorithmiaClient {
private defaultApiAddress = 'https://api.algorithmia.com';
private algoPrefix = '/v1/algo';
private algorithmsPrefix = '/v1/algorithms';
private dataPrefix = '/v1/data';
private scmPrefix = '/v1/scms';
private organizationTypePrefix = '/v1/organization';
private organizationsPrefix = '/v1/organizations';
private typesMapList: OrgTypes[] = [];
private key: string;
private apiAddress: string;
private httpClient: HttpClient;
constructor(key?: string, apiAddress?: string) {
this.apiAddress =
apiAddress ||
process.env.ALGORITHMIA_API_ADDRESS ||
this.defaultApiAddress;
this.key = key || process.env.ALGORITHMIA_DEFAULT_API_KEY || '';
if (key) {
if (key.startsWith('Simple ')) {
this.key = key;
} else {
this.key = 'Simple ' + key;
}
}
this.httpClient = new HttpClient(this.key);
}
/**
* Initialize an Algorithm object from this client
* @param algoUri the algorithm's URI, e.g., algo://user/algoname
* @return an Algorithm client for the specified algorithm
*/
algo(algoUri: string): AlgorithmExecutable {
return new AlgorithmExecutable(
this.httpClient,
this.apiAddress + this.algoPrefix + '/' + algoUri
);
}
/**
* Get an Algorithm object from this client
* @param userName the users algorithmia user name
* @param algoName the name of the algorithm
* @return an Algorithm object for the specified algorithm
*/
getAlgo(userName: string, algoName: string) {
return this.httpClient.get(
this.apiAddress + this.algorithmsPrefix + '/' + userName + '/' + algoName
);
}
/**
* Get an Algorithm build object from this client
* @param userName the algorithmia user name
* @param algoName the name of the algorithm
* @return an Algorithm build object for the specified algorithm
*/
buildAlgo(userName: string, algoName: string) {
return this.httpClient.post(
`${this.apiAddress}${this.algorithmsPrefix}/${userName}/${algoName}/compile`,
{},
'application/json'
);
}
/**
* Get an Algorithm published object from this client
* @param userName the algorithmia user name
* @param algoName the name of the algorithm
* @return an Algorithm published object for the specified algorithm
*/
publishAlgo(userName: string, algoName: string) {
return this.httpClient.post(
`${this.apiAddress}${this.algorithmsPrefix}/${userName}/${algoName}/version`,
{
settings: {
algorithm_callability: 'private',
insights_enabled: false,
royalty_microcredits: 0,
},
version_info: { version_type: 'revision' },
},
'application/json'
);
}
/**
* List algorithm versions from this client
* @param userName the users Algorithmia user name
* @param algoName the name of the algorithm
* @param callable whether to return only public or private algorithm versions
* @param limit items per page
* @param published whether to return only versions that have been published
* @param marker used for pagination
* @return an AlgorithmVersionsList object for the specified algorithm
*/
listAlgoVersions(
userName: string,
algoName: string,
callable = true,
limit = 50,
published = true,
marker?: string
) {
const path = `${this.algorithmsPrefix}/${userName}/${algoName}/versions`;
const params = new URLSearchParams({
callable: callable.toString(),
limit: limit.toString(),
published: published.toString(),
});
if (marker) {
params.set('marker', marker);
}
const search = `?${params.toString()}`;
return this.httpClient.get(`${this.apiAddress}${path}${search}`);
}
/**
* List algorithm builds from this client
* @param userName the users algorithmia user name
* @param algoName the name of the algorithm
* @param limit items per page
* @param marker used for pagination
* @return an AlgorithmBuildsList object for the specified algorithm
*/
listAlgoBuilds(
userName: string,
algoName: string,
limit = 50,
marker?: string
) {
const path = `${this.algorithmsPrefix}/${userName}/${algoName}/builds`;
const params = new URLSearchParams({
limit: limit.toString(),
});
if (marker) {
params.set('marker', marker);
}
const search = `?${params.toString()}`;
return this.httpClient.get(`${this.apiAddress}${path}${search}`);
}
/**
* Get build logs for an Algorithm object from this client
* @param userName the users Algorithmia user name
* @param algoName the name of the algorithm
* @param buildId id of the build to retrieve logs
* @return a BuildLogs object for the specified algorithm
*/
getAlgoBuildLogs(userName: string, algoName: string, buildId: string) {
return this.httpClient.get(
`${this.apiAddress}${this.algorithmsPrefix}/${userName}/${algoName}/builds/${buildId}/logs`
);
}
/**
* Delete an Algorithm from this client
* @param userName the users algorithmia user name
* @param algoName the name of the algorithm
* @return an empty response
*/
deleteAlgo(userName: string, algoName: string) {
return this.httpClient.delete(
this.apiAddress + this.algorithmsPrefix + '/' + userName + '/' + algoName
);
}
/**
* Create a new Algorithm object from this client
* @param userName the users algorithmia user name
* @param requestObject object payload
* @return an Algorithm object for the specified algorithm
*/
createAlgo(userName: string, requestObject: Input) {
const contentType = 'application/json';
return this.httpClient.post(
this.apiAddress + this.algorithmsPrefix + '/' + userName,
requestObject,
contentType
);
}
/**
* List Algorithm SCMs from this client
* @return an Algorithm SCM object
*/
listSCMs() {
return this.httpClient.get(this.apiAddress + this.scmPrefix);
}
/**
* Get am Algorithm SCM object from this client
* @param scmId id of the scm to retrieve
* @return an Algorithm SCM object
*/
getSCM(scmId: string) {
return this.httpClient.get(this.apiAddress + this.scmPrefix + '/' + scmId);
}
/**
* Query an Algorithm SCM status from this client
* @param scmId id of the scm to retrieve
* @return an Algorithm SCM authorization object
*/
querySCMStatus(scmId: string) {
return this.httpClient.get(
this.apiAddress + this.scmPrefix + '/' + scmId + '/oauth/status'
);
}
/**
* Revoke an Algorithm SCM status from this client
* @param scmId id of the scm to retrieve
* @return an Algorithm SCM authorization object
*/
/*revokeSCMStatus(scmId: string) {
return this.httpClient.post(this.apiAddress + this.scmPrefix + '/' + scmId + '/oauth/revoke', {});
}*/
/**
* Create an organization from this client
* @param requestObject object payload
* @return an organization object
*/
async createOrganization(requestObject: Input, type: OrgType) {
const contentType = 'application/json';
return this.httpClient.post(
`${this.apiAddress}${this.organizationsPrefix}`,
JSON.stringify(await this.organizationTypeIdChanger(requestObject, type)),
contentType
);
}
/**
* Get an organization from this client
* @param orgName the organization name
* @return an organization object
*/
async getOrganization(orgName: string): Promise<Organization> {
const organization: Organization = JSON.parse(
await this.httpClient.get(
`${this.apiAddress}${this.organizationsPrefix}/${orgName}`
)
);
return organization;
}
/**
* Edit an organization from this client
* @param orgName the organization name
* @param requestObject payload
* @return an empty response
*/
editOrganization(orgName: string, requestObject: Input) {
return this.httpClient.put(
`${this.apiAddress}${this.organizationsPrefix}/${orgName}`,
requestObject
);
}
/**
* Delete an organization from this client
* @param orgName the organization name
* @return an empty response
*/
deleteOrganization(orgName: string) {
return this.httpClient.delete(
`${this.apiAddress}${this.organizationsPrefix}/${orgName}`
);
}
clone(obj: Input) {
return JSON.parse(JSON.stringify(obj));
}
/**
* Helper for swapping out the type_id value
*/
async organizationTypeIdChanger(requestObject: Input, type: OrgType) {
const editedOrganization: Organization = this.clone(requestObject);
let isSet = false;
if (!this.typesMapList.length) {
this.typesMapList = await this.getOrgTypes();
}
for (const typesMapObject of this.typesMapList) {
if (type === typesMapObject.name) {
editedOrganization.type_id = typesMapObject.id;
isSet = true;
break;
}
}
if (!isSet) {
throw new Error(
"No matching organization type found, should be one of 'legacy', 'basic', 'pro'"
);
}
return editedOrganization;
}
/**
* Get types uuid endpoint
*/
async getOrgTypes() {
return JSON.parse(
await this.httpClient.get(
`${this.apiAddress}${this.organizationTypePrefix}/types`
)
);
}
/**
* Initialize an DataFile object from this client
* @param path to a data file, e.g., data://.my/foo/bar.txt
* @return a DataFile client for the specified file
*/
file(path: string): DataFile {
return new DataFile(
this.httpClient,
this.apiAddress + this.dataPrefix + '/' + path
);
}
/**
* Initialize a DataDirectory object from this client
* @param path to a data directory, e.g., data://.my/foo
* @return a DataDirectory client for the specified directory
*/
dir(path: string): DataDir {
return new DataDir(
this.httpClient,
this.apiAddress + this.dataPrefix + '/' + path
);
}
}
export { AlgorithmiaClient };