|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import {HttpClient, HttpEventType, HttpHeaders, HttpRequest} from "@angular/common/http"; |
| 3 | +import {Observable} from "rxjs/Observable"; |
| 4 | +import "rxjs/add/operator/catch"; |
| 5 | +import "rxjs/add/observable/of"; |
| 6 | +import "rxjs/add/operator/switchMap"; |
| 7 | +import "rxjs/add/observable/forkJoin"; |
| 8 | +import "rxjs/add/operator/map"; |
| 9 | +import "rxjs/add/operator/shareReplay"; |
| 10 | + |
| 11 | +@Injectable() |
| 12 | +export class HttpclientService { |
| 13 | + |
| 14 | + courses$: Observable<Course[]>; |
| 15 | + |
| 16 | + constructor(private http: HttpClient) { } |
| 17 | + |
| 18 | + getUserData() { |
| 19 | + return this.http.get('https://api.github.com/users/seeschweiler'); |
| 20 | + } |
| 21 | + |
| 22 | + longRequest() { |
| 23 | + |
| 24 | + const request = new HttpRequest( |
| 25 | + "POST", "/api/test-request", {}, |
| 26 | + {reportProgress: true}); |
| 27 | + |
| 28 | + this.http.request(request) |
| 29 | + .subscribe( |
| 30 | + event => { |
| 31 | + |
| 32 | + if (event.type === HttpEventType.DownloadProgress) { |
| 33 | + console.log("Download progress event", event); |
| 34 | + } |
| 35 | + |
| 36 | + if (event.type === HttpEventType.UploadProgress) { |
| 37 | + console.log("Upload progress event", event); |
| 38 | + } |
| 39 | + |
| 40 | + if (event.type === HttpEventType.Response) { |
| 41 | + console.log("response received...", event.body); |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + throwError() { |
| 49 | + |
| 50 | + this.http |
| 51 | + .get("/api/simulate-error") |
| 52 | + .catch( error => { |
| 53 | + // here we can show an error message to the user, |
| 54 | + // for example via a service |
| 55 | + console.error("error catched", error); |
| 56 | + |
| 57 | + return Observable.of({description: "Error Value Emitted"}); |
| 58 | + }) |
| 59 | + .subscribe( |
| 60 | + val => console.log('Value emitted successfully', val), |
| 61 | + error => { |
| 62 | + console.error("This line is never called ",error); |
| 63 | + }, |
| 64 | + () => console.log("HTTP Observable completed...") |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + sequentialRequests() { |
| 69 | + |
| 70 | + const sequence$ = this.http.get<Course>( |
| 71 | + '/courses/-KgVwEBq5wbFnjj7O8Fp.json') |
| 72 | + .switchMap(course => { |
| 73 | + course.description+= ' - TEST '; |
| 74 | + return this.http.put('/courses/-KgVwEBq5wbFnjj7O8Fp.json', course) |
| 75 | + }, |
| 76 | + (firstHTTPResult, secondHTTPResult) => |
| 77 | + [firstHTTPResult, secondHTTPResult]); |
| 78 | + |
| 79 | + sequence$.subscribe( |
| 80 | + values => console.log("result observable ", values) |
| 81 | + ); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + sequentialRequests1() { |
| 86 | + |
| 87 | + const sequence$ = this.http.get<Course>( |
| 88 | + '/courses/-KgVwEBq5wbFnjj7O8Fp.json') |
| 89 | + .switchMap(course => { |
| 90 | + |
| 91 | + course.description+= ' - TEST '; |
| 92 | + |
| 93 | + return this.http.put( |
| 94 | + '/courses/-KgVwEBq5wbFnjj7O8Fp.json', |
| 95 | + course) |
| 96 | + }); |
| 97 | + sequence$.subscribe(); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + parallelRequests() { |
| 102 | + |
| 103 | + const parallel$ = Observable.forkJoin( |
| 104 | + this.http.get('/courses/-KgVwEBq5wbFnjj7O8Fp.json'), |
| 105 | + this.http.get('/courses/-KgVwECOnlc-LHb_B0cQ.json') |
| 106 | + ); |
| 107 | + |
| 108 | + parallel$.subscribe( |
| 109 | + values => { |
| 110 | + console.log("all values", values) |
| 111 | + } |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + shareReply() { |
| 116 | + const httpGet$ = this.http |
| 117 | + .get("/courses.json") |
| 118 | + .map(data => data) |
| 119 | + .shareReplay(); |
| 120 | + } |
| 121 | + |
| 122 | + duplicateRequestsExample() { |
| 123 | + |
| 124 | + const httpGet$ = this.http |
| 125 | + .get("/courses.json") |
| 126 | + .map(data => data); |
| 127 | + |
| 128 | + httpGet$.subscribe( |
| 129 | + (val) => console.log("logging GET value", val) |
| 130 | + ); |
| 131 | + |
| 132 | + this.courses$ = httpGet$; |
| 133 | + } |
| 134 | + |
| 135 | + httpPostExample() { |
| 136 | + |
| 137 | + this.http.post("/courses/-KgVwECOnlc-LHb_B0cQ.json", |
| 138 | + { |
| 139 | + "courseListIcon": "...", |
| 140 | + "description": "TEST", |
| 141 | + "iconUrl": "..", |
| 142 | + "longDescription": "...", |
| 143 | + "url": "new-url" |
| 144 | + }) |
| 145 | + .subscribe( |
| 146 | + (val) => { |
| 147 | + console.log("POST call successful value returned in body", |
| 148 | + val); |
| 149 | + }, |
| 150 | + response => { |
| 151 | + console.log("POST call in error", response); |
| 152 | + }, |
| 153 | + () => { |
| 154 | + console.log("The POST observable is now completed."); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + httpDeleteExample() { |
| 159 | + |
| 160 | + this.http.delete("/courses/-KgVwECOnlc-LHb_B0cQ.json") |
| 161 | + .subscribe( |
| 162 | + (val) => { |
| 163 | + console.log("DELETE call successful value returned in body", |
| 164 | + val); |
| 165 | + }, |
| 166 | + response => { |
| 167 | + console.log("DELETE call in error", response); |
| 168 | + }, |
| 169 | + () => { |
| 170 | + console.log("The DELETE observable is now completed."); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + httpPatchExample() { |
| 175 | + |
| 176 | + this.http.patch("/courses/-KgVwECOnlc-LHb_B0cQ.json", |
| 177 | + { |
| 178 | + "description": "Angular Tutorial For Beginners PATCH TEST", |
| 179 | + }) |
| 180 | + .subscribe( |
| 181 | + (val) => { |
| 182 | + console.log("PATCH call successful value returned in body", |
| 183 | + val); |
| 184 | + }, |
| 185 | + response => { |
| 186 | + console.log("PATCH call in error", response); |
| 187 | + }, |
| 188 | + () => { |
| 189 | + console.log("The PATCH observable is now completed."); |
| 190 | + }); |
| 191 | + } |
| 192 | + |
| 193 | + httpPutExample() { |
| 194 | + |
| 195 | + const headers = new HttpHeaders() |
| 196 | + .set("Content-Type", "application/json"); |
| 197 | + |
| 198 | + this.http.put("/courses/-KgVwECOnlc-LHb_B0cQ.json", |
| 199 | + { |
| 200 | + "courseListIcon": ".../main-page-logo-small-hat.png", |
| 201 | + "description": "Angular Tutorial For Beginners TEST", |
| 202 | + "iconUrl": ".../angular2-for-beginners.jpg", |
| 203 | + "longDescription": "...", |
| 204 | + "url": "new-value-for-url" |
| 205 | + }, |
| 206 | + {headers}) |
| 207 | + .subscribe( |
| 208 | + val => { |
| 209 | + console.log("PUT call successful value returned in body", |
| 210 | + val); |
| 211 | + }, |
| 212 | + response => { |
| 213 | + console.log("PUT call in error", response); |
| 214 | + }, |
| 215 | + () => { |
| 216 | + console.log("The PUT observable is now completed."); |
| 217 | + } |
| 218 | + ); |
| 219 | + } |
| 220 | + |
| 221 | +} |
| 222 | + |
| 223 | +interface Course { |
| 224 | + description: string; |
| 225 | + courseListIcon:string; |
| 226 | + iconUrl:string; |
| 227 | + longDescription:string; |
| 228 | + url:string; |
| 229 | +} |
0 commit comments