<commit_before>import {Injectable, Inject} from '@angular/core';
import {
  Headers, RequestOptionsArgs, RequestMethod, Request, RequestOptions, Http, Response,
  ResponseContentType
} from "@angular/http";
import {Observable} from "rxjs";
import {HttpQueueService} from "../http-queue/http-queue.service";
import {ErrorHandlerService} from "../../errorhandler/error-handler.service";
import {TokenService} from "../../authentication/token.service";

@Injectable()
export class RestService {

  constructor(private httpQueueu: HttpQueueService,
              private errorHandler: ErrorHandlerService,
              private http: Http,
              private tokenService: TokenService) {}

  /*
   * @description: build request options
   */
  private buildRequestOptionArgs(url: string,
                                  method: RequestMethod = RequestMethod.Get,
                                  requestOptions: RequestOptionsArgs = {},
                                  authentication: boolean = false,
                                  payload?: any): RequestOptionsArgs {
    requestOptions.headers = new Headers(requestOptions.headers);
    requestOptions.headers.append('Content-Type', 'application/json; charset=UTF-8');
    requestOptions.headers.append('Accept', 'application/json; charset=UTF-8');
    if(authentication) {
      requestOptions.headers.append( 'Authorization', this.tokenService.getTokenHeader().Authorization );
    }
    requestOptions.method = method;
    requestOptions.url = url;
    requestOptions.body = JSON.stringify(payload);
    return requestOptions;
  }

  /*
   * @description: Create GET http-request
   */
  get(url: string, authenticationRequired?: boolean, requestOptions?: RequestOptionsArgs): Observable<any> {
    const opts = this.buildRequestOptionArgs(url, RequestMethod.Get, requestOptions, authenticationRequired);
    return this.doRequest(new Request(new RequestOptions(opts)));
  }

  /*
   * @description:Create PUT http-request
   */
  put(url: string, payload: any, authenticationRequired?: boolean, requestOptions?: RequestOptionsArgs): Observable<any> {
    const opts = this.buildRequestOptionArgs(url, RequestMethod.Put, requestOptions, authenticationRequired, payload );
    return this.doRequest(new Request(new RequestOptions(opts)));
  }

  /*
   * @description: Create POST http-request
   */
  post(url: string, authenticationRequired?: boolean, requestOptions?: RequestOptionsArgs): Observable<any> {
    const opts = this.buildRequestOptionArgs(url, RequestMethod.Post, requestOptions, authenticationRequired);
    return this.doRequest(new Request(new RequestOptions(opts)));
  }

  /*
   * @description:Create DELETE http-request
   */
  delete(url: string, authenticationRequired?: boolean): Observable<any> {
    const opts = this.buildRequestOptionArgs(url, RequestMethod.Delete, {}, authenticationRequired);
    return this.doRequest(new Request(new RequestOptions(opts)));
  }

  /*
   * @description:Fire the actual http request by given request object
   * and keep track how many request are in queue currently
   */
  private doRequest(request: Request): Observable<any> {
    this.httpQueueu.increment();
    return this.http.request(request).map( (response:Response) => {
        let resp: any;

        // handle response by expected responsetype<commit_msg>add payload to put-methods<commit_after>import {Injectable, Inject} from '@angular/core';
import {
  Headers, RequestOptionsArgs, RequestMethod, Request, RequestOptions, Http, Response,
  ResponseContentType
} from "@angular/http";
import {Observable} from "rxjs";
import {HttpQueueService} from "../http-queue/http-queue.service";
import {ErrorHandlerService} from "../../errorhandler/error-handler.service";
import {TokenService} from "../../authentication/token.service";

@Injectable()
export class RestService {

  constructor(private httpQueueu: HttpQueueService,
              private errorHandler: ErrorHandlerService,
              private http: Http,
              private tokenService: TokenService) {}

  /*
   * @description: build request options
   */
  private buildRequestOptionArgs(url: string,
                                  method: RequestMethod = RequestMethod.Get,
                                  requestOptions: RequestOptionsArgs = {},
                                  authentication: boolean = false,
                                  payload?: any): RequestOptionsArgs {
    requestOptions.headers = new Headers(requestOptions.headers);
    requestOptions.headers.append('Content-Type', 'application/json; charset=UTF-8');
    requestOptions.headers.append('Accept', 'application/json; charset=UTF-8');
    if(authentication) {
      requestOptions.headers.append( 'Authorization', this.tokenService.getTokenHeader().Authorization );
    }
    requestOptions.method = method;
    requestOptions.url = url;
    requestOptions.body = JSON.stringify(payload);
    return requestOptions;
  }

  /*
   * @description: Create GET http-request
   */
  get(url: string, authenticationRequired?: boolean, requestOptions?: RequestOptionsArgs): Observable<any> {
    const opts = this.buildRequestOptionArgs(url, RequestMethod.Get, requestOptions, authenticationRequired);
    return this.doRequest(new Request(new RequestOptions(opts)));
  }

  /*
   * @description: Create PUT http-request
   */
  put(url: string, payload: any, authenticationRequired?: boolean, requestOptions?: RequestOptionsArgs): Observable<any> {
    const opts = this.buildRequestOptionArgs(url, RequestMethod.Put, requestOptions, authenticationRequired, payload );
    return this.doRequest(new Request(new RequestOptions(opts)));
  }

  /*
   * @description: Create POST http-request
   */
  post(url: string, payload: any, authenticationRequired?: boolean, requestOptions?: RequestOptionsArgs): Observable<any> {
    const opts = this.buildRequestOptionArgs(url, RequestMethod.Post, requestOptions, authenticationRequired, payload);
    return this.doRequest(new Request(new RequestOptions(opts)));
  }

  /*
   * @description:Create DELETE http-request
   */
  delete(url: string, authenticationRequired?: boolean): Observable<any> {
    const opts = this.buildRequestOptionArgs(url, RequestMethod.Delete, {}, authenticationRequired);
    return this.doRequest(new Request(new RequestOptions(opts)));
  }

  /*
   * @description:Fire the actual http request by given request object
   * and keep track how many request are in queue currently
   */
  private doRequest(request: Request): Observable<any> {
    this.httpQueueu.increment();
    return this.http.request(request).map( (response:Response) => {
        let resp: any;

        // handle response by expected responsetype
{'fraction_non_alphanumeric': 0.11092936802973978, 'fraction_numerical': 0.0005947955390334573, 'mean_word_length': 3.753356890459364, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3397757', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1671, 'n_words': 414}