<commit_before>/**
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * 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.
 */

'use strict';
var lodash = require('lodash');
var isFunction = lodash.isFunction;
var isString = lodash.isString;
var GoogleAuth = require('google-auth-library');
var RetryTracker = require('../classes/retry-tracker.js');

/**
 * The AuthClient constructor intializes several properties on the AuthClient
 * instance, creates a new GoogleAuth authorization factory to facilitate
 * communication with the Google error reporting API and then initializes the
 * GoogleAuth client.
 * @param {String} projectId - the project id of the application
 * @param {Boolean} shouldReportErrors - whether or not the client should
 *  attempt to post errors to the API, optioanl -- used for simple API key
 *  authentication
 * @param {String} key - the API key to use to authenticate against the service
 * @class AuthClient
 * @classdesc The AuthClient class provides an abstraction layer for posting
 *  errors to the Google Error Reporting API. The AuthClient class interfaces
 *  with the RetryTracker class to provide automatic request-retry, queueing and
 *  exponential retry backoff.
 * @property {GoogleAuthClient} _authClient - an authorized GoogleAuth client
 *  instance
 * @property {Boolean} _hasInited - Indicator denoting whether or not the
 *  underlying GoogleAuth client has been authorized and is ready to interact
 *  with the service
 * @property {Boolean} _isRequesting - Indicator denoting whether or not the
 *  client is currently making a request against the service
 * @property {Array} _requestQueue - The pending request queue, requests that
 *  will be executed against the service
 * @property {GoogleAuth} _authFactory - The GoogleAuth factory for creating
 *  authorized clients
 * @property {String} _key - An optional key used for simple authentication
 * @property {String} _projectId - The project Id used to request against the
 *  service
 * @property {Boolean} _shouldReportErrors - Indicator denoting whether or not
 *  the client should actually attempt requests against the service
 */
function AuthClient(projectId, shouldReportErrors, key) {

  this._authClient = null;
  this._hasInited = false;
  this._isRequesting = false;
  this._requestQueue = [];
  this._authFactory = new GoogleAuth();

  this._key = isString(key) ? key : null;
  this._projectId = projectId;
  this._shouldReportErrors = shouldReportErrors;

  this._getApplicationDefaultCredentials();
}

/**
 * Intializes the GoogleAuth client using the default application,
 * credentials method and authorizes it against the service and
 * creates scoped credentials, if necessary, for the client.
 * @function _getApplicationDefaultCredentials
 * @returns {Undefined} - does not return a value
 */
AuthClient.prototype._getApplicationDefaultCredentials = function() {
  var self = this;

  this._authFactory.getApplicationDefault(function(err, authClient) {
    var scopes = [ 'https://www.googleapis.com/auth/cloud-platform' ];

    if (err) {

      return;
    }

    if (authClient.createScopedRequired && authClient.createScopedRequired()) {

      self._authClient = authClient.createScoped(scopes);
    } else {

      self._authClient = authClient;
    }

    self._hasInited = true;
  });
};

/**
 * Used for temporary interfacing with the Gated Error Creation URL. Will
 * be removed in favor of default auth when the API goes live. Creates the
 * url to create errors in the error API.
 * @function _generateErrorCreationUrl
 * @returns {String} - the url to create errors for the error reporting API
 */
AuthClient.prototype._generateErrorCreationUrl = function() {
  var returnValue = [
    'https://clouderrorreporting.googleapis.com/v1beta1/projects',
    this._projectId, 'events'
  ].join('/');

  if (isString(this._key)) {

    returnValue += [ '?key', this._key ].join('=');
  }

  return returnValue;
};<commit_msg>update api url (#3)<commit_after>/**
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * 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.
 */

'use strict';
var lodash = require('lodash');
var isFunction = lodash.isFunction;
var isString = lodash.isString;
var GoogleAuth = require('google-auth-library');
var RetryTracker = require('../classes/retry-tracker.js');

/**
 * The AuthClient constructor intializes several properties on the AuthClient
 * instance, creates a new GoogleAuth authorization factory to facilitate
 * communication with the Google error reporting API and then initializes the
 * GoogleAuth client.
 * @param {String} projectId - the project id of the application
 * @param {Boolean} shouldReportErrors - whether or not the client should
 *  attempt to post errors to the API, optioanl -- used for simple API key
 *  authentication
 * @param {String} key - the API key to use to authenticate against the service
 * @class AuthClient
 * @classdesc The AuthClient class provides an abstraction layer for posting
 *  errors to the Google Error Reporting API. The AuthClient class interfaces
 *  with the RetryTracker class to provide automatic request-retry, queueing and
 *  exponential retry backoff.
 * @property {GoogleAuthClient} _authClient - an authorized GoogleAuth client
 *  instance
 * @property {Boolean} _hasInited - Indicator denoting whether or not the
 *  underlying GoogleAuth client has been authorized and is ready to interact
 *  with the service
 * @property {Boolean} _isRequesting - Indicator denoting whether or not the
 *  client is currently making a request against the service
 * @property {Array} _requestQueue - The pending request queue, requests that
 *  will be executed against the service
 * @property {GoogleAuth} _authFactory - The GoogleAuth factory for creating
 *  authorized clients
 * @property {String} _key - An optional key used for simple authentication
 * @property {String} _projectId - The project Id used to request against the
 *  service
 * @property {Boolean} _shouldReportErrors - Indicator denoting whether or not
 *  the client should actually attempt requests against the service
 */
function AuthClient(projectId, shouldReportErrors, key) {

  this._authClient = null;
  this._hasInited = false;
  this._isRequesting = false;
  this._requestQueue = [];
  this._authFactory = new GoogleAuth();

  this._key = isString(key) ? key : null;
  this._projectId = projectId;
  this._shouldReportErrors = shouldReportErrors;

  this._getApplicationDefaultCredentials();
}

/**
 * Intializes the GoogleAuth client using the default application,
 * credentials method and authorizes it against the service and
 * creates scoped credentials, if necessary, for the client.
 * @function _getApplicationDefaultCredentials
 * @returns {Undefined} - does not return a value
 */
AuthClient.prototype._getApplicationDefaultCredentials = function() {
  var self = this;

  this._authFactory.getApplicationDefault(function(err, authClient) {
    var scopes = [ 'https://www.googleapis.com/auth/cloud-platform' ];

    if (err) {

      return;
    }

    if (authClient.createScopedRequired && authClient.createScopedRequired()) {

      self._authClient = authClient.createScoped(scopes);
    } else {

      self._authClient = authClient;
    }

    self._hasInited = true;
  });
};

/**
 * Used for temporary interfacing with the Gated Error Creation URL. Will
 * be removed in favor of default auth when the API goes live. Creates the
 * url to create errors in the error API.
 * @function _generateErrorCreationUrl
 * @returns {String} - the url to create errors for the error reporting API
 */
AuthClient.prototype._generateErrorCreationUrl = function() {
  var returnValue = [
    'https://clouderrorreporting.googleapis.com/v1beta1/projects',
    this._projectId, 'events:report'
  ].join('/');

  if (isString(this._key)) {

    returnValue += [ '?key', this._key ].join('=');
  }

  return returnValue;
};
{'fraction_non_alphanumeric': 0.08336126941557716, 'fraction_numerical': 0.0023466309084813944, 'mean_word_length': 4.137772675086108, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5806683', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2298, 'n_words': 945}