Skip to content

Commit 0e576a3

Browse files
committed
interceptor and polling has added
1 parent f97930f commit 0e576a3

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

src/app/Providers/httpservice.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,22 @@ export class HTTPService {
1111
getData() {
1212
return this.http.get(`${this.baseUrl}assets/data/election.json`);
1313
}
14+
15+
startPolling() {
16+
setTimeout(() => {
17+
this.polling();
18+
}, 1500);
19+
}
20+
21+
polling() {
22+
setTimeout(() => {
23+
this.http.get(`${this.baseUrl}assets/data/election.json`)
24+
.subscribe((response: any) => {
25+
console.log("polling");
26+
this.startPolling();
27+
});
28+
}, 2500);
29+
}
30+
31+
1432
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Injectable } from "@angular/core";
2+
import { HttpRequest, HttpHandler, HttpEvent } from "@angular/common/http";
3+
import { Observable } from "rxjs/Rx";
4+
5+
6+
@Injectable()
7+
export class MyHttpInterceptor {
8+
constructor(
9+
) { }
10+
11+
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
12+
13+
console.log("intercepted request ... ");
14+
15+
// Clone the request to add the new header.
16+
const authReq = req.clone({ headers: req.headers.set("headerName", "headerValue") });
17+
18+
console.log("Sending request with new header now ...");
19+
20+
//send the newly created request
21+
return next.handle(authReq)
22+
.catch((error, caught) => {
23+
//intercept the respons error and displace it to the console
24+
console.log("Error Occurred");
25+
console.log(error);
26+
//return the error to the method that called it
27+
return Observable.throw(error);
28+
}) as any;
29+
}
30+
31+
}

src/app/app.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ToastrModule } from 'ngx-toastr';
2-
import { HttpClientModule, HttpClient } from '@angular/common/http';
2+
import { HttpClientModule, HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http';
33
import { BrowserModule } from '@angular/platform-browser';
44
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
55
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
@@ -11,6 +11,7 @@ import { PrivateModule } from './private/private.module';
1111
import { AlwaysAuthGuard, OnlyLoggedInUsersGuard } from './AlwaysAuthGuard ';
1212
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
1313
import { StorageService } from './Providers/storageservice';
14+
import { MyHttpInterceptor } from './Providers/my-http-interceptor';
1415
@NgModule({
1516
declarations: [
1617
AppComponent
@@ -34,7 +35,8 @@ import { StorageService } from './Providers/storageservice';
3435
StorageService,
3536
AlwaysAuthGuard,
3637
OnlyLoggedInUsersGuard,
37-
{ provide: LocationStrategy, useClass: HashLocationStrategy }
38+
{ provide: LocationStrategy, useClass: HashLocationStrategy },
39+
{ provide: HTTP_INTERCEPTORS, useClass: MyHttpInterceptor, multi: true }
3840
],
3941
bootstrap: [AppComponent],
4042
schemas: [

src/app/private/private.component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { HTTPService } from './../Providers/httpservice';
12
import { Component, OnInit } from '@angular/core';
23

34
@Component({
@@ -7,9 +8,12 @@ import { Component, OnInit } from '@angular/core';
78
})
89
export class PrivateComponent implements OnInit {
910

10-
constructor() { }
11+
constructor(
12+
private http: HTTPService
13+
) { }
1114

1215
ngOnInit() {
16+
// this.http.startPolling();
1317
}
1418

1519
}

0 commit comments

Comments
 (0)