Skip to content

Commit 688fb25

Browse files
authored
Merge pull request #22 from sonnenkern/develop
Develop
2 parents 5e054c8 + e33220c commit 688fb25

File tree

5 files changed

+72
-12
lines changed

5 files changed

+72
-12
lines changed

lib/QuipProcessor.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,12 @@ class QuipProcessor {
375375
if(file.fileName) {
376376
fileName = file.fileName.trim();
377377
} else {
378-
fileName = `${file.blobId.trim()}.${Mime.getExtension(blob.type).trim()}`;
378+
const extension = Mime.getExtension(blob.type);
379+
if(extension) {
380+
fileName = `${file.blobId.trim()}.${Mime.getExtension(blob.type).trim()}`;
381+
} else {
382+
fileName = `${file.blobId.trim()}`;
383+
}
379384
}
380385
fileName = sanitizeFilename(fileName);
381386

lib/QuipService.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
const fetch = require('node-fetch');
22
const LoggerAdapter = require('./common/LoggerAdapter');
33

4+
const TIMES_LIMIT_503 = 10;
5+
46
class QuipService {
57
constructor(accessToken, apiURL='https://platform.quip.com:443/1') {
68
this.accessToken = accessToken;
79
this.apiURL = apiURL;
810
this.logger = new LoggerAdapter();
11+
this.querries503 = new Map();
912
this.stats = {
1013
query_count: 0,
1114
getThread_count: 0,
@@ -100,15 +103,22 @@ class QuipService {
100103
if(res.status === 503) {
101104
const waitingInMs = res.headers.get('x-ratelimit-reset')*1000 - new Date().getTime();
102105
this.logger.debug(`HTTP 503: for ${url}, waiting in ms: ${waitingInMs}`);
103-
return new Promise(resolve => setTimeout(() => {
104-
resolve(this._apiCallBlob(url, method));
105-
}, waitingInMs));
106+
if(this._check503Query(url)) {
107+
return new Promise(resolve => setTimeout(() => {
108+
resolve(this._apiCallBlob(url, method));
109+
}, waitingInMs));
110+
} else {
111+
this.logger.error(`Couldn't fetch ${url}, tryed to get it ${TIMES_LIMIT_503} times`);
112+
return;
113+
}
106114
} else {
107115
this.logger.debug(`Couldn't fetch ${url}, received ${res.status}`);
108116
return;
109117
}
110118
}
111119

120+
this.querries503.delete(url);
121+
112122
return res.blob();
113123
} catch (e) {
114124
this.logger.error(`Couldn't fetch ${url}, `, e);
@@ -124,9 +134,14 @@ class QuipService {
124134
if(res.status === 503) {
125135
const waitingInMs = res.headers.get('x-ratelimit-reset')*1000 - new Date().getTime();
126136
this.logger.debug(`HTTP 503: for ${url}, waiting in ms: ${waitingInMs}`);
127-
return new Promise(resolve => setTimeout(() => {
128-
resolve(this._apiCall(url, method));
129-
}, waitingInMs));
137+
if(this._check503Query(url)) {
138+
return new Promise(resolve => setTimeout(() => {
139+
resolve(this._apiCall(url, method));
140+
}, waitingInMs));
141+
} else {
142+
this.logger.error(`Couldn't fetch ${url}, tryed to get it ${TIMES_LIMIT_503} times`);
143+
return;
144+
}
130145
} else {
131146
this.logger.debug(`Couldn't fetch ${url}, received ${res.status}`);
132147
return;
@@ -139,6 +154,20 @@ class QuipService {
139154
}
140155
}
141156

157+
_check503Query(url) {
158+
let count = this.querries503.get(url);
159+
if(!count) {
160+
count = 0;
161+
}
162+
163+
this.querries503.set(url, ++count);
164+
if(count > TIMES_LIMIT_503) {
165+
return false;
166+
}
167+
168+
return true;
169+
}
170+
142171
_getOptions(method) {
143172
return {
144173
method: method,

lib/__tests__/QuipProcessor.test.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,17 +698,27 @@ describe('methods tests', () => {
698698
const returnedValue = await quipProcessor._processFile('HTML-REPLACEMENT', fileInfo, '/aaa/', false);
699699
expect(quipProcessor.saveCallback).toHaveBeenCalledWith(blob, 'FILE_NAME_SANITIZED', 'BLOB', '/aaa/blobs');
700700
expect(returnedValue).toBe('HTML-blobs/FILE_NAME_SANITIZED');
701-
//`BLOB_ID.pdf`;
702-
//
703701
});
704702

705703
test('blob processing as file without fileName', async () => {
706704
fileInfo.fileName = undefined;
707705
const returnedValue = await quipProcessor._processFile('HTML-REPLACEMENT', fileInfo, '/aaa/', false);
708706
expect(quipProcessor.saveCallback).toHaveBeenCalledWith(blob, 'BLOB_ID.pdf_SANITIZED', 'BLOB', '/aaa/blobs');
709707
expect(returnedValue).toBe('HTML-blobs/BLOB_ID.pdf_SANITIZED');
710-
//``;
711-
//
708+
});
709+
710+
test('blob processing as file without fileName and without right mime type', async () => {
711+
fileInfo.fileName = undefined;
712+
//type is not defined
713+
delete blob.type;
714+
let returnedValue = await quipProcessor._processFile('HTML-REPLACEMENT', fileInfo, '/aaa/', false);
715+
expect(quipProcessor.saveCallback).toHaveBeenCalledWith(blob, 'BLOB_ID_SANITIZED', 'BLOB', '/aaa/blobs');
716+
expect(returnedValue).toBe('HTML-blobs/BLOB_ID_SANITIZED');
717+
//type is wrong
718+
blob.type = "dddfffggg";
719+
returnedValue = await quipProcessor._processFile('HTML-REPLACEMENT', fileInfo, '/aaa/', false);
720+
expect(quipProcessor.saveCallback).toHaveBeenCalledWith(blob, 'BLOB_ID_SANITIZED', 'BLOB', '/aaa/blobs');
721+
expect(returnedValue).toBe('HTML-blobs/BLOB_ID_SANITIZED');
712722
});
713723
});
714724

lib/__tests__/QuipService.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ test('_apiCall response with 503 code', async () => {
4545
expect(quipService.stats.query_count).toBe(2);
4646
});
4747

48+
test('_apiCall response with 503 code more than 10 times', async () => {
49+
quipService.stats.query_count = 0;
50+
fetch.mockReturnValue(Promise.resolve(response503));
51+
const res = await quipService._apiCall('/someURL');
52+
expect(res).toBe(undefined);
53+
expect(quipService.logger.error).toHaveBeenCalledWith('Couldn\'t fetch /someURL, tryed to get it 10 times');
54+
});
55+
4856
test('_apiCall response with 500 code', async () => {
4957
fetch.mockReturnValue(Promise.resolve(response500));
5058
await quipService._apiCall('/someURL');
@@ -68,6 +76,14 @@ test('_apiCallBlob response with 503 code', async () => {
6876
expect(quipService.stats.query_count).toBe(2);
6977
});
7078

79+
test('_apiCallBlob response with 503 code more than 10 times', async () => {
80+
quipService.stats.query_count = 0;
81+
fetch.mockReturnValue(Promise.resolve(response503));
82+
const res = await quipService._apiCallBlob('/someURL');
83+
expect(res).toBe(undefined);
84+
expect(quipService.logger.error).toHaveBeenCalledWith('Couldn\'t fetch /someURL, tryed to get it 10 times');
85+
});
86+
7187
test('_apiCallBlob response with 500 code', async () => {
7288
fetch.mockReturnValue(Promise.resolve(response500));
7389
await quipService._apiCallBlob('/someURL');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quip-export",
3-
"version": "2.2.1",
3+
"version": "2.2.2",
44
"description": "Export all folders and documents from Quip",
55
"main": "index.js",
66
"jest": {

0 commit comments

Comments
 (0)