@@ -71,59 +71,66 @@ exports.all = async (contentServer, channelToken, limit, query, oAuthStr, previe
7171 } ;
7272
7373 const fetchAll = async ( ) => {
74- const fetchLimit = ( ( limit != null && limit > 0 ) ? limit : 10 ) ;
74+ // Fetch a response from the apiUrl
7575
76- const fetchQuery = query ? `&q=${ query } ` : '' ;
77- const allPublishedItemsUrl = `${ contentServer } /content/published/api/v1.1/items?limit=${ fetchLimit } &offset=0&totalResults=true&offset=0&channelToken=${ channelToken } ${ fetchQuery } ` ;
78- const allPreviewItemsUrl = `${ contentServer } /content/preview/api/v1.1/items?limit=${ fetchLimit } &offset=0&totalResults=true&offset=0&channelToken=${ channelToken } ${ fetchQuery } ` ;
79-
80- const allItemsUrl = isPreview === true ? allPreviewItemsUrl : allPublishedItemsUrl ;
81-
82- // Fetch a response from the apiUrl
76+ let itemsArray = [ ] ;
8377
78+ let fetchLimit = ( ( limit != null && limit > 0 ) ? limit : 10 ) ;
79+ let hasMore = true ;
8480 let response = null ;
81+ let scrollId = '' ; // Used for efficient paging when downloading a large query.
82+
8583 const headers = new Headers ( {
8684 Authorization : `${ oAuthStr } ` ,
8785 Accept : '*/*' ,
8886 Connection : 'keep-alive' ,
8987 'User-Agent' : 'oracle/gatsby-source-oce' ,
9088 } ) ;
91-
92- try {
93- response = await fetch ( allItemsUrl , { headers } ) ;
94- } catch ( e ) {
95- console . log ( `ERROR Failed downloading item list using ${ allItemsUrl } ` ) ;
89+ console . log ( 'Downloading channel asset list' ) ;
90+ while ( hasMore ) {
91+ // Used if a query was added for use with the REST API. The default returns all assets
92+ const fetchQuery = query ? `&q=${ query } ` : '&q=(name%20ne%20".*")' ;
93+
94+ // Build a URL based on whether published or preview data is desired
95+ const scrollIdStr = scrollId . length === 0 ? '' : `&scrollId=${ scrollId } ` ;
96+ const allItemsUrl = `${ contentServer } /content/${ isPreview === true ? 'preview' : 'published' } /api/v1.1/items?limit=${ fetchLimit } &scroll=true&orderBy=id:asc&channelToken=${ channelToken } ${ fetchQuery } ${ scrollIdStr } ` ;
97+
98+ try {
99+ console . log ( allItemsUrl ) ;
100+ response = await fetch ( allItemsUrl , { headers } ) ;
101+ const data = await response . json ( ) ;
102+ // The maximum number of assets the server will return based on configuration
103+ fetchLimit = fetchLimit > data . limit ? limit : fetchLimit ;
104+ if ( data . count > 0 ) {
105+ itemsArray = itemsArray . concat ( data . items ) ;
106+ // The scroll Id is used when making multiple calls to the server api.
107+ if ( scrollId . length === 0 ) {
108+ scrollId = encodeURIComponent ( data . scrollId ) ;
109+ }
110+ } else {
111+ hasMore = false ;
112+ console . log ( 'Finished Downloading channel asset list' ) ;
113+ }
114+ } catch ( e ) {
115+ console . log ( `ERROR Failed downloading item list using ${ allItemsUrl } ` ) ;
116+ hasMore = false ;
117+ }
96118 }
97- // Parse the response as JSON
119+ // Now perform some updates on the data to ensure that it will process properly
98120 try {
99- const data = await response . json ( ) ;
100-
101- // Now, if needed loop through the assets and retrieve any remaining ones above the limit.
102-
103- if ( data . hasMore ) {
104- const { totalResults } = data ;
105-
106- for ( let offset = fetchLimit ; offset < totalResults ; offset += fetchLimit ) {
107- const partialQuery = allItemsUrl . replace ( '&offset=0' , `&offset=${ offset } ` ) ;
108- response = await fetch ( partialQuery , { headers } ) ;
109- const partialData = await response . json ( ) ;
110- data . items = data . items . concat ( partialData . items ) ;
111- }
112-
113121 // We have to ensure that the types of any of the items don't have any hyphens.
114122 // Having a hyphen on a GraphQl index type seems to cause issues.
115- }
116- for ( let x = 0 ; x < data . items . length ; x += 1 ) {
117- data . items [ x ] . type = data . items [ x ] . type . replace ( '-' , '' ) ;
123+ for ( let x = 0 ; x < itemsArray . length ; x += 1 ) {
124+ itemsArray [ x ] . type = itemsArray [ x ] . type . replace ( '-' , '' ) ;
118125 }
119126
120- // console.log(JSON.stringify(data , null, 2));
127+ // console.log(JSON.stringify(itemsArray , null, 2));
121128 if ( debug ) {
122- await fs . writeFile ( '.data/items.json' , JSON . stringify ( data ) , 'utf-8' ) ;
129+ await fs . writeFile ( '.data/items.json' , JSON . stringify ( itemsArray ) , 'utf-8' ) ;
123130 }
124- return Promise . all ( data . items . map ( ( e ) => e . id ) . map ( fetchItem ) ) ;
131+ return Promise . all ( itemsArray . map ( ( e ) => e . id ) . map ( fetchItem ) ) ;
125132 } catch ( err ) {
126- console . log ( response ) ;
133+ console . log ( err ) ;
127134 throw err ;
128135 }
129136 } ;
0 commit comments