Skip to content

Commit 4b9e903

Browse files
DaleHerringDale Herring
andauthored
DEV-170 organization api calls (#21)
* DEV-170 organization api calls * DEV-170 address pr comments * DEV-170 lint fixes * DEV-170 - fix Co-authored-by: Dale Herring <dale.herring@ruralsourcing.com>
1 parent aa3dc53 commit 4b9e903

4 files changed

Lines changed: 132 additions & 71 deletions

File tree

src/Algorithm.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,20 @@ class Build {
225225
}
226226
}
227227

228+
interface Organization {
229+
org_contact_name: string;
230+
org_email: string;
231+
org_label: string;
232+
org_name: string;
233+
resource_type: string;
234+
235+
org_url?: string;
236+
external_id?: string;
237+
external_admin_group_id?: string;
238+
external_member_group_id?: string;
239+
self_link?: string;
240+
}
241+
228242
interface AlgorithmVersionsList {
229243
marker: string;
230244
next_link: string;
@@ -260,4 +274,5 @@ export {
260274
AlgorithmVersionsList,
261275
AlgorithmBuildsList,
262276
AlgorithmSCMAuthorizationStatus,
277+
Organization,
263278
};

src/AlgorithmiaClient.ts

Lines changed: 66 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { HttpClient } from './HttpClient';
22
import { AlgorithmExecutable } from './AlgorithmExecutable';
33
import type { Input } from './ContentTypeHelper';
44
import { DataFile, DataDir } from './Data';
5+
import { URLSearchParams } from 'url';
56

67
class AlgorithmiaClient {
78
private defaultApiAddress = 'https://api.algorithmia.com';
89
private algoPrefix = '/v1/algo';
910
private algorithmsPrefix = '/v1/algorithms';
1011
private dataPrefix = '/v1/data';
1112
private scmPrefix = '/v1/scms';
13+
private organizationPrefix = '/v1/organizations';
1214
private key: string;
1315
private apiAddress: string;
1416
private httpClient: HttpClient;
@@ -73,41 +75,20 @@ class AlgorithmiaClient {
7375
published = true,
7476
marker?: string
7577
) {
76-
if (marker == undefined) {
77-
return this.httpClient.get(
78-
this.apiAddress +
79-
this.algorithmsPrefix +
80-
'/' +
81-
userName +
82-
'/' +
83-
algoName +
84-
'/versions' +
85-
'?callable=' +
86-
callable +
87-
'&limit=' +
88-
limit +
89-
'&published=' +
90-
published
91-
);
92-
} else {
93-
return this.httpClient.get(
94-
this.apiAddress +
95-
this.algorithmsPrefix +
96-
'/' +
97-
userName +
98-
'/' +
99-
algoName +
100-
'/versions' +
101-
'?callable=' +
102-
callable +
103-
'&limit=' +
104-
limit +
105-
'&published=' +
106-
published +
107-
'&marker=' +
108-
marker
109-
);
78+
const path = `${this.algorithmsPrefix}/${userName}/${algoName}/versions`;
79+
const params = new URLSearchParams({
80+
callable: callable.toString(),
81+
limit: limit.toString(),
82+
published: published.toString(),
83+
});
84+
85+
if (marker) {
86+
params.set('marker', marker);
11087
}
88+
89+
const search = `?${params.toString()}`;
90+
91+
return this.httpClient.get(`${this.apiAddress}${path}${search}`);
11192
}
11293

11394
/**
@@ -124,33 +105,18 @@ class AlgorithmiaClient {
124105
limit = 50,
125106
marker?: string
126107
) {
127-
if (marker == undefined) {
128-
return this.httpClient.get(
129-
this.apiAddress +
130-
this.algorithmsPrefix +
131-
'/' +
132-
userName +
133-
'/' +
134-
algoName +
135-
'/builds' +
136-
'?limit=' +
137-
limit
138-
);
139-
} else {
140-
return this.httpClient.get(
141-
this.apiAddress +
142-
this.algorithmsPrefix +
143-
'/' +
144-
userName +
145-
'/' +
146-
algoName +
147-
'/builds' +
148-
'?limit=' +
149-
limit +
150-
'&marker=' +
151-
marker
152-
);
108+
const path = `${this.algorithmsPrefix}/${userName}/${algoName}/builds`;
109+
const params = new URLSearchParams({
110+
limit: limit.toString(),
111+
});
112+
113+
if (marker) {
114+
params.set('marker', marker);
153115
}
116+
117+
const search = `?${params.toString()}`;
118+
119+
return this.httpClient.get(`${this.apiAddress}${path}${search}`);
154120
}
155121

156122
/**
@@ -161,17 +127,8 @@ class AlgorithmiaClient {
161127
* @return a BuildLogs object for the specified algorithm
162128
*/
163129
getAlgoBuildLogs(userName: string, algoName: string, buildId: string) {
164-
return this.httpClient.get(
165-
this.apiAddress +
166-
this.algorithmsPrefix +
167-
'/' +
168-
userName +
169-
'/' +
170-
algoName +
171-
'/builds/' +
172-
buildId +
173-
'/logs'
174-
);
130+
const path = `${this.algorithmsPrefix}/${userName}/${algoName}/builds/${buildId}/logs`;
131+
return this.httpClient.get(`${this.apiAddress}${path}`);
175132
}
176133

177134
/**
@@ -238,6 +195,44 @@ class AlgorithmiaClient {
238195
return this.httpClient.post(this.apiAddress + this.scmPrefix + '/' + scmId + '/oauth/revoke', {});
239196
}*/
240197

198+
/**
199+
* Create an organization from this client
200+
* @param requestObject object payload
201+
* @return an organization object
202+
*/
203+
createOrganization(requestObject: Input) {
204+
const contentType = 'application/json';
205+
return this.httpClient.post(
206+
this.apiAddress + this.organizationPrefix,
207+
requestObject,
208+
contentType
209+
);
210+
}
211+
212+
/**
213+
* Get an organization from this client
214+
* @param orgName the organization name
215+
* @return an organization object
216+
*/
217+
getOrganization(orgName: string) {
218+
return this.httpClient.get(
219+
this.apiAddress + this.organizationPrefix + '/' + orgName
220+
);
221+
}
222+
223+
/**
224+
* Edit an organization from this client
225+
* @param orgName the organization name
226+
* @param requestObject payload
227+
* @return an empty response
228+
*/
229+
editOrganization(orgName: string, requestObject: Input) {
230+
return this.httpClient.put(
231+
this.apiAddress + this.organizationPrefix + '/' + orgName,
232+
JSON.stringify(requestObject)
233+
);
234+
}
235+
241236
/**
242237
* Initialize an DataFile object from this client
243238
* @param path to a data file, e.g., data://.my/foo/bar.txt

src/HttpClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class HttpClient {
5454
}
5555

5656
async put(path: string, data: string) {
57+
this.headers['Content-Type'] = 'application/json';
5758
const response = await this.httpClient.put(path, data, this.headers);
5859

5960
return response.readBody();

test/AlgorithmiaClient.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
AlgorithmVersionsList,
66
AlgorithmBuildsList,
77
AlgorithmSCMAuthorizationStatus,
8+
Organization,
89
} from '../src/Algorithm';
910

1011
describe('Localisation initialization', () => {
@@ -139,4 +140,53 @@ describe('Localisation initialization', () => {
139140
expect(response).toBe('');
140141
});
141142
});*/
143+
144+
describe('organization get organization', () => {
145+
it('gets an organization', async () => {
146+
const organization: Organization = JSON.parse(
147+
await Algorithmia.getClient(
148+
process.env.ALGORITHMIA_ADMIN_API_KEY,
149+
process.env.ALGORITHMIA_TEST_ADDRESS
150+
).getOrganization('a_myOrg15')
151+
);
152+
153+
expect(organization.org_email).toBe('a_myOrg15@algo.com');
154+
});
155+
});
156+
157+
describe('organization edit call', () => {
158+
it('edits for organization', async () => {
159+
const response = await Algorithmia.getClient(
160+
process.env.ALGORITHMIA_ADMIN_API_KEY,
161+
process.env.ALGORITHMIA_TEST_ADDRESS
162+
).editOrganization('MyOrg1606332498213', {
163+
org_contact_name: 'some owner',
164+
org_email: 'SomeEmail@Whatsittoyou.com',
165+
org_label: 'myLabel',
166+
type_id: '3d40e3b0-d82a-11ea-9a3c-0ee5e2d35097',
167+
resource_type: 'organization',
168+
id: '3d9a9f41-d82a-11ea-9a3c-0ee5e2d35097',
169+
});
170+
expect(response).toBe('');
171+
});
172+
});
173+
174+
describe('organization create call', () => {
175+
it('creates for organization', async () => {
176+
const testOrganization = {
177+
org_contact_name: 'some owner',
178+
org_email: 'SomeEmail@Whatsittoyou.com',
179+
org_label: 'myLabel',
180+
org_name: 'MyOrg' + Date.now(),
181+
};
182+
183+
const organization: Organization = JSON.parse(
184+
await Algorithmia.getClient(
185+
process.env.ALGORITHMIA_ADMIN_API_KEY,
186+
process.env.ALGORITHMIA_TEST_ADDRESS
187+
).createOrganization(testOrganization)
188+
);
189+
expect(organization.org_name).toBe(testOrganization.org_name);
190+
});
191+
});
142192
});

0 commit comments

Comments
 (0)