-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.ts
99 lines (88 loc) · 2.5 KB
/
http.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { fetch } from 'undici';
import { GlobalStorage } from '../../storage';
import { Credentials, Vote, isAppStorageFilesApiResponse } from '../../types';
import { getHTTPServer, getDomain } from './config';
/**
* Download image from imgURL and save it to `imgDir/imgName`.
* Skips download if file already exists.
*/
export async function downloadImage(
testID: string,
imgURL: string,
imgName: string,
username: string,
accessKey: string,
storage: GlobalStorage,
) {
for (let attempt = 0; attempt < 15; attempt++) {
if (attempt > 0) {
console.log('Retrying image download...');
await new Promise((f) => setTimeout(f, 3000));
}
try {
const response = await fetch(imgURL, {
headers: {
Authorization: 'Basic ' + btoa(username + ':' + accessKey),
},
});
if (response.status === 404) {
console.log('Image not found.');
continue;
}
if (!response.ok) {
console.error(`Unexpected response: ${response.statusText}`);
continue;
}
if (!response.body) {
console.error(`Unexpected response: ${response.statusText}: no body`);
continue;
}
console.log('Saving image...');
await storage.saveTestRecordAsset(testID, imgName, response.body);
return;
} catch (error) {
console.error('Failed to download image:', error);
}
}
}
export async function sendUserRating(
votes: Vote[],
testID: string,
creds: Credentials,
) {
const resp = await fetch(`${getHTTPServer(creds.region)}/submitFeedback`, {
method: 'POST',
body: JSON.stringify({
test_id: testID,
votes,
}),
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa(creds.username + ':' + creds.accessKey),
},
});
if (!resp.ok) {
throw new Error('Unexpected status code: ' + resp.status);
}
}
export async function fetchApps(creds: Credentials) {
const response = await fetch(
`https://${getDomain(creds.region)}/v1/storage/files?kind=ios&kind=android`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa(`${creds.username}:${creds.accessKey}`),
},
},
);
if (!response.ok) {
throw new Error('Network response was not ok: ' + response.status);
}
const data = await response.json();
if (isAppStorageFilesApiResponse(data)) {
return data.items;
} else {
throw new Error('Unexpected data format from API: ' + data);
}
}