@@ -5,43 +5,74 @@ import arcfetch, { ArcFetchRequest, ArcFetchMethod } from './arcfetch';
55import IArcAccount from './interfaces/IArcAccount' ;
66import arcapi_any from './arcapi.any' ;
77
8- export default ( account : IArcAccount , endpoints : Array < string > ) => {
8+ export default async ( account : IArcAccount ,
9+ endpoints : Array < string > ) : Promise < any > => {
10+
11+ // account will be BANNED from server if exceed
12+ if ( endpoints . length > BOTARCAPI_AGGREGATE_LIMITATION )
13+ throw new Error ( 'Endpoints limit exceeded' ) ;
14+
15+ if ( BOTARCAPI_AGGREGATE_ENABLED ) {
16+
17+ // construct endpoint object
18+ const _endpoints : Array < any > = [ ] ;
19+ endpoints . forEach ( ( element , index ) => {
20+ _endpoints . push ( { endpoint : element , id : index } ) ;
21+ } ) ;
22+
23+ // construct remote request
24+ const _remote_request =
25+ new ArcFetchRequest ( ArcFetchMethod . GET , 'compose/aggregate' , {
26+ deviceId : account . device ,
27+ userToken : account . token ,
28+ submitData : new URLSearchParams ( { 'calls' : JSON . stringify ( _endpoints ) } )
29+ } ) ;
930
10- return new Promise ( async ( resolve , reject ) => {
31+ // send request
32+ return arcfetch ( _remote_request )
33+ . then ( ( root : any ) => {
1134
12- // account will be BANNED from server if exceed
13- if ( endpoints . length > BOTARCAPI_AGGREGATE_LIMITATION )
14- return reject ( new Error ( 'Endpoints limit exceeded' ) ) ;
35+ // teardown the object and pack data into array
36+ const _data : Array < any > = [ ] ;
37+ root . value . forEach ( ( element : any ) => {
38+ _data [ element . id ] = element . value [ 0 ] ;
39+ } )
1540
16- if ( BOTARCAPI_AGGREGATE_ENABLED ) {
41+ return _data ;
42+ } )
43+ . catch ( ( e : string ) => {
1744
18- // construct endpoint object
19- const _endpoints : Array < any > = [ ] ;
20- endpoints . forEach ( ( element , index ) => {
21- _endpoints . push ( { endpoint : element , id : index } ) ;
45+ // if token is invalid
46+ // just erase the token and wait for
47+ // auto login in next time allocating
48+ if ( e == 'UnauthorizedError' ) {
49+ account . token = '' ;
50+ syslog . w ( TAG , `Invalid token => ${ account . name } ${ account . token } ` ) ;
51+ }
52+
53+ throw e ;
2254 } ) ;
2355
24- // construct remote request
25- const _remote_request =
26- new ArcFetchRequest ( ArcFetchMethod . GET , 'compose/aggregate' , {
27- deviceId : account . device ,
28- userToken : account . token ,
29- submitData : new URLSearchParams ( { 'calls' : JSON . stringify ( _endpoints ) } )
30- } ) ;
56+ } else {
57+ if ( BOTARCAPI_AGGREGATE_CONCURRENT ) {
3158
32- // send request
33- arcfetch ( _remote_request )
34- . then ( ( root : any ) => {
59+ // construct tasks
60+ const _tasks : Array < Promise < any > > = [ ] ;
61+ endpoints . forEach ( ( element , index ) => {
62+ _tasks . push ( arcapi_any ( account , ArcFetchMethod . GET , element , "" ) ) ;
63+ } ) ;
3564
36- // teardown the object and pack data into array
37- const _data : Array < any > = [ ] ;
38- root . value . forEach ( ( element : any ) => {
39- _data [ element . id ] = element . value [ 0 ] ;
40- } )
65+ // wait for data coming in
66+ return Promise . all ( _tasks )
67+ . then ( data => {
68+ let _results : any [ ] = [ ] ;
69+
70+ data . forEach ( ( element , index ) =>
71+ _results . push ( element . value [ 0 ] ) ) ;
4172
42- resolve ( _data ) ;
43- } )
44- . catch ( ( e : string ) => {
73+ return _results ;
74+
75+ } ) . catch ( ( e : string ) => {
4576
4677 // if token is invalid
4778 // just erase the token and wait for
@@ -50,49 +81,36 @@ export default (account: IArcAccount, endpoints: Array<string>) => {
5081 account . token = '' ;
5182 syslog . w ( TAG , `Invalid token => ${ account . name } ${ account . token } ` ) ;
5283 }
53-
54- reject ( e ) ;
55-
84+ throw e ;
5685 } ) ;
5786
5887 } else {
59- if ( BOTARCAPI_AGGREGATE_CONCURRENT ) {
60-
61- // construct tasks
62- const _tasks : Array < Promise < any > > = [ ] ;
63- endpoints . forEach ( ( element , index ) => {
64- _tasks . push ( new Promise ( async ( resolve , reject ) => {
65- resolve ( await arcapi_any ( account , ArcFetchMethod . GET , element , "" ) ) ;
66- } ) ) ;
67- } ) ;
68-
69- // wait for data coming in
70- Promise . all ( _tasks )
71- . then ( data => {
72- let _results : any [ ] = [ ] ;
73-
74- data . forEach ( ( element , index ) =>
75- _results . push ( element . value [ 0 ] ) ) ;
7688
77- resolve ( _results ) ;
89+ let _results : any [ ] = [ ] ;
7890
79- } ) . catch ( ( e : string ) => reject ( e ) ) ;
80-
81- } else {
82-
83- let _results : any [ ] = [ ] ;
91+ try {
8492
8593 // request one by one
8694 for ( let i = 0 ; i < endpoints . length ; ++ i ) {
8795 const _data : any = await arcapi_any ( account , ArcFetchMethod . GET , endpoints [ i ] , "" ) ;
8896 _results . push ( _data . value [ 0 ] ) ;
8997 }
98+ return _results ;
9099
91- resolve ( _results ) ;
100+ } catch ( e ) {
101+
102+ // if token is invalid
103+ // just erase the token and wait for
104+ // auto login in next time allocating
105+ if ( e == 'UnauthorizedError' ) {
106+ account . token = '' ;
107+ syslog . w ( TAG , `Invalid token => ${ account . name } ${ account . token } ` ) ;
108+ }
109+ throw e ;
92110 }
93111
94112 }
95113
96- } ) ;
114+ }
97115
98116}
0 commit comments