Skip to content

Instantly share code, notes, and snippets.

@paritosh64ce
Last active November 12, 2017 14:03
Show Gist options
  • Save paritosh64ce/917e70f43e7d160a27b00f36491b87c0 to your computer and use it in GitHub Desktop.
Save paritosh64ce/917e70f43e7d160a27b00f36491b87c0 to your computer and use it in GitHub Desktop.
Example - Angular 2+ controller calling data service which in turn calls custom common HTTP service
import { Http, Headers, RequestOptions } from '@angular/http';
import { AsyncSubject } from 'rxjs/AsyncSubject';
@Injectable()
export class myHTTPSvc {
constructor(private http: Http, private upSvc: MyUserProfileService) {}
get(url) : Observable<any> {
let subjectResult = new AsyncSubject();
this.upSvc.getLoggedInUserDetails()
.subscribe(details => {
let headerConfig = this.getHeaderConfig(details);
// here is the actual GET call you want to make
this.http.get(url, headerConfig)
.map(data => data.json())
.subscribe(result => {
// similar to deferred.resolve(result) we used to do with Angular 1.x
subjectResult.next(result);
subjectResult.complete();
});
});
return subjectResult;
}
private getHeaderConfig(loggedInUserDetails: any): RequestOptions {
let options = new RequestOptions();
options.headers = new Headers();
options.headers.append('license', loggedInUserDetails.licenseInfo);
// or any details you want to push in message header
return options;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment