@@ -2,13 +2,15 @@ import { HttpClient } from './HttpClient';
22import { AlgorithmExecutable } from './AlgorithmExecutable' ;
33import type { Input } from './ContentTypeHelper' ;
44import { DataFile , DataDir } from './Data' ;
5+ import { URLSearchParams } from 'url' ;
56
67class 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
0 commit comments