Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: switch to axios #182

Merged
merged 9 commits into from
Nov 24, 2017
Merged

chore: switch to axios #182

merged 9 commits into from
Nov 24, 2017

Conversation

JustinBeckwith
Copy link
Contributor

@JustinBeckwith JustinBeckwith commented Nov 18, 2017

Switching out request for axios, and making the API support callback and async styles.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Nov 18, 2017
@JustinBeckwith
Copy link
Contributor Author

@ofrobots all the tests are passing, this is ready for a pass. Apologies for the size of the review, couldn't really find a way to split it up.

@JustinBeckwith JustinBeckwith added this to the 1.0 milestone Nov 19, 2017
package.json Outdated
},
"devDependencies": {
"@types/jws": "^3.1.0",

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@@ -28,5 +34,5 @@ export abstract class AuthClient {
* Provides an alternative request
* implementations with auth credentials.
*/
abstract request(opts: request.Options): request.Request|void;
abstract request(opts: AxiosRequestConfig): Promise<BodyResponse>;

This comment was marked as spam.

This comment was marked as spam.

@JustinBeckwith JustinBeckwith changed the base branch from master to next November 21, 2017 02:09
request(opts: request.Options, callback?: BodyResponseCallback) {
request(opts: AxiosRequestConfig): AxiosPromise;
request(opts: AxiosRequestConfig, callback?: BodyResponseCallback): void;
request(opts: AxiosRequestConfig, callback?: BodyResponseCallback):

This comment was marked as spam.

This comment was marked as spam.


import {DefaultTransporter} from '../transporters';

import {Credentials} from './credentials';

export interface BodyResponse {
// tslint:disable-next-line no-any
body: any;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

body = JSON.parse(body as string);
} catch (err) {
/* no op */
private processError(e: AxiosError): RequestError {

This comment was marked as spam.

@JustinBeckwith
Copy link
Contributor Author

@ofrobots I think I covered all the feedback so far. Ready for the next wave :)

Copy link
Contributor

@ofrobots ofrobots left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial comments. Still want going through the rest of the file, but ran out of time.

try {
const url = this.opts.tokenUrl || Compute._GOOGLE_OAUTH2_TOKEN_URL;
// request for new token
const res = await this.transporter.request({url});

This comment was marked as spam.

This comment was marked as spam.

});
protected async refreshToken(refreshToken?: string|
null): Promise<GetTokenResponse> {
try {

This comment was marked as spam.

This comment was marked as spam.

@@ -19,6 +19,7 @@ export interface Credentials {
expiry_date?: number;
access_token?: string;
token_type?: string;
expires_in?: number;

This comment was marked as spam.

This comment was marked as spam.

getDefaultProjectId(callback: ProjectIdCallback) {
getDefaultProjectId(): Promise<string>;
getDefaultProjectId(callback: ProjectIdCallback): void;
getDefaultProjectId(callback?: ProjectIdCallback): Promise<string|null>|void {

This comment was marked as spam.

This comment was marked as spam.

return;
}
// environment variable
projectId = this.getProductionProjectId();

This comment was marked as spam.

This comment was marked as spam.

callback:
(error: Error|null, stdout: string, stderr: string|null) => void) {
exec('gcloud -q config list core/project --format=json', callback);
_getSDKDefaultProjectId():

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Copy link
Contributor

@ofrobots ofrobots left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still more to come

if (gce) {
// For GCE, just return a default ComputeClient. It will take care of
// the rest.
return {projectId: null, credential: new Compute()};

This comment was marked as spam.

This comment was marked as spam.

let client: UserRefreshClient|JWT;
if (!json) {
try {
if (!json) {

This comment was marked as spam.

This comment was marked as spam.

json: JWTInput,
callback?: (err: Error|null, client?: UserRefreshClient|JWT) => void) {
fromJSON(json: JWTInput): JWT|UserRefreshClient;
fromJSON(json: JWTInput, callback: CredentialCallback): void;

This comment was marked as spam.

This comment was marked as spam.

@@ -453,15 +486,23 @@ export class GoogleAuth {
* @param {function=} - Optional callback function
*/
fromAPIKey(
apiKey: string, callback?: (err?: Error|null, client?: JWT) => void) {
apiKey: string,

This comment was marked as spam.

This comment was marked as spam.

(err: Error|null, headers?: http.IncomingHttpHeaders|null) => void) {
const iat = Math.floor(new Date().getTime() / 1000);
const exp = iat + 3600; // 3600 seconds = 1 hour
getRequestMetadata(authURI: string): RequestMetadataResponse;

This comment was marked as spam.

This comment was marked as spam.

} else {
return newGToken.getToken((err2?: Error|null, token?: string|null) => {
return done(err2, {
async refreshToken(refreshToken?: string|null) {

This comment was marked as spam.

This comment was marked as spam.

});
});
}
} as Credentials;

This comment was marked as spam.

This comment was marked as spam.

done(new Error(
'The incoming JSON object does not contain a private_key field'));
return;
fromJSON(json: JWTInput, callback?: (err?: Error) => void): void {

This comment was marked as spam.

This comment was marked as spam.

done(err);
}

private async fromStreamAsync(inputStream: stream.Readable) {

This comment was marked as spam.

This comment was marked as spam.

@@ -215,37 +233,36 @@ export class JWT extends OAuth2Client {
* @param {function=} callback - Optional callback to be invoked after
* initialization.
*/
fromAPIKey(apiKey: string, callback?: (err: Error) => void) {
const done = callback || noop;
fromAPIKey(apiKey: string, callback?: (err?: Error) => void): void {

This comment was marked as spam.

This comment was marked as spam.

const res = await this.transporter.request<CredentialRequest>(
{method: 'POST', url, data});
const tokens = res.data as Credentials;
if (res.data && res.data.expires_in) {

This comment was marked as spam.

This comment was marked as spam.

} else {
return callback(null, this.credentials.access_token, null);
return {token: this.credentials.access_token, res: undefined};

This comment was marked as spam.

This comment was marked as spam.

const headers = {
Authorization: credentials.token_type + ' ' + tokens.access_token
};
return {headers, res: r ? r.res : null};

This comment was marked as spam.

This comment was marked as spam.

if (callback) {
callback(new Error(
'Must pass in a JSON object containing the user refresh token'));
try {

This comment was marked as spam.

This comment was marked as spam.

src/options.ts Outdated
@@ -0,0 +1,33 @@
import {AxiosRequestConfig} from 'axios';

This comment was marked as spam.

This comment was marked as spam.

@@ -643,7 +612,7 @@ describe('.fromAPIKey', () => {
it('should set the .apiKey property on the instance', (done) => {
jwt.fromAPIKey(KEY, (err) => {
assert.strictEqual(jwt.apiKey, KEY);
assert.strictEqual(err, null);
assert.equal(err, null);

This comment was marked as spam.

This comment was marked as spam.

@@ -855,7 +854,7 @@ describe('OAuth2 client', () => {
const auth = new GoogleAuth();
const oauth2client =
new auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
oauth2client.request(({} as request.OptionsWithUri), (err, result) => {
oauth2client.request(({} as AxiosRequestConfig), (err, result) => {

This comment was marked as spam.

This comment was marked as spam.

@@ -996,7 +995,10 @@ describe('OAuth2 client', () => {
oauth2client.credentials = {access_token: 'abc', refresh_token: 'abc'};
oauth2client.revokeCredentials((err, result) => {
assert.equal(err, null);
assert.equal(result.success, true);
assert(result);

This comment was marked as spam.

This comment was marked as spam.

assert(tokens.expiry_date >= now + (10 * 1000));
assert(tokens.expiry_date <= now + (15 * 1000));
assert(tokens);
if (tokens && tokens.expiry_date) {

This comment was marked as spam.

This comment was marked as spam.

const defaultUserAgentRE = 'google-api-nodejs-client/\\d+.\\d+.\\d+';
const transporter = new DefaultTransporter();

it('should set default client user agent if none is set', () => {
const opts = transporter.configure(({} as request.OptionsWithUrl));
const opts = transporter.configure(({} as AxiosRequestConfig));

This comment was marked as spam.

This comment was marked as spam.

@JustinBeckwith JustinBeckwith merged commit 98b2b1a into googleapis:next Nov 24, 2017
ofrobots pushed a commit to ofrobots/google-auth-library-nodejs that referenced this pull request Dec 1, 2017
ofrobots pushed a commit that referenced this pull request Jan 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants