<commit_before>
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError, timeout, map } from 'rxjs/operators';
import { DrupalConstants } from '../application/drupal-constants';
import { SystemConnection } from '../models/system';

/**
 * the main service is the basic http service of all other services "parent" that implements all the required request.
 * this service will add the headers automatically for each request it self
 */
@Injectable()
export class MainService {
  /**
   * entity type of the current service, the main service dont have anything but the other services must use it
   * for example "node, comment, user, system"
   */
  protected readonly entityType: string;

  /**
   * the main constractor of this service will inject the required services dynamically.
   * @param http basic http service
   * @param cookieService ngx-cookie service provider to save the cookies
   * @see https://angular.io/guide/dependency-injection
   * @see https://www.npmjs.com/package/ngx-cookie
   */
  constructor(protected httpClient: HttpClient) { }

  /**
   * structure a full entity for drupal request
   * @param entity the entity to be structured
   * @param ignoredFields fields to be ignored just like nid or uid or title
   * @param fieldLabels: the label for each field just like "{field_custom_field: 'value'}"
   * @param language language of the entity
   */
  structureEntity(entity: any, ignoredFields: string[], fieldLabels?: any[], language?: string): any {
    Object.keys(entity).forEach((key: string, index: number) => {
      if (ignoredFields.indexOf(key) === -1) {
        let fieldLabel;
        if (fieldLabels[key]) {
          fieldLabel = fieldLabels[key];
        }
        entity[key] = this.structureField(entity[key], fieldLabel, language);
      }
    });
    return entity;
  }

  /**
   * structure the field for drupal services request
   * @param value the field value
   * @param label field label name
   * @param language language of the field
   */
  structureField(value: any, label: string = 'value', language?: string) {
    if (!language) {
      language = DrupalConstants.Settings.language;
    }
    const item = {};
    if (this.isArray(value)) {
      const field_array = [];
      for (let i = 0, l = value.length; i < l; i++) {
        item[label] = value[i];
        field_array.push(item);
      }
      return {
        [language]: field_array
      };
    }
    if (value instanceof Date) {
      const obj = {
        value: {
          date: `${value.getFullYear()}-${value.getMonth() + 1}-` +
            `${value.getDate()} ${value.getHours()}:${value.getMinutes()}:${value.getSeconds()}`
        }
      };
      return {
        [language]: [
          obj
        ]
      };
    }
    // field value given with label(s) already built
    if (typeof value === 'object') {
      return {
        [language]: [
          value
        ]
      };
    }
    item[label] = value;
    return {
      [language]: [
        item
      ]
    };
  }

  /**
   * a getter to return the required headers for drupal
   * X-CSRF-Token - application token from services/session/connect
   * Content-Type - the type of the request content.
   * Accept - forcing drupal to return the response as a json object
   * @return object of the headers
   */
  get options(): any {
    const headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    };
    const token = this.getSavedVariable('token');
    if (token) {
      headers['X-CSRF-Token'] = token;
    }
    headers['Authentication'] = `${this.getSavedVariable('session_name')}=${this.getSavedVariable('sessid')}`;
    if (DrupalConstants.Settings.cookieHeader) {
      headers['Cookie'] = `${this.getSavedVariable('session_name')}=${this.getSavedVariable('sessid')}`;
    }
    const options = {
      headers: headers,
      withCredentials: true,
      reportProgress: true,
    };
    return options;
  }
<commit_msg>Add observe to httpClient options<commit_after>
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError, timeout, map } from 'rxjs/operators';
import { DrupalConstants } from '../application/drupal-constants';
import { SystemConnection } from '../models/system';

/**
 * the main service is the basic http service of all other services "parent" that implements all the required request.
 * this service will add the headers automatically for each request it self
 */
@Injectable()
export class MainService {
  /**
   * entity type of the current service, the main service dont have anything but the other services must use it
   * for example "node, comment, user, system"
   */
  protected readonly entityType: string;

  /**
   * the main constractor of this service will inject the required services dynamically.
   * @param http basic http service
   * @param cookieService ngx-cookie service provider to save the cookies
   * @see https://angular.io/guide/dependency-injection
   * @see https://www.npmjs.com/package/ngx-cookie
   */
  constructor(protected httpClient: HttpClient) { }

  /**
   * structure a full entity for drupal request
   * @param entity the entity to be structured
   * @param ignoredFields fields to be ignored just like nid or uid or title
   * @param fieldLabels: the label for each field just like "{field_custom_field: 'value'}"
   * @param language language of the entity
   */
  structureEntity(entity: any, ignoredFields: string[], fieldLabels?: any[], language?: string): any {
    Object.keys(entity).forEach((key: string, index: number) => {
      if (ignoredFields.indexOf(key) === -1) {
        let fieldLabel;
        if (fieldLabels[key]) {
          fieldLabel = fieldLabels[key];
        }
        entity[key] = this.structureField(entity[key], fieldLabel, language);
      }
    });
    return entity;
  }

  /**
   * structure the field for drupal services request
   * @param value the field value
   * @param label field label name
   * @param language language of the field
   */
  structureField(value: any, label: string = 'value', language?: string) {
    if (!language) {
      language = DrupalConstants.Settings.language;
    }
    const item = {};
    if (this.isArray(value)) {
      const field_array = [];
      for (let i = 0, l = value.length; i < l; i++) {
        item[label] = value[i];
        field_array.push(item);
      }
      return {
        [language]: field_array
      };
    }
    if (value instanceof Date) {
      const obj = {
        value: {
          date: `${value.getFullYear()}-${value.getMonth() + 1}-` +
            `${value.getDate()} ${value.getHours()}:${value.getMinutes()}:${value.getSeconds()}`
        }
      };
      return {
        [language]: [
          obj
        ]
      };
    }
    // field value given with label(s) already built
    if (typeof value === 'object') {
      return {
        [language]: [
          value
        ]
      };
    }
    item[label] = value;
    return {
      [language]: [
        item
      ]
    };
  }

  /**
   * a getter to return the required headers for drupal
   * X-CSRF-Token - application token from services/session/connect
   * Content-Type - the type of the request content.
   * Accept - forcing drupal to return the response as a json object
   * @return object of the headers
   */
  get options(): any {
    const headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    };
    const token = this.getSavedVariable('token');
    if (token) {
      headers['X-CSRF-Token'] = token;
    }
    headers['Authentication'] = `${this.getSavedVariable('session_name')}=${this.getSavedVariable('sessid')}`;
    if (DrupalConstants.Settings.cookieHeader) {
      headers['Cookie'] = `${this.getSavedVariable('session_name')}=${this.getSavedVariable('sessid')}`;
    }
    const options = {
      headers: headers,
      withCredentials: true,
      reportProgress: true,
      observe: 'events'
    };
    return options;
  }

{'fraction_non_alphanumeric': 0.12588869821034568, 'fraction_numerical': 0.0007354743809757293, 'mean_word_length': 2.7375171781951444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '3953375', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2311, 'n_words': 714}