11const request = require ( 'request-promise-native' ) ;
22const url = require ( 'url' ) ;
33const async = require ( 'async' ) ;
4+ const crypto = require ( 'crypto' ) ;
45
56/**
67 * base model
@@ -29,17 +30,25 @@ class base {
2930 * @returns {* }
3031 */
3132 post ( path , data ) {
33+ let key = base . createDumpKey ( path , data ) ;
34+ if ( this . config . dump ) return this . getDump ( key ) ;
35+
3236 data . private_token = this . token ;
3337
34- return request . post ( `${ this . url } ${ path } ` , {
35- json : true ,
36- body : data ,
37- insecure : this . _insecure ,
38- proxy : this . _proxy ,
39- resolveWithFullResponse : true ,
40- headers : {
41- 'PRIVATE-TOKEN' : this . token
42- }
38+ return new Promise ( ( resolve , reject ) => {
39+ request . post ( `${ this . url } ${ path } ` , {
40+ json : true ,
41+ body : data ,
42+ insecure : this . _insecure ,
43+ proxy : this . _proxy ,
44+ resolveWithFullResponse : true ,
45+ headers : {
46+ 'PRIVATE-TOKEN' : this . token
47+ }
48+ } ) . then ( response => {
49+ if ( this . config . get ( '_createDump' ) ) this . setDump ( response , key ) ;
50+ resolve ( response ) ;
51+ } ) . catch ( e => reject ( e ) ) ;
4352 } ) ;
4453 }
4554
@@ -51,17 +60,25 @@ class base {
5160 * @returns {Promise }
5261 */
5362 get ( path , page = 1 , perPage = this . _perPage ) {
63+ let key = base . createDumpKey ( path , page , perPage ) ;
64+ if ( this . config . dump ) return this . getDump ( key ) ;
65+
5466 path += ( path . includes ( '?' ) ? '&' : '?' ) + `private_token=${ this . token } ` ;
5567 path += `&page=${ page } &per_page=${ perPage } ` ;
5668
57- return request ( `${ this . url } ${ path } ` , {
58- json : true ,
59- insecure : this . _insecure ,
60- proxy : this . _proxy ,
61- resolveWithFullResponse : true ,
62- headers : {
63- 'PRIVATE-TOKEN' : this . token
64- }
69+ return new Promise ( ( resolve , reject ) => {
70+ request ( `${ this . url } ${ path } ` , {
71+ json : true ,
72+ insecure : this . _insecure ,
73+ proxy : this . _proxy ,
74+ resolveWithFullResponse : true ,
75+ headers : {
76+ 'PRIVATE-TOKEN' : this . token
77+ }
78+ } ) . then ( response => {
79+ if ( this . config . get ( '_createDump' ) ) this . setDump ( response , key ) ;
80+ resolve ( response ) ;
81+ } ) . catch ( e => reject ( e ) ) ;
6582 } ) ;
6683 }
6784
@@ -124,6 +141,29 @@ class base {
124141 } , runners ) ;
125142 }
126143
144+ /**
145+ * save the given response to dump
146+ * @param response
147+ * @param key
148+ */
149+ setDump ( response , key ) {
150+ if ( ! this . config . _dump ) this . config . _dump = { } ;
151+
152+ this . config . _dump [ key ] = {
153+ headers : response . headers ,
154+ body : response . body
155+ } ;
156+ }
157+
158+ /**
159+ * get from dump
160+ * @param key
161+ * @returns {Promise }
162+ */
163+ getDump ( key ) {
164+ return new Promise ( r => r ( this . config . dump [ key ] ) ) ;
165+ }
166+
127167 /**
128168 * create a task list to get all pages from
129169 * the given path
@@ -142,6 +182,14 @@ class base {
142182
143183 return tasks ;
144184 }
185+
186+ /**
187+ * create a key representing a request
188+ * @param args
189+ */
190+ static createDumpKey ( ...args ) {
191+ return crypto . createHash ( 'md5' ) . update ( JSON . stringify ( args ) ) . digest ( "hex" ) ;
192+ }
145193}
146194
147195module . exports = base ;
0 commit comments