@@ -3,14 +3,17 @@ import { AlgorithmExecutable } from './AlgorithmExecutable';
33import type { Input } from './ContentTypeHelper' ;
44import { DataFile , DataDir } from './Data' ;
55import { URLSearchParams } from 'url' ;
6+ import { Organization , OrgType , OrgTypes } from './Algorithm' ;
67
78class AlgorithmiaClient {
89 private defaultApiAddress = 'https://api.algorithmia.com' ;
910 private algoPrefix = '/v1/algo' ;
1011 private algorithmsPrefix = '/v1/algorithms' ;
1112 private dataPrefix = '/v1/data' ;
1213 private scmPrefix = '/v1/scms' ;
13- private organizationPrefix = '/v1/organizations' ;
14+ private organizationTypePrefix = '/v1/organization' ;
15+ private organizationsPrefix = '/v1/organizations' ;
16+ private typesMapList : OrgTypes [ ] = [ ] ;
1417 private key : string ;
1518 private apiAddress : string ;
1619 private httpClient : HttpClient ;
@@ -200,24 +203,25 @@ class AlgorithmiaClient {
200203 * @param requestObject object payload
201204 * @return an organization object
202205 */
203- createOrganization ( requestObject : Input ) {
206+ async createOrganization ( requestObject : Input , type : OrgType ) {
204207 const contentType = 'application/json' ;
205208 return this . httpClient . post (
206- this . apiAddress + this . organizationPrefix ,
207- requestObject ,
209+ ` ${ this . apiAddress } ${ this . organizationsPrefix } ` ,
210+ JSON . stringify ( await this . organizationTypeIdChanger ( requestObject , type ) ) ,
208211 contentType
209- ) ;
212+ ) ;
210213 }
211214
212215 /**
213216 * Get an organization from this client
214217 * @param orgName the organization name
215218 * @return an organization object
216219 */
217- getOrganization ( orgName : string ) {
218- return this . httpClient . get (
219- this . apiAddress + this . organizationPrefix + '/' + orgName
220- ) ;
220+ async getOrganization ( orgName : string ) : Promise < Organization > {
221+ const organization : Organization = JSON . parse ( await this . httpClient . get (
222+ `${ this . apiAddress } ${ this . organizationsPrefix } /${ orgName } `
223+ ) ) ;
224+ return organization ;
221225 }
222226
223227 /**
@@ -228,11 +232,44 @@ class AlgorithmiaClient {
228232 */
229233 editOrganization ( orgName : string , requestObject : Input ) {
230234 return this . httpClient . put (
231- this . apiAddress + this . organizationPrefix + '/' + orgName ,
232- JSON . stringify ( requestObject )
235+ ` ${ this . apiAddress } ${ this . organizationsPrefix } / ${ orgName } ` ,
236+ requestObject
233237 ) ;
234238 }
235239
240+ clone ( obj : Input ) {
241+ return JSON . parse ( JSON . stringify ( obj ) ) ;
242+ }
243+
244+ /**
245+ * Helper for swapping out the type_id value
246+ */
247+ async organizationTypeIdChanger ( requestObject : Input , type : OrgType ) {
248+ const editedOrganization : Organization = this . clone ( requestObject ) ;
249+ let isSet = false ;
250+ if ( ! this . typesMapList . length ) {
251+ this . typesMapList = await this . getOrgTypes ( ) ;
252+ }
253+ for ( var typesMapObject of this . typesMapList ) {
254+ if ( type === typesMapObject . name ) {
255+ editedOrganization . type_id = typesMapObject . id ;
256+ isSet = true ;
257+ break ;
258+ } ;
259+ }
260+ if ( ! isSet ) {
261+ throw new Error ( "No matching organization type found, should be one of 'legacy', 'basic', 'pro'" ) ;
262+ }
263+ return editedOrganization ;
264+ }
265+
266+ /**
267+ * Get types uuid endpoint
268+ */
269+ async getOrgTypes ( ) {
270+ return JSON . parse ( await this . httpClient . get ( `${ this . apiAddress } ${ this . organizationTypePrefix } /types` ) ) ;
271+ }
272+
236273 /**
237274 * Initialize an DataFile object from this client
238275 * @param path to a data file, e.g., data://.my/foo/bar.txt
0 commit comments