-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathproject-management-api-request-internal.ts
338 lines (309 loc) · 11.5 KB
/
project-management-api-request-internal.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*!
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { App } from '../app';
import { FirebaseApp } from '../app/firebase-app';
import {
AuthorizedHttpClient, HttpError, HttpMethod, HttpRequestConfig, ExponentialBackoffPoller,
} from '../utils/api-request';
import { FirebaseProjectManagementError, ProjectManagementErrorCode } from '../utils/error';
import { getSdkVersion } from '../utils/index';
import * as validator from '../utils/validator';
import { ShaCertificate } from './android-app';
/** Project management backend host and port. */
const PROJECT_MANAGEMENT_HOST_AND_PORT = 'firebase.googleapis.com:443';
/** Project management backend path. */
const PROJECT_MANAGEMENT_PATH = '/v1/';
/** Project management beta backend path. */
const PROJECT_MANAGEMENT_BETA_PATH = '/v1beta1/';
/** Project management request header. */
const PROJECT_MANAGEMENT_HEADERS = {
'X-Client-Version': `Node/Admin/${getSdkVersion()}`,
};
/** Project management request timeout duration in milliseconds. */
const FIREBASE_PROJECT_MANAGEMENT_TIMEOUT = parseInt(process.env.FIREBASE_PROJECT_MANAGEMENT_TIMEOUT || '10000', 10);
const LIST_APPS_MAX_PAGE_SIZE = 100;
const CERT_TYPE_API_MAP = {
sha1: 'SHA_1',
sha256: 'SHA_256',
};
export function assertServerResponse(
condition: boolean, responseData: object, message: string): void {
if (!condition) {
throw new FirebaseProjectManagementError(
'invalid-server-response',
`${message} Response data: ${JSON.stringify(responseData, null, 2)}`);
}
}
/**
* Class that provides mechanism to send requests to the Firebase project management backend
* endpoints.
*
* @internal
*/
export class ProjectManagementRequestHandler {
private readonly baseUrl: string =
`https://${PROJECT_MANAGEMENT_HOST_AND_PORT}${PROJECT_MANAGEMENT_PATH}`;
private readonly baseBetaUrl: string =
`https://${PROJECT_MANAGEMENT_HOST_AND_PORT}${PROJECT_MANAGEMENT_BETA_PATH}`;
private readonly httpClient: AuthorizedHttpClient;
private static wrapAndRethrowHttpError(errStatusCode: number, errText?: string): void {
let errorCode: ProjectManagementErrorCode;
let errorMessage: string;
switch (errStatusCode) {
case 400:
errorCode = 'invalid-argument';
errorMessage = 'Invalid argument provided.';
break;
case 401:
case 403:
errorCode = 'authentication-error';
errorMessage = 'An error occurred when trying to authenticate. Make sure the credential '
+ 'used to authenticate this SDK has the proper permissions. See '
+ 'https://firebase.google.com/docs/admin/setup for setup instructions.';
break;
case 404:
errorCode = 'not-found';
errorMessage = 'The specified entity could not be found.';
break;
case 409:
errorCode = 'already-exists';
errorMessage = 'The specified entity already exists.';
break;
case 500:
errorCode = 'internal-error';
errorMessage = 'An internal error has occurred. Please retry the request.';
break;
case 503:
errorCode = 'service-unavailable';
errorMessage = 'The server could not process the request in time. See the error '
+ 'documentation for more details.';
break;
default:
errorCode = 'unknown-error';
errorMessage = 'An unknown server error was returned.';
}
if (!errText) {
errText = '<missing>';
}
throw new FirebaseProjectManagementError(
errorCode,
`${ errorMessage } Status code: ${ errStatusCode }. Raw server response: "${ errText }".`);
}
/**
* @param app - The app used to fetch access tokens to sign API requests.
* @constructor
*/
constructor(app: App) {
this.httpClient = new AuthorizedHttpClient(app as FirebaseApp);
}
/**
* @param parentResourceName - Fully-qualified resource name of the project whose Android
* apps you want to list.
*/
public listAndroidApps(parentResourceName: string): Promise<object> {
return this.invokeRequestHandler(
'GET',
`${parentResourceName}/androidApps?page_size=${LIST_APPS_MAX_PAGE_SIZE}`,
/* requestData */ null,
'v1beta1');
}
/**
* @param parentResourceName - Fully-qualified resource name of the project whose iOS apps
* you want to list.
*/
public listIosApps(parentResourceName: string): Promise<object> {
return this.invokeRequestHandler(
'GET',
`${parentResourceName}/iosApps?page_size=${LIST_APPS_MAX_PAGE_SIZE}`,
/* requestData */ null,
'v1beta1');
}
/**
* @param parentResourceName - Fully-qualified resource name of the project whose iOS apps
* you want to list.
*/
public listAppMetadata(parentResourceName: string): Promise<object> {
return this.invokeRequestHandler(
'GET',
`${parentResourceName}:searchApps?page_size=${LIST_APPS_MAX_PAGE_SIZE}`,
/* requestData */ null,
'v1beta1');
}
/**
* @param parentResourceName - Fully-qualified resource name of the project that you want
* to create the Android app within.
*/
public createAndroidApp(
parentResourceName: string, packageName: string, displayName?: string): Promise<object> {
const requestData: any = {
packageName,
};
if (validator.isNonEmptyString(displayName)) {
requestData.displayName = displayName;
}
return this
.invokeRequestHandler('POST', `${parentResourceName}/androidApps`, requestData, 'v1beta1')
.then((responseData: any) => {
assertServerResponse(
validator.isNonNullObject(responseData),
responseData,
'createAndroidApp\'s responseData must be a non-null object.');
assertServerResponse(
validator.isNonEmptyString(responseData.name),
responseData,
'createAndroidApp\'s responseData.name must be a non-empty string.');
return this.pollRemoteOperationWithExponentialBackoff(responseData.name);
});
}
/**
* @param parentResourceName - Fully-qualified resource name of the project that you want
* to create the iOS app within.
*/
public createIosApp(
parentResourceName: string, bundleId: string, displayName?: string): Promise<object> {
const requestData: any = {
bundleId,
};
if (validator.isNonEmptyString(displayName)) {
requestData.displayName = displayName;
}
return this
.invokeRequestHandler('POST', `${parentResourceName}/iosApps`, requestData, 'v1beta1')
.then((responseData: any) => {
assertServerResponse(
validator.isNonNullObject(responseData),
responseData,
'createIosApp\'s responseData must be a non-null object.');
assertServerResponse(
validator.isNonEmptyString(responseData.name),
responseData,
'createIosApp\'s responseData.name must be a non-empty string.');
return this.pollRemoteOperationWithExponentialBackoff(responseData.name);
});
}
/**
* @param resourceName - Fully-qualified resource name of the entity whose display name you
* want to set.
*/
public setDisplayName(resourceName: string, newDisplayName: string): Promise<void> {
const requestData = {
displayName: newDisplayName,
};
return this
.invokeRequestHandler(
'PATCH', `${resourceName}?update_mask=display_name`, requestData, 'v1beta1')
.then(() => undefined);
}
/**
* @param parentResourceName - Fully-qualified resource name of the Android app whose SHA
* certificates you want to get.
*/
public getAndroidShaCertificates(parentResourceName: string): Promise<object> {
return this.invokeRequestHandler(
'GET', `${parentResourceName}/sha`, /* requestData */ null, 'v1beta1');
}
/**
* @param parentResourceName - Fully-qualified resource name of the Android app that you
* want to add the given SHA certificate to.
*/
public addAndroidShaCertificate(
parentResourceName: string, certificate: ShaCertificate): Promise<void> {
const requestData = {
shaHash: certificate.shaHash,
certType: CERT_TYPE_API_MAP[certificate.certType],
};
return this
.invokeRequestHandler('POST', `${parentResourceName}/sha`, requestData, 'v1beta1')
.then(() => undefined);
}
/**
* @param parentResourceName - Fully-qualified resource name of the app whose config you
* want to get.
*/
public getConfig(parentResourceName: string): Promise<object> {
return this.invokeRequestHandler(
'GET', `${parentResourceName}/config`, /* requestData */ null, 'v1beta1');
}
/**
* @param parentResourceName - Fully-qualified resource name of the entity that you want to
* get.
*/
public getResource(parentResourceName: string): Promise<object> {
return this.invokeRequestHandler('GET', parentResourceName, /* requestData */ null, 'v1beta1');
}
/**
* @param resourceName - Fully-qualified resource name of the entity that you want to
* delete.
*/
public deleteResource(resourceName: string): Promise<void> {
return this
.invokeRequestHandler('DELETE', resourceName, /* requestData */ null, 'v1beta1')
.then(() => undefined);
}
private pollRemoteOperationWithExponentialBackoff(
operationResourceName: string): Promise<object> {
const poller = new ExponentialBackoffPoller<object>();
return poller.poll(() => {
return this.invokeRequestHandler('GET', operationResourceName, /* requestData */ null)
.then((responseData: any) => {
if (responseData.error) {
const errStatusCode: number = responseData.error.code || 500;
const errText: string =
responseData.error.message || JSON.stringify(responseData.error);
ProjectManagementRequestHandler.wrapAndRethrowHttpError(errStatusCode, errText);
}
if (!responseData.done) {
// Continue polling.
return null;
}
// Polling complete. Resolve with operation response JSON.
return responseData.response;
});
});
}
/**
* Invokes the request handler with the provided request data.
*/
private invokeRequestHandler(
method: HttpMethod,
path: string,
requestData: object | null,
apiVersion: ('v1' | 'v1beta1') = 'v1'): Promise<object> {
const baseUrlToUse = (apiVersion === 'v1') ? this.baseUrl : this.baseBetaUrl;
const request: HttpRequestConfig = {
method,
url: `${baseUrlToUse}${path}`,
headers: PROJECT_MANAGEMENT_HEADERS,
data: requestData,
timeout: FIREBASE_PROJECT_MANAGEMENT_TIMEOUT,
};
return this.httpClient.send(request)
.then((response) => {
// Send non-JSON responses to the catch() below, where they will be treated as errors.
if (!response.isJson()) {
throw new HttpError(response);
}
return response.data;
})
.catch((err) => {
if (err instanceof HttpError) {
ProjectManagementRequestHandler.wrapAndRethrowHttpError(
err.response.status, err.response.text);
}
throw err;
});
}
}