This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
forked from mobile-dev-inc/action-maestro-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiClient.ts
119 lines (103 loc) · 2.57 KB
/
ApiClient.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import fetch, { FetchError, fileFromSync, FormData } from 'node-fetch';
export enum BenchmarkStatus {
PENDING = 'PENDING',
RUNNING = 'RUNNING',
SUCCESS = 'SUCCESS',
ERROR = 'ERROR',
CANCELED = 'CANCELED',
WARNING = 'WARNING',
}
export type UploadRequest = {
benchmarkName?: string
repoOwner?: string
repoName?: string
pullRequestId?: string
branch?: string,
commitSha?: string,
env?: { [key: string]: string },
agent: string,
androidApiLevel?: number,
includeTags: string[],
excludeTags: string[],
}
// irrelevant data has been factored out from this model
export type UploadResponse = {
uploadId: string,
teamId: string,
targetId: string
}
export class UploadStatusError {
constructor(public status: number, public text: string) { }
}
export type Flow = {
name: string,
status: BenchmarkStatus
}
export type UploadStatusResponse = {
uploadId: string,
status: BenchmarkStatus,
completed: boolean,
flows: Flow[]
}
export default class ApiClient {
constructor(
private apiKey: string,
private apiUrl: string
) { }
async uploadRequest(
request: UploadRequest,
appFile: string,
workspaceZip: string | null,
mappingFile: string | null,
): Promise<UploadResponse> {
const formData = new FormData()
formData.set('request', JSON.stringify(request))
formData.set(
'app_binary',
fileFromSync(appFile)
)
if (workspaceZip) {
formData.set(
'workspace',
fileFromSync(workspaceZip)
);
}
if (mappingFile) {
formData.set(
'mapping',
fileFromSync(mappingFile)
)
}
const res = await fetch(`${this.apiUrl}/v2/upload`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
},
body: formData
});
if (!res.ok) {
const body = await res.text();
throw new Error(`Request to ${res.url} failed (${res.status}): ${body}`);
}
return await res.json() as UploadResponse;
}
async getUploadStatus(
uploadId: string,
): Promise<UploadStatusResponse> {
const res = await fetch(`${this.apiUrl}/v2/upload/${uploadId}/status`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
},
});
if (!res.ok) {
const body = await res.text();
throw new Error(`Request to ${res.url} failed (${res.status}): ${body}`);
}
if (res.status >= 400) {
const text = await res.text();
Promise.reject(new UploadStatusError(res.status, text));
}
return await res.json() as UploadStatusResponse;
}
}