11import { axios } from "@pipedream/platform" ;
2+ import constants from "./common/constants.mjs" ;
23
34export default {
45 type : "app" ,
@@ -8,14 +9,30 @@ export default {
89 type : "string" ,
910 label : "Job" ,
1011 description : "Identifier of a job" ,
11- async options ( ) {
12- const { jobs } = await this . listJobs ( ) ;
13- return jobs ?. map ( ( {
12+ async options ( { prevContext } ) {
13+ if ( prevContext . pageToken === null ) {
14+ return [ ] ;
15+ }
16+ const {
17+ jobs, next_page_token : pageToken ,
18+ } = await this . listJobs ( {
19+ params : {
20+ page_token : prevContext . pageToken ,
21+ limit : constants . DEFAULT_LIMIT ,
22+ } ,
23+ } ) ;
24+ const options = jobs ?. map ( ( {
1425 job_id : value , settings,
1526 } ) => ( {
1627 value,
17- label : settings . name ,
28+ label : settings ? .name || value ,
1829 } ) ) || [ ] ;
30+ return {
31+ options,
32+ context : {
33+ pageToken : pageToken || null ,
34+ } ,
35+ } ;
1936 } ,
2037 } ,
2138 runId : {
@@ -102,51 +119,155 @@ export default {
102119 } ,
103120 } ,
104121 methods : {
105-
106- _baseUrl ( ) {
107- return `https:// ${ this . $auth . domain } .cloud.databricks.com/api/2.0 ` ;
122+ getUrl ( path , versionPath = constants . VERSION_PATH . V2_0 ) {
123+ const baseUrl = constants . BASE_URL . replace ( constants . DOMAIN_PLACEHOLDER , this . $auth . domain ) ;
124+ return `${ baseUrl } ${ versionPath } ${ path } ` ;
108125 } ,
109126 _headers ( ) {
110127 return {
111128 Authorization : `Bearer ${ this . $auth . access_token } ` ,
112129 } ;
113130 } ,
114131 _makeRequest ( {
115- $ = this ,
116- path,
117- ...args
118- } ) {
132+ $ = this , path, versionPath, ...args
133+ } = { } ) {
119134 return axios ( $ , {
120- url : ` ${ this . _baseUrl ( ) } ${ path } ` ,
135+ url : this . getUrl ( path , versionPath ) ,
121136 headers : this . _headers ( ) ,
122137 paramsSerializer : {
123138 indexes : null ,
124139 } ,
125140 ...args ,
126141 } ) ;
127142 } ,
143+ createJob ( args = { } ) {
144+ return this . _makeRequest ( {
145+ path : "/jobs/create" ,
146+ method : "POST" ,
147+ versionPath : constants . VERSION_PATH . V2_2 ,
148+ ...args ,
149+ } ) ;
150+ } ,
128151 listJobs ( args = { } ) {
129152 return this . _makeRequest ( {
130153 path : "/jobs/list" ,
154+ versionPath : constants . VERSION_PATH . V2_2 ,
155+ ...args ,
156+ } ) ;
157+ } ,
158+ getJob ( args = { } ) {
159+ return this . _makeRequest ( {
160+ path : "/jobs/get" ,
161+ versionPath : constants . VERSION_PATH . V2_2 ,
162+ ...args ,
163+ } ) ;
164+ } ,
165+ resetJob ( args = { } ) {
166+ return this . _makeRequest ( {
167+ path : "/jobs/reset" ,
168+ method : "POST" ,
169+ versionPath : constants . VERSION_PATH . V2_2 ,
170+ ...args ,
171+ } ) ;
172+ } ,
173+ updateJob ( args = { } ) {
174+ return this . _makeRequest ( {
175+ path : "/jobs/update" ,
176+ method : "POST" ,
177+ versionPath : constants . VERSION_PATH . V2_2 ,
178+ ...args ,
179+ } ) ;
180+ } ,
181+ deleteJob ( args = { } ) {
182+ return this . _makeRequest ( {
183+ path : "/jobs/delete" ,
184+ method : "POST" ,
185+ versionPath : constants . VERSION_PATH . V2_2 ,
186+ ...args ,
187+ } ) ;
188+ } ,
189+ runJobNow ( args = { } ) {
190+ return this . _makeRequest ( {
191+ path : "/jobs/run-now" ,
192+ method : "POST" ,
193+ versionPath : constants . VERSION_PATH . V2_2 ,
194+ ...args ,
195+ } ) ;
196+ } ,
197+ getRun ( args = { } ) {
198+ return this . _makeRequest ( {
199+ path : "/jobs/runs/get" ,
200+ versionPath : constants . VERSION_PATH . V2_2 ,
131201 ...args ,
132202 } ) ;
133203 } ,
134204 listRuns ( args = { } ) {
135205 return this . _makeRequest ( {
136206 path : "/jobs/runs/list" ,
207+ versionPath : constants . VERSION_PATH . V2_2 ,
208+ ...args ,
209+ } ) ;
210+ } ,
211+ cancelRun ( args = { } ) {
212+ return this . _makeRequest ( {
213+ path : "/jobs/runs/cancel" ,
214+ method : "POST" ,
215+ versionPath : constants . VERSION_PATH . V2_2 ,
216+ ...args ,
217+ } ) ;
218+ } ,
219+ cancelAllRuns ( args = { } ) {
220+ return this . _makeRequest ( {
221+ path : "/jobs/runs/cancel-all" ,
222+ method : "POST" ,
223+ versionPath : constants . VERSION_PATH . V2_2 ,
137224 ...args ,
138225 } ) ;
139226 } ,
140227 getRunOutput ( args = { } ) {
141228 return this . _makeRequest ( {
142229 path : "/jobs/runs/get-output" ,
230+ versionPath : constants . VERSION_PATH . V2_2 ,
143231 ...args ,
144232 } ) ;
145233 } ,
146- runJobNow ( args = { } ) {
234+ deleteRun ( args = { } ) {
147235 return this . _makeRequest ( {
148- path : "/jobs/run-now " ,
236+ path : "/jobs/runs/delete " ,
149237 method : "POST" ,
238+ versionPath : constants . VERSION_PATH . V2_2 ,
239+ ...args ,
240+ } ) ;
241+ } ,
242+ repairRun ( args = { } ) {
243+ return this . _makeRequest ( {
244+ path : "/jobs/runs/repair" ,
245+ method : "POST" ,
246+ versionPath : constants . VERSION_PATH . V2_2 ,
247+ ...args ,
248+ } ) ;
249+ } ,
250+ exportRun ( args = { } ) {
251+ return this . _makeRequest ( {
252+ path : "/jobs/runs/export" ,
253+ versionPath : constants . VERSION_PATH . V2_2 ,
254+ ...args ,
255+ } ) ;
256+ } ,
257+ getJobPermissions ( {
258+ jobId, ...args
259+ } ) {
260+ return this . _makeRequest ( {
261+ path : `/permissions/jobs/${ jobId } ` ,
262+ ...args ,
263+ } ) ;
264+ } ,
265+ setJobPermissions ( {
266+ jobId, ...args
267+ } ) {
268+ return this . _makeRequest ( {
269+ path : `/permissions/jobs/${ jobId } ` ,
270+ method : "PUT" ,
150271 ...args ,
151272 } ) ;
152273 } ,
@@ -264,7 +385,6 @@ export default {
264385 ...args ,
265386 } ) ;
266387 } ,
267-
268388 setSQLWarehousePermissions ( {
269389 warehouseId, ...args
270390 } ) {
@@ -364,5 +484,45 @@ export default {
364484 ...args ,
365485 } ) ;
366486 } ,
487+ async paginate ( {
488+ requestor, requestorArgs = { } ,
489+ maxRequests = 3 , resultsKey = "jobs" ,
490+ } ) {
491+ const allResults = [ ] ;
492+ let requestCount = 0 ;
493+ let nextPageToken = null ;
494+ let hasMore = true ;
495+
496+ while ( hasMore && requestCount < maxRequests ) {
497+ try {
498+ const response = await requestor ( {
499+ ...requestorArgs ,
500+ params : {
501+ ...requestorArgs . params ,
502+ page_token : nextPageToken ,
503+ } ,
504+ } ) ;
505+
506+ requestCount ++ ;
507+
508+ const results = response [ resultsKey ] || [ ] ;
509+
510+ allResults . push ( ...results ) ;
511+
512+ nextPageToken = response . next_page_token ;
513+ hasMore = ! ! nextPageToken ;
514+
515+ if ( results . length === 0 ) {
516+ hasMore = false ;
517+ }
518+
519+ } catch ( error ) {
520+ console . error ( `Pagination error on request ${ requestCount } :` , error ) ;
521+ throw error ;
522+ }
523+ }
524+
525+ return allResults ;
526+ } ,
367527 } ,
368528} ;
0 commit comments