Hi Brian,

This sample is based on http, but you can do something similar for 
httpClient. HttpClient even has the concept of interceptors that might even 
simplify this.
here we go:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import {
Headers,
Http,
Response,
RequestOptions,
Request,
BaseRequestOptions,
RequestMethod,
RequestOptionsArgs,

} from '@angular/http';


@Injectable()
export class Phttp {
private token: string;

constructor(private http: Http) {
}

get(url: string, options?: RequestOptionsArgs): Observable<Response> {
options = this._myDefaultOptions(options);
return this.http.get(url, options)

}

private _header(OrgHeaders) {
if ((!this.token) ) {
this.token = getMyTokenFromSomewhere();
}
if (!this.token) {
throw Error('no token found for pHttp')
}
return new Headers({
authorization: this.token,
...OrgHeaders
})
}

private _myDefaultOptions(options?: RequestOptionsArgs) {
if (!options) {
options = new RequestOptions();
}
options.headers = this._header(options.headers)
return options;
}



post(url: string, body: any, options?: RequestOptionsArgs): 
Observable<Response> {
options = this._myDefaultOptions(options);
return this.http.post(url, body, options)

}
put(url: string, body: any, options?: RequestOptionsArgs): 
Observable<Response> {
options = this._myDefaultOptions(options);
return this.http.put(url, body , options)

}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
options = this._myDefaultOptions(options);
return this.http.delete(url, options)

}
}

Hope this is of some use to you.

Regards
Sander



-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to