Skip to content

Commit

Permalink
changed reference from Http to HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
grumar committed Dec 15, 2017
1 parent 824a03e commit 9bf3b95
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 84 deletions.
24 changes: 24 additions & 0 deletions src/adal4-angular.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { Adal4Interceptor } from './adal4-interceptor';
import { Adal4User } from './adal4-user';
import { Adal4Service } from './adal4.service';
import { Adal4HTTPService } from './adal4-http.service';

@NgModule({
imports: [
CommonModule,
],
exports: [
Adal4User, Adal4Service, Adal4HTTPService, Adal4Interceptor
],
providers: [,
{
provide: HTTP_INTERCEPTORS,
useClass: Adal4Interceptor,
multi: true
},
],
})
export class Adal4AgnularModule { }
100 changes: 27 additions & 73 deletions src/adal4-http.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptionsArgs, RequestOptions, RequestMethod, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Adal4Service } from './adal4.service';
import { HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http';

/**
*
Expand All @@ -16,118 +16,106 @@ export class Adal4HTTPService {
*
*
* @static
* @param {Http} http
* @param {HttpClient} http
* @param {Adal4Service} service
*
* @memberOf Adal4HTTPService
*/
static factory(http: Http, service: Adal4Service) {
static factory(http: HttpClient, service: Adal4Service) {
return new Adal4HTTPService(http, service);
}

/**
* Creates an instance of Adal4HTTPService.
* @param {Http} http
* @param {HttpClient} http
* @param {Adal4Service} service
*
* @memberOf Adal4HTTPService
*/
constructor(
private http: Http,
private http: HttpClient,
private service: Adal4Service
) { }

/**
*
*
* @param {string} url
* @param {RequestOptionsArgs} [options]
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal4HTTPService
*/
get(url: string, options?: RequestOptionsArgs): Observable<any> {
let options1 = new RequestOptions({ method: RequestMethod.Get });
options1 = options1.merge(options);
return this.sendRequest(url, options1);
get(url: string, options?: any): Observable<any> {
return this.sendRequest('get', url, options);
}

/**
*
*
* @param {string} url
* @param {*} body
* @param {RequestOptionsArgs} [options]
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal4HTTPService
*/
post(url: string, body: any, options?: RequestOptionsArgs): Observable<any> {
let options1 = new RequestOptions({ method: RequestMethod.Post, body: body });
options1 = options1.merge(options);
return this.sendRequest(url, options1);
post(url: string, body: any, options?: any): Observable<any> {
return this.sendRequest('post', url, options, body);
}

/**
*
*
* @param {string} url
* @param {RequestOptionsArgs} [options]
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal4HTTPService
*/
delete(url: string, options?: RequestOptionsArgs): Observable<any> {
let options1 = new RequestOptions({ method: RequestMethod.Delete });
options1 = options1.merge(options);
return this.sendRequest(url, options1);
delete(url: string, options?: any): Observable<any> {
return this.sendRequest('delete', url, options);
}

/**
*
*
* @param {string} url
* @param {*} body
* @param {RequestOptionsArgs} [options]
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal4HTTPService
*/
patch(url: string, body: any, options?: RequestOptionsArgs): Observable<any> {
let options1 = new RequestOptions({ method: RequestMethod.Patch, body: body });
options1 = options1.merge(options);
return this.sendRequest(url, options1);
patch(url: string, body: any, options?: any): Observable<any> {
return this.sendRequest('patch', url, options, body);
}

/**
*
*
* @param {string} url
* @param {*} body
* @param {RequestOptionsArgs} [options]
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal4HTTPService
*/
put(url: string, body: any, options?: RequestOptionsArgs): Observable<any> {
let options1 = new RequestOptions({ method: RequestMethod.Put, body: body });
options1 = options1.merge(options);
return this.sendRequest(url, options1);
put(url: string, body: any, options?: any): Observable<any> {
return this.sendRequest('put', url, options, body);
}

/**
*
*
* @param {string} url
* @param {RequestOptionsArgs} [options]
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal4HTTPService
*/
head(url: string, options?: RequestOptionsArgs): Observable<any> {
let options1 = new RequestOptions({ method: RequestMethod.Put });
options1 = options1.merge(options);
return this.sendRequest(url, options1);
head(url: string, options?: any): Observable<any> {
return this.sendRequest('head', url, options);
}

/**
Expand All @@ -140,58 +128,24 @@ export class Adal4HTTPService {
*
* @memberOf Adal4HTTPService
*/
private sendRequest(url: string, options: RequestOptionsArgs): Observable<string> {
// make a copy
let options1 = new RequestOptions();
options1.method = options.method;
options1 = options1.merge(options);

private sendRequest(method: string, url: string, options: any, body?: any): Observable<string> {
const resource = this.service.GetResourceForEndpoint(url);
let authenticatedCall: Observable<string>;
if (resource) {
if (this.service.userInfo.authenticated) {
authenticatedCall = this.service.acquireToken(resource)
.flatMap((token: string) => {
if (options1.headers == null) {
options1.headers = new Headers();
}
options1.headers.append('Authorization', 'Bearer ' + token);
return this.http.request(url, options1)
return this.http.request(method, url, options)
.catch(this.handleError);
});
}
else {
} else {
authenticatedCall = Observable.throw(new Error('User Not Authenticated.'));
}
}
else { authenticatedCall = this.http.request(url, options).map(this.extractData).catch(this.handleError); }
} else { authenticatedCall = this.http.request(method, url, options).catch(this.handleError); }

return authenticatedCall;
}

/**
*
*
* @private
* @param {Response} res
* @returns
*
* @memberOf Adal4HTTPService
*/
private extractData(res: Response) {
if (res.status < 200 || res.status >= 300) {
throw new Error('Bad response status: ' + res.status);
}

let body = {};
// if there is some content, parse it
if (res.status !== 204) {
body = res.json();
}

return body || {};
}

/**
*
*
Expand Down
5 changes: 2 additions & 3 deletions src/adal4.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Observable } from 'rxjs/Rx';
import { Adal4User } from './adal4-user';
import * as adalLib from 'adal-angular';
import { adal } from 'adal-angular';
Expand Down Expand Up @@ -156,8 +156,7 @@ export class Adal4Service {
if (requestInfo.parameters['access_token']) {
this.adalContext.callback(this.adalContext._getItem(this.adalContext.CONSTANTS.STORAGE.ERROR_DESCRIPTION)
, requestInfo.parameters['access_token']);
}
else if (requestInfo.parameters['error']) {
} else if (requestInfo.parameters['error']) {
this.adalContext.callback(this.adalContext._getItem(this.adalContext.CONSTANTS.STORAGE.ERROR_DESCRIPTION), null);
this.adalContext._renewFailed = true;
}
Expand Down
5 changes: 0 additions & 5 deletions src/index.ts

This file was deleted.

6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"outDir": "dist/",
"rootDir": "src/",
"sourceMap": true,
" declaration": true,
"declaration": true,
"moduleResolution": "node",
"removeComments": false,
"lib": [
Expand All @@ -25,8 +25,8 @@
"./src"
],
"skipLibCheck": false,
"globalDependencies": {
"adal": "file:./src/adal.d.ts"
"globalDependencies": {
"adal": "file:./src/adal.d.ts"
}
}
}

0 comments on commit 9bf3b95

Please sign in to comment.