Update to HttpClient

This commit is contained in:
2018-06-02 15:22:19 +02:00
parent 330adb4b9f
commit f8662894b3
2 changed files with 32 additions and 32 deletions

View File

@@ -1,6 +1,6 @@
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component';
@@ -24,7 +24,7 @@ import { RoundPipe } from './pipes/round.pipe';
@NgModule({
imports: [
BrowserModule,
HttpModule,
HttpClientModule,
AppRoutingModule
],
declarations: [

View File

@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import { HttpClient, HttpHeaders } from '@angular/common/http'
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise';
@@ -19,13 +19,13 @@ export class ElsService {
private static readonly ACTION_COUNT = '/_count';
private elsUrl = 'http://localhost:9200/' + ElsService.INDEX_NAME + '/';
private headers = new Headers({'Content-Type': 'application/json'});
private headers = new HttpHeaders({'Content-Type': 'application/json'});
constructor(private http: Http) { }
constructor(private http: HttpClient) { }
getTime(): Promise<number> {
return this.http
.post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH,
.post<any>(this.elsUrl + 'song' + ElsService.ACTION_SEARCH,
JSON.stringify({
aggs: {
sum_time: {
@@ -35,7 +35,7 @@ export class ElsService {
'size': 0
}), {headers: this.headers})
.toPromise()
.then(res => res.json().aggregations.sum_time.value as number)
.then(res => res.aggregations.sum_time.value as number)
.catch(this.handleError);
}
@@ -47,7 +47,7 @@ export class ElsService {
getSize(): Promise<number> {
return this.http
.post(this.elsUrl + ElsService.ACTION_SEARCH,
.post<any>(this.elsUrl + ElsService.ACTION_SEARCH,
JSON.stringify({
aggs: {
sum_time: {
@@ -57,7 +57,7 @@ export class ElsService {
'size': 0
}), {headers: this.headers})
.toPromise()
.then(res => res.json().aggregations.sum_time.value as number)
.then(res => res.aggregations.sum_time.value as number)
.catch(this.handleError);
}
@@ -69,15 +69,15 @@ export class ElsService {
getCountSong(type: string): Promise<number> {
return this.http
.get(this.elsUrl + type + ElsService.ACTION_COUNT)
.get<any>(this.elsUrl + type + ElsService.ACTION_COUNT)
.toPromise()
.then(res => res.json().count as number)
.then(res => res.count as number)
.catch(this.handleError);
}
getCountNeverListenSong(): Promise<number> {
return this.http
.post(this.elsUrl + 'song' + ElsService.ACTION_COUNT,
.post<any>(this.elsUrl + 'song' + ElsService.ACTION_COUNT,
JSON.stringify({
'query': {
'bool': {
@@ -88,7 +88,7 @@ export class ElsService {
}
}), {headers: this.headers})
.toPromise()
.then(res => res.json().count as number)
.then(res => res.count as number)
.catch(this.handleError);
}
@@ -102,7 +102,7 @@ export class ElsService {
// Thank to http://chariotsolutions.com/blog/post/angular2-observables-http-separating-services-components/
// for the map part
return this.http
.post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH,
.post<any>(this.elsUrl + 'song' + ElsService.ACTION_SEARCH,
JSON.stringify({
'sort': [ {
'Play Count': {
@@ -271,7 +271,7 @@ export class ElsService {
getGenreCount(): Observable<number> {
return this.http
.post(this.elsUrl + 'song' + ElsService.ACTION_SEARCH,
.post<any>(this.elsUrl + 'song' + ElsService.ACTION_SEARCH,
JSON.stringify({
'aggs' : {
'genres' : {
@@ -283,7 +283,7 @@ export class ElsService {
},
'size': 0
}), {headers: this.headers})
.map(res => res.json().aggregations.genres.value);
.map(res => res.aggregations.genres.value);
}
getLastAddedAlbums(month: number): Observable<Bucket[]> {
@@ -322,7 +322,7 @@ export class ElsService {
getArtistFromAlbumName(albumname: string): Observable<Album[]> {
return this.http
.post(this.elsUrl + 'album' + ElsService.ACTION_SEARCH,
.post<any>(this.elsUrl + 'album' + ElsService.ACTION_SEARCH,
JSON.stringify({
'query': {
'match_phrase' : {
@@ -330,7 +330,7 @@ export class ElsService {
}
}
}), {headers: this.headers})
.map(res => res.json().hits.hits)
.map(res => res.hits.hits)
.map((hits: Array<any>) => {
// TODO Use a method (duplicated code ?)
const result: Array<Album> = [];
@@ -344,7 +344,7 @@ export class ElsService {
getCountArtistSong(artistName: string): Observable<number> {
console.log('artistname: ' + artistName);
return this.http
.post(this.elsUrl + 'song' + ElsService.ACTION_COUNT,
.post<any>(this.elsUrl + 'song' + ElsService.ACTION_COUNT,
JSON.stringify({
'query': {
'bool': {
@@ -355,7 +355,7 @@ export class ElsService {
}
}
}), {headers: this.headers})
.map(res => res.json().count as number);
.map(res => res.count as number);
}
/** Process a result to return just one result.
@@ -365,8 +365,8 @@ export class ElsService {
* @param res Response to process
* @param name The searched name - for console output
*/
private responseToOneTypedResult<T>(res: Response, name: string): T {
const hits = res.json().hits.hits;
private responseToOneTypedResult<T>(res: any, name: string): T {
const hits = res.hits.hits;
if (hits.length < 1) {
console.info('No result found for name: "' + name);
@@ -383,9 +383,9 @@ export class ElsService {
*
* @param res Response to process
*/
private responseToSongs(res: Response): Song[] {
private responseToSongs(res: any): Song[] {
const result: Array<Song> = [];
res.json().hits.hits.forEach((hit) => {
res.hits.hits.forEach((hit) => {
result.push(hit._source);
});
return result;
@@ -395,9 +395,9 @@ export class ElsService {
*
* @param res Response to process
*/
private responseToAlbums(res: Response): Album[] {
private responseToAlbums(res: any): Album[] {
const result: Array<Album> = [];
res.json().hits.hits.forEach((hit) => {
res.hits.hits.forEach((hit) => {
result.push(hit._source);
});
return result;
@@ -407,9 +407,9 @@ export class ElsService {
*
* @param res Response to process
*/
private responseToArtists(res: Response): Artist[] {
private responseToArtists(res: any): Artist[] {
const result: Array<Artist> = [];
res.json().hits.hits.forEach((hit) => {
res.hits.hits.forEach((hit) => {
result.push(hit._source);
});
return result;
@@ -421,17 +421,17 @@ export class ElsService {
* @param res Response to process
* @param name Name of aggregation
*/
private responseAggregationToBucket(res: Response, name: string): Bucket[] {
private responseAggregationToBucket(res: any, name: string): Bucket[] {
const result: Array<Bucket> = [];
res.json().aggregations[name].buckets.forEach((bucket) => {
res.aggregations[name].buckets.forEach((bucket) => {
result.push(bucket);
});
return result;
}
private responseSubAggregationToBucket(res: Response, name: string): Bucket[] {
private responseSubAggregationToBucket(res: any, name: string): Bucket[] {
const result: Array<Bucket> = [];
res.json().aggregations[name].buckets.forEach((bucket) => {
res.aggregations[name].buckets.forEach((bucket) => {
bucket['album'].buckets.forEach((subBucket) => {
result.push(subBucket);
});