diff --git a/src/app/core/data.service.ts b/src/app/core/data.service.ts index ecccafeb128d2d0fe98961020a146dac711491a6..ea4a10f0af30ffa5957df187a900da1b9e77ca6e 100644 --- a/src/app/core/data.service.ts +++ b/src/app/core/data.service.ts @@ -1,16 +1,43 @@ import { identifierModuleUrl } from '@angular/compiler'; import { Injectable } from '@angular/core'; -import { HttpClientModule } from '@angular/common/http'; import { Router } from '@angular/router'; -import { Observable } from 'rxjs'; - +import { HttpClient, HttpHeaders } from '@angular/common/http'; import { ConfigService } from './config.service'; +import { Response, ResponseContentType } from '@angular/http'; +import { Observable } from 'rxjs'; +import { catchError, map, tap } from 'rxjs/operators'; +import { throwError } from 'rxjs'; @Injectable() export class DataService { private api: string; + private coursesUrl = 'https://dev.enroll.wisc.edu/api/planner/v1/degreePlan/519261/courses'; + private log: any; - constructor(private http: HttpClientModule, private configService: ConfigService) { + constructor(private http: HttpClient, private configService: ConfigService) { this.api = this.configService.apiUrl; } + + getCourseData() { + return this.http + .get(this.coursesUrl) + // .subscribe( + // data => console.log('data: ', data), // success path + // error => console.log('error: ', error) // error path + // ); + .pipe( + map(this.extractData), + catchError(this.errorHandler) + ); + } + + private extractData(res: Response) { + return res.text() ? res.json() : {}; + } + + private errorHandler(error: Response) { + // console.error(error); + return throwError(error || 'Server Error'); + } + }