Skip to content

Commit 63b9547

Browse files
authored
Using the new HttpClient API in instanceId module (#321)
* Using the new HttpClient API in instanceId module * Fixing some lint errors
1 parent b410895 commit 63b9547

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

src/instance-id/instance-id-request.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616

1717
import {FirebaseApp} from '../firebase-app';
18-
import {FirebaseError, FirebaseInstanceIdError, InstanceIdClientErrorCode} from '../utils/error';
18+
import {FirebaseInstanceIdError, InstanceIdClientErrorCode} from '../utils/error';
1919
import {
20-
HttpMethod, SignedApiRequestHandler, ApiSettings,
20+
ApiSettings, AuthorizedHttpClient, HttpRequestConfig, HttpError,
2121
} from '../utils/api-request';
2222

2323
import * as validator from '../utils/validator';
@@ -48,11 +48,11 @@ const ERROR_CODES = {
4848
*/
4949
export class FirebaseInstanceIdRequestHandler {
5050

51-
private host: string = FIREBASE_IID_HOST;
52-
private port: number = FIREBASE_IID_PORT;
53-
private timeout: number = FIREBASE_IID_TIMEOUT;
54-
private signedApiRequestHandler: SignedApiRequestHandler;
55-
private path: string;
51+
private readonly host: string = FIREBASE_IID_HOST;
52+
private readonly port: number = FIREBASE_IID_PORT;
53+
private readonly timeout: number = FIREBASE_IID_TIMEOUT;
54+
private readonly httpClient: AuthorizedHttpClient;
55+
private readonly path: string;
5656

5757
/**
5858
* @param {FirebaseApp} app The app used to fetch access tokens to sign API requests.
@@ -61,7 +61,7 @@ export class FirebaseInstanceIdRequestHandler {
6161
* @constructor
6262
*/
6363
constructor(app: FirebaseApp, projectId: string) {
64-
this.signedApiRequestHandler = new SignedApiRequestHandler(app);
64+
this.httpClient = new AuthorizedHttpClient(app);
6565
this.path = FIREBASE_IID_PATH + `project/${projectId}/instanceId/`;
6666
}
6767

@@ -83,28 +83,31 @@ export class FirebaseInstanceIdRequestHandler {
8383
*/
8484
private invokeRequestHandler(apiSettings: ApiSettings): Promise<object> {
8585
const path: string = this.path + apiSettings.getEndpoint();
86-
const httpMethod: HttpMethod = apiSettings.getHttpMethod();
8786
return Promise.resolve()
8887
.then(() => {
89-
return this.signedApiRequestHandler.sendRequest(
90-
this.host, this.port, path, httpMethod, undefined, undefined, this.timeout);
88+
const req: HttpRequestConfig = {
89+
url: `https://${this.host}${path}`,
90+
method: apiSettings.getHttpMethod(),
91+
timeout: this.timeout,
92+
};
93+
return this.httpClient.send(req);
9194
})
9295
.then((response) => {
93-
return response;
96+
return response.data;
9497
})
95-
.catch((response) => {
96-
const error = (typeof response === 'object' && 'error' in response) ?
97-
response.error : response;
98-
if (error instanceof FirebaseError) {
99-
// In case of timeouts and other network errors, the API request handler returns a
100-
// FirebaseError wrapped in the response. Simply throw it here.
101-
throw error;
98+
.catch((err) => {
99+
if (err instanceof HttpError) {
100+
const response = err.response;
101+
const errorMessage: string = (response.isJson() && 'error' in response.data) ?
102+
response.data.error : response.text;
103+
const template: string = ERROR_CODES[response.status];
104+
const message: string = template ?
105+
`Instance ID "${apiSettings.getEndpoint()}": ${template}` : errorMessage;
106+
throw new FirebaseInstanceIdError(InstanceIdClientErrorCode.API_ERROR, message);
102107
}
103-
104-
const template: string = ERROR_CODES[response.statusCode];
105-
const message: string = template ?
106-
`Instance ID "${apiSettings.getEndpoint()}": ${template}` : JSON.stringify(error);
107-
throw new FirebaseInstanceIdError(InstanceIdClientErrorCode.API_ERROR, message);
108+
// In case of timeouts and other network errors, the HttpClient returns a
109+
// FirebaseError wrapped in the response. Simply throw it here.
110+
throw err;
108111
});
109112
}
110113
}

test/unit/instance-id/instance-id-request.spec.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import * as utils from '../utils';
2727
import * as mocks from '../../resources/mocks';
2828

2929
import {FirebaseApp} from '../../../src/firebase-app';
30-
import {HttpRequestHandler} from '../../../src/utils/api-request';
30+
import {HttpClient} from '../../../src/utils/api-request';
3131
import {FirebaseInstanceIdRequestHandler} from '../../../src/instance-id/instance-id-request';
3232

3333
chai.should();
@@ -56,6 +56,7 @@ describe('FirebaseInstanceIdRequestHandler', () => {
5656
expectedHeaders = {
5757
Authorization: 'Bearer ' + mockAccessToken,
5858
};
59+
return mockApp.INTERNAL.getToken();
5960
});
6061

6162
afterEach(() => {
@@ -75,31 +76,31 @@ describe('FirebaseInstanceIdRequestHandler', () => {
7576
describe('deleteInstanceId', () => {
7677
const httpMethod = 'DELETE';
7778
const host = 'console.firebase.google.com';
78-
const port = 443;
7979
const path = `/v1/project/${projectId}/instanceId/test-iid`;
8080
const timeout = 10000;
8181

8282
it('should be fulfilled given a valid instance ID', () => {
8383
const expectedResult = {};
84-
85-
const stub = sinon.stub(HttpRequestHandler.prototype, 'sendRequest')
86-
.returns(Promise.resolve(expectedResult));
84+
const stub = sinon.stub(HttpClient.prototype, 'send')
85+
.resolves(utils.responseFrom(expectedResult));
8786
stubs.push(stub);
8887

8988
const requestHandler = new FirebaseInstanceIdRequestHandler(mockApp, projectId);
9089
return requestHandler.deleteInstanceId('test-iid')
9190
.then((result) => {
9291
expect(result).to.deep.equal(expectedResult);
93-
expect(stub).to.have.been.calledOnce.and.calledWith(
94-
host, port, path, httpMethod, undefined, expectedHeaders, timeout);
92+
expect(stub).to.have.been.calledOnce.and.calledWith({
93+
method: httpMethod,
94+
url: `https://${host}${path}`,
95+
headers: expectedHeaders,
96+
timeout,
97+
});
9598
});
9699
});
97100

98101
it('should throw for HTTP 404 errors', () => {
99-
const expectedResult = {statusCode: 404};
100-
101-
const stub = sinon.stub(HttpRequestHandler.prototype, 'sendRequest')
102-
.rejects(expectedResult);
102+
const stub = sinon.stub(HttpClient.prototype, 'send')
103+
.rejects(utils.errorFrom({}, 404));
103104
stubs.push(stub);
104105

105106
const requestHandler = new FirebaseInstanceIdRequestHandler(mockApp, projectId);
@@ -114,10 +115,8 @@ describe('FirebaseInstanceIdRequestHandler', () => {
114115
});
115116

116117
it('should throw for HTTP 409 errors', () => {
117-
const expectedResult = {statusCode: 409};
118-
119-
const stub = sinon.stub(HttpRequestHandler.prototype, 'sendRequest')
120-
.rejects(expectedResult);
118+
const stub = sinon.stub(HttpClient.prototype, 'send')
119+
.rejects(utils.errorFrom({}, 409));
121120
stubs.push(stub);
122121

123122
const requestHandler = new FirebaseInstanceIdRequestHandler(mockApp, projectId);
@@ -132,10 +131,9 @@ describe('FirebaseInstanceIdRequestHandler', () => {
132131
});
133132

134133
it('should throw for unexpected HTTP errors', () => {
135-
const expectedResult = {statusCode: 511};
136-
137-
const stub = sinon.stub(HttpRequestHandler.prototype, 'sendRequest')
138-
.rejects(expectedResult);
134+
const expectedResult = {error: 'test error'};
135+
const stub = sinon.stub(HttpClient.prototype, 'send')
136+
.rejects(utils.errorFrom(expectedResult, 511));
139137
stubs.push(stub);
140138

141139
const requestHandler = new FirebaseInstanceIdRequestHandler(mockApp, projectId);
@@ -145,7 +143,7 @@ describe('FirebaseInstanceIdRequestHandler', () => {
145143
})
146144
.catch((error) => {
147145
expect(error.code).to.equal('instance-id/api-error');
148-
expect(error.message).to.equal(JSON.stringify(expectedResult));
146+
expect(error.message).to.equal('test error');
149147
});
150148
});
151149
});

0 commit comments

Comments
 (0)