<commit_before>import * as _ from 'lodash';
import { Inject, Optional, OpaqueToken, Provider, Injectable } from '@angular/core';

import { Http, URLSearchParams, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

import { errorHandlerToken, IErrorHandlerService } from '../errorHandler/errorHandler.service';

export const interceptorToken: OpaqueToken = new OpaqueToken('Custom interceptor for http requests');

export interface IHttpInterceptor {
	handleSuccess?: { (Response): any };
	handleError?: { (Response): Observable<any> };
}

export interface IHttpUtility {
	buildQueryString(params: any): URLSearchParams;

	get<TDataType>(endpoint: string, params?: any): Observable<TDataType>;
	post<TDataType>(endpoint: string, data: any): Observable<TDataType>;
	put<TDataType>(endpoint: string, data: any): Observable<TDataType>;
	delete(endpoint: string, params?: any): Observable<void>;
}

@Injectable()
export class HttpUtility implements IHttpUtility {
	private http: Http;
	private interceptor: IHttpInterceptor;

	constructor( @Inject(Http) http: Http
			, @Optional() @Inject(interceptorToken) interceptor: IHttpInterceptor) {
		this.http = http;
		this.interceptor = this.setDefaults(interceptor);
	}

	buildQueryString(params: any): URLSearchParams {
		const searchParams: URLSearchParams = new URLSearchParams();
		_.each(params, (param: any, key: string): void => {
			searchParams.set(key, param);
		});
		return searchParams;
	}

	get<TDataType>(endpoint: string, params?: any): Observable<TDataType> {
		return this.http.get(endpoint, { search: this.buildQueryString(params) })
			.catch(this.handleError.bind(this))
			.map(this.handleSuccess.bind(this))
			.map((response: Response): TDataType => response.json());
	}

	post<TDataType>(endpoint: string, data: any): Observable<TDataType> {
		const headers: Headers = new Headers({
			'Content-Type': 'application/json',
		});
		const options: RequestOptions = new RequestOptions({ headers });

		return this.http.post(endpoint, JSON.stringify(data), options)
			.catch(this.handleError.bind(this))
			.map(this.handleSuccess.bind(this))
			.map((response: Response) => response.json());<commit_msg>Default the params to an empty string<commit_after>import * as _ from 'lodash';
import { Inject, Optional, OpaqueToken, Provider, Injectable } from '@angular/core';

import { Http, URLSearchParams, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

import { errorHandlerToken, IErrorHandlerService } from '../errorHandler/errorHandler.service';

export const interceptorToken: OpaqueToken = new OpaqueToken('Custom interceptor for http requests');

export interface IHttpInterceptor {
	handleSuccess?: { (Response): any };
	handleError?: { (Response): Observable<any> };
}

export interface IHttpUtility {
	buildQueryString(params: any): URLSearchParams;

	get<TDataType>(endpoint: string, params?: any): Observable<TDataType>;
	post<TDataType>(endpoint: string, data: any): Observable<TDataType>;
	put<TDataType>(endpoint: string, data: any): Observable<TDataType>;
	delete(endpoint: string, params?: any): Observable<void>;
}

@Injectable()
export class HttpUtility implements IHttpUtility {
	private http: Http;
	private interceptor: IHttpInterceptor;

	constructor( @Inject(Http) http: Http
			, @Optional() @Inject(interceptorToken) interceptor: IHttpInterceptor) {
		this.http = http;
		this.interceptor = this.setDefaults(interceptor);
	}

	buildQueryString(params: any): URLSearchParams {
		const searchParams: URLSearchParams = new URLSearchParams();
		_.each(params, (param: any, key: string): void => {
			searchParams.set(key, param || '');
		});
		return searchParams;
	}

	get<TDataType>(endpoint: string, params?: any): Observable<TDataType> {
		return this.http.get(endpoint, { search: this.buildQueryString(params) })
			.catch(this.handleError.bind(this))
			.map(this.handleSuccess.bind(this))
			.map((response: Response): TDataType => response.json());
	}

	post<TDataType>(endpoint: string, data: any): Observable<TDataType> {
		const headers: Headers = new Headers({
			'Content-Type': 'application/json',
		});
		const options: RequestOptions = new RequestOptions({ headers });

		return this.http.post(endpoint, JSON.stringify(data), options)
			.catch(this.handleError.bind(this))
			.map(this.handleSuccess.bind(this))
			.map((response: Response) => response.json());
{'fraction_non_alphanumeric': 0.13945278022947927, 'fraction_numerical': 0.0, 'mean_word_length': 6.027906976744186, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5648994', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1352, 'n_words': 325}