my-test123
Version:
A planner front-end for Fabric8.
179 lines • 7.46 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { Injectable } from '@angular/core';
import { Http, RequestOptions, Headers } from '@angular/http';
import { AuthenticationService } from 'ngx-login-client';
import { Observable } from 'rxjs/Observable';
/**
* This class is an abstract of Angular2 HTTP
* Extended from Angular2 HTTP
*
* Purpose:
* Error handling on API services
* To implement a general retry logic
*/
var HttpService = /** @class */ (function (_super) {
__extends(HttpService, _super);
function HttpService(backend, options, auth) {
var _this = _super.call(this, backend, options) || this;
_this.headers = new Headers({ 'Content-Type': 'application/json' });
if (auth && auth.getToken() != null) {
_this.headers.set('Authorization', "Bearer " + auth.getToken());
}
return _this;
// We are not dealing with cache so random Etag value is sent
// this.headers.set('If-None-Match', 'somerandomEtagValue');
}
HttpService.prototype.setHeaders = function (options) {
var _this = this;
Object.entries(options).forEach(function (_a) {
var key = _a[0], value = _a[1];
_this.headers.set(key, value);
});
};
HttpService.prototype.get = function (url, options) {
if (options === void 0) { options = {}; }
console.log('GET request initiated');
console.log('URL - ', url);
console.log('Options - ', options);
this.setHeaders(options);
var retryCount = 1;
return _super.prototype.get.call(this, url, { headers: this.headers })
.retryWhen(function (attempts) {
console.log('retryWhen callback');
var count = 0;
return attempts.flatMap(function (error) {
if (error.status == 0) {
console.log('########### Now offline #############', error);
return Observable.timer(++count * 1000);
}
else if (error.status == 500 || error.status == 401) {
return ++count >= 3 ? Observable.throw(error) : Observable.timer(1000);
}
else {
return Observable.throw(error);
}
});
});
};
HttpService.prototype.post = function (url, body, options) {
if (options === void 0) { options = {}; }
options = Object.assign(options, this.options);
console.log('POST request initiated');
console.log('URL - ', url);
console.log('Body - ', body);
console.log('Options - ', options);
this.setHeaders(options);
return _super.prototype.post.call(this, url, body, { headers: this.headers })
.retryWhen(function (attempts) {
console.log('retryWhen callback');
var count = 0;
return attempts.flatMap(function (error) {
if (error.status == 0) {
console.log('########### Now offline #############', error);
return Observable.timer(++count * 1000);
}
else if (error.status == 500 || error.status == 401) {
return ++count >= 3 ? Observable.throw(error) : Observable.timer(1000);
}
else {
return Observable.throw(error);
}
});
});
};
HttpService.prototype.put = function (url, body, options) {
if (options === void 0) { options = {}; }
console.log('PUT request initiated');
console.log('URL - ', url);
console.log('Body - ', body);
console.log('Options - ', options);
this.setHeaders(options);
return _super.prototype.put.call(this, url, body, { headers: this.headers })
.retryWhen(function (attempts) {
console.log('retryWhen callback');
var count = 0;
return attempts.flatMap(function (error) {
if (error.status == 0) {
console.log('########### Now offline #############', error);
return Observable.timer(++count * 1000);
}
else if (error.status == 500 || error.status == 401) {
return ++count >= 3 ? Observable.throw(error) : Observable.timer(1000);
}
else {
return Observable.throw(error);
}
});
});
};
HttpService.prototype.patch = function (url, body, options) {
if (options === void 0) { options = {}; }
console.log('PATCH request initiated');
console.log('URL - ', url);
console.log('Body - ', body);
console.log('Options - ', options);
this.setHeaders(options);
return _super.prototype.patch.call(this, url, body, { headers: this.headers })
.retryWhen(function (attempts) {
console.log('retryWhen callback');
var count = 0;
return attempts.flatMap(function (error) {
if (error.status == 0) {
console.log('########### Now offline #############', error);
return Observable.timer(++count * 1000);
}
else if (error.status == 500 || error.status == 401) {
return ++count >= 3 ? Observable.throw(error) : Observable.timer(1000);
}
else {
return Observable.throw(error);
}
});
});
};
HttpService.prototype.delete = function (url, options) {
if (options === void 0) { options = {}; }
console.log('DELETE request initiated');
console.log('URL - ', url);
console.log('Options - ', options);
this.setHeaders(options);
return _super.prototype.delete.call(this, url, { headers: this.headers })
.retryWhen(function (attempts) {
console.log('retryWhen callback');
var count = 0;
return attempts.flatMap(function (error) {
if (error.status == 0) {
console.log('########### Now offline #############', error);
return Observable.timer(++count * 1000);
}
else if (error.status == 500 || error.status == 401) {
return ++count >= 3 ? Observable.throw(error) : Observable.timer(1000);
}
else {
return Observable.throw(error);
}
});
});
};
HttpService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
HttpService.ctorParameters = function () { return [
null,
{ type: RequestOptions, },
{ type: AuthenticationService, },
]; };
return HttpService;
}(Http));
export { HttpService };
//# sourceMappingURL=http-service.js.map