diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2019-11-27 22:40:42 -0500 |
---|---|---|
committer | Tucker Evans <tuckerevans24@gmail.com> | 2019-12-02 19:24:21 -0500 |
commit | 3970d90fc06a0cc55c260a1155202eaba18e373b (patch) | |
tree | 6673aa64771555d6586e75fd359589eb3e4ea5a2 /recipeBuddy/src/app/REST_service | |
parent | cafe1b4fd3cc02554f44ffbaa8467d867a6838cb (diff) |
Add base code for BackendService
Diffstat (limited to 'recipeBuddy/src/app/REST_service')
-rw-r--r-- | recipeBuddy/src/app/REST_service/backend.service.spec.ts | 12 | ||||
-rw-r--r-- | recipeBuddy/src/app/REST_service/backend.service.ts | 51 |
2 files changed, 63 insertions, 0 deletions
diff --git a/recipeBuddy/src/app/REST_service/backend.service.spec.ts b/recipeBuddy/src/app/REST_service/backend.service.spec.ts new file mode 100644 index 0000000..20e8bf9 --- /dev/null +++ b/recipeBuddy/src/app/REST_service/backend.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { BackendServiceService } from './backend-service.service'; + +describe('BackendServiceService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: BackendServiceService = TestBed.get(BackendServiceService); + expect(service).toBeTruthy(); + }); +}); diff --git a/recipeBuddy/src/app/REST_service/backend.service.ts b/recipeBuddy/src/app/REST_service/backend.service.ts new file mode 100644 index 0000000..7834cb5 --- /dev/null +++ b/recipeBuddy/src/app/REST_service/backend.service.ts @@ -0,0 +1,51 @@ +import { Injectable } from '@angular/core' +import { HttpClient, HttpHeaders } from '@angular/common/http' +import { Observable, throwError } from 'rxjs' +import { retry, catchError, map } from 'rxjs/operators' +import { Recipe } from '../DataModels/recipe' + + +export interface Status { + Code: number; + Msg: string; +} +export interface MsgList { + Status: Status; + Data: number[]; +} + +export interface Msg { + Status: Status; + Data: Recipe; +} + + +@Injectable({ providedIn: 'root' }) + +/* BackendService class based on tutorial at: + * <https://www.positronx.io/angular-8-httpclient-http-tutorial-build-consume-restful-api/> + */ +export class BackendService { + apiURL = 'http://api.recipebuddy.xyz:8888' + + constructor( private http: HttpClient) + { + } + + httpOptions = {headers: new HttpHeaders( + {'Content-Type':'application/json'} + )} + + + handleError(error) { + let errMsg = ''; + if (error.error instanceof ErrorEvent) { + errMsg = error.error.message; + } else { + errMsg = 'Error API'; + } + console.log(errMsg) + window.alert(errMsg) + return throwError(errMsg); + } +} |