<commit_before>/*!
 * Copyright 2015 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.
 */

/*!
 * @module common/service-object
 */

import {promisifyAll} from '@google-cloud/promisify';
import * as arrify from 'arrify';
import {EventEmitter} from 'events';
import * as extend from 'extend';
import * as is from 'is';
import * as r from 'request';

import {Service, StreamRequestOptions} from '.';
import {ApiError, BodyResponseCallback, DecorateRequestOptions, util} from './util';

export interface Interceptor {
  // tslint:disable-next-line:no-any
  [index: string]: any;
}

export interface Metadata {
  error?: Error;
  done?: boolean;
}

export type GetMetadataCallback =
    (err: Error|null, metadata?: Metadata|null, apiResponse?: r.Response) =>
        void;

export interface ExistsCallback {
  (err: Error|null, exists?: boolean): void;
}

export interface ServiceObjectConfig {
  /**
   * The base URL to make API requests to.
   */
  baseUrl?: string;

  /**
   * The method which creates this object.
   */
  createMethod?: Function;

  /**
   * The identifier of the object. For example, the name of a Storage bucket or
   * Pub/Sub topic.
   */
  id?: string;

  /**
   * A map of each method name that should be inherited.
   */
  methods?: Methods;

  /**
   * The parent service instance. For example, an instance of Storage if the
   * object is Bucket.
   */
  parent: Service;
}

export interface Methods {
  [methodName: string]: {reqOpts?: r.OptionsWithUri}|boolean;
}

export interface CreateOptions {}

export interface InstanceResponseCallback {
  (err: ApiError|null, instance?: ServiceObject|null,
   apiResponse?: r.Response): void;
}

export interface DeleteCallback {
  (err: Error|null, apiResponse?: r.Response): void;
}

export interface GetConfig {
  /**
   * Create the object if it doesn't already exist.
   */
  autoCreate?: boolean;
}

/**
 * ServiceObject is a base class, meant to be inherited from by a "service
 * object," like a BigQuery dataset or Storage bucket.
 *
 * Most of the time, these objects share common functionality; they can be
 * created or deleted, and you can get or set their metadata.
 *
 * By inheriting from this class, a service object will be extended with these
 * shared behaviors. Note that any method can be overridden when the service
 * object requires specific behavior.
 */
class ServiceObject extends EventEmitter {
  // tslint:disable-next-line:no-any
  metadata: any;
  baseUrl?: string;
  protected parent: Service;
  protected id?: string;
  private createMethod?: Function;
  protected methods: Methods;
  protected interceptors: Interceptor[];
  // tslint:disable-next-line:variable-name
  protected Promise?: PromiseConstructor;
  // tslint:disable-next-line:no-any
  [index: string]: any;

  /*
   * @constructor
   * @alias module:common/service-object
   *
   * @private
   *
   * @param {object} config - Configuration object.
   * @param {string} config.baseUrl - The base URL to make API requests to.
   * @param {string} config.createMethod - The method which creates this object.<commit_msg>allow ServiceObject`s parent to be an ServiceObject (#212)<commit_after>/*!
 * Copyright 2015 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.
 */

/*!
 * @module common/service-object
 */

import {promisifyAll} from '@google-cloud/promisify';
import * as arrify from 'arrify';
import {EventEmitter} from 'events';
import * as extend from 'extend';
import * as is from 'is';
import * as r from 'request';

import {Service, StreamRequestOptions} from '.';
import {ApiError, BodyResponseCallback, DecorateRequestOptions, util} from './util';

export interface Interceptor {
  // tslint:disable-next-line:no-any
  [index: string]: any;
}

export interface Metadata {
  error?: Error;
  done?: boolean;
}

export type GetMetadataCallback =
    (err: Error|null, metadata?: Metadata|null, apiResponse?: r.Response) =>
        void;

export interface ExistsCallback {
  (err: Error|null, exists?: boolean): void;
}

export interface ServiceObjectConfig {
  /**
   * The base URL to make API requests to.
   */
  baseUrl?: string;

  /**
   * The method which creates this object.
   */
  createMethod?: Function;

  /**
   * The identifier of the object. For example, the name of a Storage bucket or
   * Pub/Sub topic.
   */
  id?: string;

  /**
   * A map of each method name that should be inherited.
   */
  methods?: Methods;

  /**
   * The parent service instance. For example, an instance of Storage if the
   * object is Bucket.
   */
  parent: Service|ServiceObject;
}

export interface Methods {
  [methodName: string]: {reqOpts?: r.OptionsWithUri}|boolean;
}

export interface CreateOptions {}

export interface InstanceResponseCallback {
  (err: ApiError|null, instance?: ServiceObject|null,
   apiResponse?: r.Response): void;
}

export interface DeleteCallback {
  (err: Error|null, apiResponse?: r.Response): void;
}

export interface GetConfig {
  /**
   * Create the object if it doesn't already exist.
   */
  autoCreate?: boolean;
}

/**
 * ServiceObject is a base class, meant to be inherited from by a "service
 * object," like a BigQuery dataset or Storage bucket.
 *
 * Most of the time, these objects share common functionality; they can be
 * created or deleted, and you can get or set their metadata.
 *
 * By inheriting from this class, a service object will be extended with these
 * shared behaviors. Note that any method can be overridden when the service
 * object requires specific behavior.
 */
class ServiceObject extends EventEmitter {
  // tslint:disable-next-line:no-any
  metadata: any;
  baseUrl?: string;
  protected parent: Service|ServiceObject;
  protected id?: string;
  private createMethod?: Function;
  protected methods: Methods;
  protected interceptors: Interceptor[];
  // tslint:disable-next-line:variable-name
  Promise?: PromiseConstructor;
  // tslint:disable-next-line:no-any
  [index: string]: any;

  /*
   * @constructor
   * @alias module:common/service-object
   *
   * @private
   *
   * @param {object} config - Configuration object.
   * @param {string} config.baseUrl - The base URL to make API requests to.
   * @param {string} config.createMethod - The method which creates this object.
{'fraction_non_alphanumeric': 0.1027182078950963, 'fraction_numerical': 0.002595273869689933, 'mean_word_length': 3.7086816720257234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1165482', 'n_tokens_mistral': 2055, 'n_tokens_neox': 2030, 'n_words': 809}