<commit_before>import {Inject} from 'angular2/core';
import {Http, Headers} from 'angular2/http';
import {Cookie} from '../cookie';

/**
 * API Class
 */
export class Client {
	base : string = "/";
	cookie : Cookie = new Cookie();
	constructor(public http : Http){
	}

	private buildParams(object : Object){
		return Object.keys(object).map((k) => {
			return encodeURIComponent(k) + "=" + encodeURIComponent(object[k]);
		}).join('&');
	}

	/**
	 * Build the options
	 */
	private buildOptions(options : Object){
		var XSRF_TOKEN = this.cookie.get('XSRF-TOKEN');
		var headers = new Headers();
		headers.append('X-XSRF-TOKEN', XSRF_TOKEN);
		var Objecti : any = Object;
		return Objecti.assign(options, {
					headers: headers,
					cache: true
				});
	}

	/**
	 * Return a GET request
	 */
	get(endpoint : string, data : Object = {}, options: Object = {}){
		var self = this;
		endpoint += "?" + this.buildParams(data);
		return new Promise((resolve, reject) => {
			self.http.get(
					self.base + endpoint,
					this.buildOptions(options)
				)
				.subscribe(
          res => {
  					var data = res.json();
  					if(data.status != 'success')
  						return reject(data);

  					return resolve(data);
				  },
          err => {
            if(err.status == 401 && err.json().loggedin === false){
              window.location.href="/login";
              return reject(err);
            }
            return reject(err);
          });
		});
	}

	/**
	 * Return a POST request
	 */
	post(endpoint : string, data : Object = {}, options: Object = {}){
		var self = this;
		return new Promise((resolve, reject) => {
			self.http.post(
					self.base + endpoint,
					JSON.stringify(data),
					this.buildOptions(options)
				)
				.subscribe(
          res => {
						var data = res.json();
						if(data.status != 'success')
							return reject(data);

						return resolve(data);
				  },
          err => {
            if(err.status == 401 && err.json().loggedin === false){
							window.location.href="/login";
							return reject(err);
						}
						if(err.status != 200){
							return reject(err.json());
						}
          });
		});
	}

	/**
	 * Return a PUT request
	 */
	put(endpoint : string, data : Object = {}, options: Object = {}){
		var self = this;
		return new Promise((resolve, reject) => {
			self.http.put(
					self.base + endpoint,
					JSON.stringify(data),
					this.buildOptions(options)
				)
				.subscribe(
          res => {
						var data = res.json();
						if(data.status != 'success')
							return reject(data);

						return resolve(data);
  				},
          err => {
            if(err.status == 401 && err.json().loggedin === false){
              window.location.href="/login";
              return reject(err);
            }
            if(err.status != 200){
              return reject(err.json());
            }
          });
		});
	}

	/**
	 * Return a DELETE request
	 */
	delete(endpoint : string, data : Object = {}, options: Object = {}){
		var self = this;
		return new Promise((resolve, reject) => {
			self.http.delete(
					self.base + endpoint,
					this.buildOptions(options)
				)
				.subscribe(
          res => {
						var data = res.json();
						if(data.status != 'success')
							return reject(data);

						return resolve(data);
				  },
          err => {<commit_msg>(fix) Client crashing when no data is returned<commit_after>import {Inject} from 'angular2/core';
import {Http, Headers} from 'angular2/http';
import {Cookie} from '../cookie';

/**
 * API Class
 */
export class Client {
	base : string = "/";
	cookie : Cookie = new Cookie();
	constructor(public http : Http){
	}

	private buildParams(object : Object){
		return Object.keys(object).map((k) => {
			return encodeURIComponent(k) + "=" + encodeURIComponent(object[k]);
		}).join('&');
	}

	/**
	 * Build the options
	 */
	private buildOptions(options : Object){
		var XSRF_TOKEN = this.cookie.get('XSRF-TOKEN');
		var headers = new Headers();
		headers.append('X-XSRF-TOKEN', XSRF_TOKEN);
		var Objecti : any = Object;
		return Objecti.assign(options, {
					headers: headers,
					cache: true
				});
	}

	/**
	 * Return a GET request
	 */
	get(endpoint : string, data : Object = {}, options: Object = {}){
		var self = this;
		endpoint += "?" + this.buildParams(data);
		return new Promise((resolve, reject) => {
			self.http.get(
					self.base + endpoint,
					this.buildOptions(options)
				)
				.subscribe(
          res => {
  					var data = res.json();
  					if(!data || data.status != 'success')
  						return reject(data);

  					return resolve(data);
				  },
          err => {
            if(err.status == 401 && err.json().loggedin === false){
              window.location.href="/login";
              return reject(err);
            }
            return reject(err);
          });
		});
	}

	/**
	 * Return a POST request
	 */
	post(endpoint : string, data : Object = {}, options: Object = {}){
		var self = this;
		return new Promise((resolve, reject) => {
			self.http.post(
					self.base + endpoint,
					JSON.stringify(data),
					this.buildOptions(options)
				)
				.subscribe(
          res => {
						var data = res.json();
						if(!data || data.status != 'success')
							return reject(data);

						return resolve(data);
				  },
          err => {
            if(err.status == 401 && err.json().loggedin === false){
							window.location.href="/login";
							return reject(err);
						}
						if(err.status != 200){
							return reject(err.json());
						}
          });
		});
	}

	/**
	 * Return a PUT request
	 */
	put(endpoint : string, data : Object = {}, options: Object = {}){
		var self = this;
		return new Promise((resolve, reject) => {
			self.http.put(
					self.base + endpoint,
					JSON.stringify(data),
					this.buildOptions(options)
				)
				.subscribe(
          res => {
						var data = res.json();
						if(!data || data.status != 'success')
							return reject(data);

						return resolve(data);
  				},
          err => {
            if(err.status == 401 && err.json().loggedin === false){
              window.location.href="/login";
              return reject(err);
            }
            if(err.status != 200){
              return reject(err.json());
            }
          });
		});
	}

	/**
	 * Return a DELETE request
	 */
	delete(endpoint : string, data : Object = {}, options: Object = {}){
		var self = this;
		return new Promise((resolve, reject) => {
			self.http.delete(
					self.base + endpoint,
					this.buildOptions(options)
				)
				.subscribe(
          res => {
						var data = res.json();
						if(!data || data.status != 'success')
							return reject(data);

						return resolve(data);
				  },
          err => {
{'fraction_non_alphanumeric': 0.1630354231510301, 'fraction_numerical': 0.005039276715577293, 'mean_word_length': 2.061705989110708, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '1184214', 'n_tokens_mistral': 2560, 'n_tokens_neox': 2052, 'n_words': 467}