Permalink
Browse files

Auto-generated commit -- Update to new HttpClient

- Import HttpClientModule from @angular/common/http (instead of HttpModule from @angular/http) in AppModule
- Inject HttpClient from @angular/common/http (instead of Http) in the BookStoreService
- remove mapping to response.json()
- no JSON.stringify() needed anymore for POST/PUT requests
- no Content-Type header needed anymore
- responseType text for requests with non-JSON responses
- generic type param for this.http.get when array of books is being retrieved (any[] will be replaced by BookRaw[] interface later)

Signed-off-by: Johannes Hoppe <johannes.hoppe@haushoppe-its.de>

Triggered by commit: angular-buch/book-monkey2@efeb008
Travis build: https://travis-ci.org/angular-buch/book-monkey2/builds/307854537
  • Loading branch information...
TheBuildbot committed Nov 27, 2017
1 parent 6e54598 commit efd88396773ba0c5b52494e0f1aef958f7cc3c3e
Showing with 9 additions and 16 deletions.
  1. +2 −2 src/app/app.module.ts
  2. +7 −14 src/app/shared/book-store.service.ts
@@ -1,5 +1,5 @@
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { HttpClientModule } from '@angular/common/http';
import { NgModule, LOCALE_ID } from '@angular/core';

import { AppComponent } from './app.component';
@@ -17,7 +17,7 @@ import { BookResolver } from './shared/book-resolver.service';
],
imports: [
BrowserModule,
HttpModule,
HttpClientModule,
AppRoutingModule
],
providers: [
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/retry';
@@ -12,17 +12,13 @@ import { BookFactory } from './book-factory';
@Injectable()
export class BookStoreService {
private api = 'https://book-monkey2-api.angular-buch.com';
private headers: Headers = new Headers();

constructor(private http: Http) {
this.headers.append('Content-Type', 'application/json');
}
constructor(private http: HttpClient) {}

getAll(): Observable<Array<Book>> {
return this.http
.get(`${this.api}/books`)
.get<any[]>(`${this.api}/books`)
.retry(3)
.map(response => response.json())
.map(rawBooks => rawBooks
.map(rawBook => BookFactory.fromObject(rawBook))
)
@@ -33,33 +29,31 @@ export class BookStoreService {
return this.http
.get(`${this.api}/book/${isbn}`)
.retry(3)
.map(response => response.json())
.map(rawBook => BookFactory.fromObject(rawBook))
.catch(this.errorHandler);
}

check(isbn: string): Observable<Boolean> {
return this.http
.get(`${this.api}/book/${isbn}/check`)
.map(response => response.json())
.catch(this.errorHandler);
}

create(book: Book): Observable<any> {
return this.http
.post(`${this.api}/book`, JSON.stringify(book), { headers: this.headers })
.post(`${this.api}/book`, book, { responseType: 'text' })
.catch(this.errorHandler);
}

update(book: Book): Observable<any> {
return this.http
.put(`${this.api}/book/${book.isbn}`, JSON.stringify(book), { headers: this.headers })
.put(`${this.api}/book/${book.isbn}`, book, { responseType: 'text' })
.catch(this.errorHandler);
}

remove(isbn: string): Observable<any> {
return this.http
.delete(`${this.api}/book/${isbn}`)
.delete(`${this.api}/book/${isbn}`, { responseType: 'text' })
.catch(this.errorHandler);
}

@@ -69,9 +63,8 @@ export class BookStoreService {

getAllSearch(searchTerm: string): Observable<Array<Book>> {
return this.http
.get(`${this.api}/books/search/${searchTerm}`)
.get<any[]>(`${this.api}/books/search/${searchTerm}`)
.retry(3)
.map(response => response.json())
.map(rawBooks => rawBooks
.map(rawBook => BookFactory.fromObject(rawBook))
)

0 comments on commit efd8839

Please sign in to comment.