Skip to content

Commit cb8257a

Browse files
committed
pre-release
1 parent 31cc6fc commit cb8257a

File tree

1 file changed

+163
-136
lines changed

1 file changed

+163
-136
lines changed

src/app/services/movie-db.service.ts

Lines changed: 163 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,186 @@
1-
import { Injectable } from '@angular/core';
2-
import { Http } from '@angular/http';
3-
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
4-
import { Observable } from 'rxjs';
5-
import { MoviesInt } from './interface';
6-
import { catchError, retry, retryWhen, debounceTime, delay, throttle } from 'rxjs/operators';
7-
import { map, filter } from 'rxjs/operators';
8-
import { throwError } from 'rxjs/internal/observable/throwError';
1+
import { Injectable } from '@angular/core'
2+
import { Http } from '@angular/http'
3+
import { HttpClient, HttpErrorResponse } from '@angular/common/http'
4+
import { Observable } from 'rxjs'
5+
import { MoviesInt } from './interface'
6+
import {
7+
catchError,
8+
retry,
9+
retryWhen,
10+
debounceTime,
11+
delay,
12+
throttle,
13+
} from 'rxjs/operators'
14+
import { map, filter } from 'rxjs/operators'
15+
import { throwError } from 'rxjs/internal/observable/throwError'
916

1017
@Injectable({
11-
providedIn: 'root'
18+
providedIn: 'root',
1219
})
1320
export class MovieDbService {
14-
15-
constructor( private httpClient: HttpClient) { }
16-
17-
baseUrl = 'https://api.themoviedb.org/3';
18-
apiKey = 'c3a07ff98aaeb2065ebee321bf08d23a';
19-
20-
// example
21-
// https://api.themoviedb.org/3/movie/550?api_key=c3a07ff98aaeb2065ebee321bf08d23a
22-
// https://api.themoviedb.org/3/tv/latest?api_key
23-
24-
/** error handling */
25-
private handleError(error: HttpErrorResponse) {
26-
if (error.error instanceof ErrorEvent) {
27-
// A client-side or network error occurred. Handle it accordingly.
28-
retryWhen(errors => errors.pipe(delay(500)));
29-
console.error('An error occurred:', error.message);
30-
} else {
31-
// The backend returned an unsuccessful response code.
32-
// The response body may contain clues as to what went wrong,
33-
console.error(`Backend returned code ${error.status}, body was: ${error.status}`);
21+
constructor(private httpClient: HttpClient) {}
22+
23+
baseUrl = 'https://api.themoviedb.org/3'
24+
apiKey = 'c3a07ff98aaeb2065ebee321bf08d23a'
25+
26+
// example
27+
// https://api.themoviedb.org/3/movie/550?api_key=c3a07ff98aaeb2065ebee321bf08d23a
28+
// https://api.themoviedb.org/3/tv/latest?api_key
29+
30+
/** error handling */
31+
private handleError(error: HttpErrorResponse) {
32+
if (error.error instanceof ErrorEvent) {
33+
// A client-side or network error occurred. Handle it accordingly.
34+
retryWhen(errors => errors.pipe(delay(500)))
35+
console.error('An error occurred:', error.message)
36+
} else {
37+
// The backend returned an unsuccessful response code.
38+
// The response body may contain clues as to what went wrong,
39+
console.error(
40+
`Backend returned code ${error.status}, body was: ${
41+
error.status
42+
}`
43+
)
44+
}
45+
// return an observable with a user-facing error message
46+
return throwError(`ERROR: ${error.message}`)
3447
}
35-
// return an observable with a user-facing error message
36-
return throwError(`ERROR: ${error.message}`);
37-
}
38-
39-
40-
41-
getDetails(id , type): Observable<MoviesInt> {
42-
const url = `${this.baseUrl}/${type}/${id}?api_key=${this.apiKey}`;
43-
const request = this.httpClient.get<MoviesInt>(url);
44-
retry(10) // retry a failed request up to 3 times
4548

46-
return request;
47-
}
48-
49-
50-
getLatest(type , page): Observable<any> {
51-
const url = `${this.baseUrl}/${type}/latest?api_key=${this.apiKey}&page=${page}&language=en-US`;
52-
const request = this.httpClient.get(url).pipe(
53-
retry(6), // retry a failed request up to 3 times
54-
catchError(this.handleError) // then handle the error
55-
);
56-
57-
return request;
58-
}
49+
getDetails(id, type): Observable<MoviesInt> {
50+
const url = `${this.baseUrl}/${type}/${id}?api_key=${this.apiKey}`
51+
const request = this.httpClient.get<MoviesInt>(url)
52+
retry(10) // retry a failed request up to 3 times
5953

60-
getOnAir(type , page): Observable<MoviesInt> {
61-
const url = `${this.baseUrl}/${type}/on_the_air?api_key=${this.apiKey}&page=${page}&language=en-US`;
62-
const request = this.httpClient.get<MoviesInt>(url).pipe(
63-
retry(6), // retry a failed request up to 3 times
64-
catchError(this.handleError) // then handle the error
65-
);
54+
return request
55+
}
6656

67-
return request;
68-
}
69-
getUpcoming( page): Observable<MoviesInt> {
70-
const url = `${this.baseUrl}/movie/upcoming?api_key=${this.apiKey}&page=${page}&language=en-US`;
71-
const request = this.httpClient.get<MoviesInt>(url).pipe(
72-
retry(6), // retry a failed request up to 3 times
73-
catchError(this.handleError) // then handle the error
74-
);
75-
76-
return request;
77-
}
78-
getAiringToday(type , page): Observable<MoviesInt> {
79-
const url = `${this.baseUrl}/${type}/airing_today?api_key=${this.apiKey}&page=${page}&language=en-US`;
80-
const request = this.httpClient.get<MoviesInt>(url).pipe(
81-
retry(6), // retry a failed request up to 3 times
82-
catchError(this.handleError) // then handle the error
83-
);
84-
85-
return request;
86-
}
57+
getLatest(type, page): Observable<any> {
58+
const url = `${this.baseUrl}/${type}/latest?api_key=${
59+
this.apiKey
60+
}&page=${page}&language=en-US`
61+
const request = this.httpClient.get(url).pipe(
62+
retry(6), // retry a failed request up to 3 times
63+
catchError(this.handleError) // then handle the error
64+
)
8765

88-
getNowPlaying(page): Observable<MoviesInt> {
89-
const url = `${this.baseUrl}/movie/now_playing?api_key=${this.apiKey}&page=${page}&region=US`;
90-
const request = this.httpClient.get<MoviesInt>(url).pipe(
91-
retry(6), // retry a failed request up to 3 times
92-
catchError(this.handleError) // then handle the error
93-
);
66+
return request
67+
}
9468

95-
return request;
96-
}
69+
getOnAir(type, page): Observable<MoviesInt> {
70+
const url = `${this.baseUrl}/${type}/on_the_air?api_key=${
71+
this.apiKey
72+
}&page=${page}&language=en-US`
73+
const request = this.httpClient.get<MoviesInt>(url).pipe(
74+
retry(6), // retry a failed request up to 3 times
75+
catchError(this.handleError) // then handle the error
76+
)
9777

98-
getPopular(type , page): Observable<any> {
99-
const url = `${this.baseUrl}/${type}/popular?api_key=${this.apiKey}&page=${page}&language=en-US`;
100-
const request = this.httpClient.get(url).pipe(
101-
retry(6), // retry a failed request up to 3 times
102-
catchError(this.handleError) // then handle the error
103-
);
78+
return request
79+
}
80+
getUpcoming(page): Observable<MoviesInt> {
81+
const url = `${this.baseUrl}/movie/upcoming?api_key=${
82+
this.apiKey
83+
}&page=${page}&language=en-US`
84+
const request = this.httpClient.get<MoviesInt>(url).pipe(
85+
retry(6), // retry a failed request up to 3 times
86+
catchError(this.handleError) // then handle the error
87+
)
88+
89+
return request
90+
}
91+
getAiringToday(type, page): Observable<MoviesInt> {
92+
const url = `${this.baseUrl}/${type}/airing_today?api_key=${
93+
this.apiKey
94+
}&page=${page}&language=en-US`
95+
const request = this.httpClient.get<MoviesInt>(url).pipe(
96+
retry(6), // retry a failed request up to 3 times
97+
catchError(this.handleError) // then handle the error
98+
)
99+
100+
return request
101+
}
104102

105-
return request;
106-
}
103+
getNowPlaying(page): Observable<MoviesInt> {
104+
const url = `${this.baseUrl}/movie/now_playing?api_key=${
105+
this.apiKey
106+
}&page=${page}&region=US`
107+
const request = this.httpClient.get<MoviesInt>(url).pipe(
108+
retry(6), // retry a failed request up to 3 times
109+
catchError(this.handleError) // then handle the error
110+
)
107111

108-
getTopRated(type , page): Observable<MoviesInt> {
109-
const url = `${this.baseUrl}/${type}/top_rated?api_key=${this.apiKey}&page=${page}&adult=false&region=US`;
110-
const request = this.httpClient.get<MoviesInt>(url).pipe(
111-
retry(6), // retry a failed request up to 3 times
112-
catchError(this.handleError) // then handle the error
113-
);
112+
return request
113+
}
114114

115-
return request;
116-
}
117-
getSimilar(type , page): Observable<any> {
118-
const url = `${this.baseUrl}/${type}/similar?api_key=${this.apiKey}&page=${page}&adult=false&language=en-US`;
119-
const request = this.httpClient.get(url).pipe(
120-
retry(6), // retry a failed request up to 3 times
121-
catchError(this.handleError) // then handle the error
122-
);
123-
124-
return request;
125-
}
115+
getPopular(type, page): Observable<any> {
116+
const url = `${this.baseUrl}/${type}/popular?api_key=${
117+
this.apiKey
118+
}&page=${page}&language=en-US`
119+
const request = this.httpClient.get(url).pipe(
120+
retry(6), // retry a failed request up to 3 times
121+
catchError(this.handleError) // then handle the error
122+
)
126123

127-
searchKeyword(keyword, type, page): Observable<any> {
128-
const url = `${this.baseUrl}/search/${type}?api_key=${this.apiKey}&query=${keyword}&page=${page}&adult=false&language=en-US`;
129-
const request = this.httpClient.get(url).pipe(
130-
map(res => res['results']),
131-
retry(6), // retry a failed request up to 3 times
132-
catchError(this.handleError) // then handle the error
133-
);
124+
return request
125+
}
134126

135-
return request;
136-
}
127+
getTopRated(type, page): Observable<MoviesInt> {
128+
const url = `${this.baseUrl}/${type}/top_rated?api_key=${
129+
this.apiKey
130+
}&page=${page}&adult=false&region=US`
131+
const request = this.httpClient.get<MoviesInt>(url).pipe(
132+
retry(6), // retry a failed request up to 3 times
133+
catchError(this.handleError) // then handle the error
134+
)
137135

136+
return request
137+
}
138+
getSimilar(type, page): Observable<any> {
139+
const url = `${this.baseUrl}/${type}/similar?api_key=${
140+
this.apiKey
141+
}&page=${page}&adult=false&language=en-US`
142+
const request = this.httpClient.get(url).pipe(
143+
retry(6), // retry a failed request up to 3 times
144+
catchError(this.handleError) // then handle the error
145+
)
146+
147+
return request
148+
}
138149

139-
SearchAll( keyword, page): Observable<any> {
140-
const url = `${this.baseUrl}/search/multi?api_key=${this.apiKey}&query=${keyword}&page=${page}&language=en-US`;
141-
const request = this.httpClient.get(url).pipe(
142-
retry(6), // retry a failed request up to 3 times
143-
catchError(this.handleError) // then handle the error
144-
);
150+
searchKeyword(keyword, type, page): Observable<any> {
151+
const url = `${this.baseUrl}/search/${type}?api_key=${
152+
this.apiKey
153+
}&query=${keyword}&page=${page}&adult=false&language=en-US`
154+
const request = this.httpClient.get(url).pipe(
155+
map(res => res['results']),
156+
retry(6), // retry a failed request up to 3 times
157+
catchError(this.handleError) // then handle the error
158+
)
159+
160+
return request
161+
}
145162

146-
return request;
147-
}
163+
SearchAll(keyword, page): Observable<any> {
164+
const url = `${this.baseUrl}/search/multi?api_key=${
165+
this.apiKey
166+
}&query=${keyword}&page=${page}&language=en-US`
167+
const request = this.httpClient.get(url).pipe(
168+
retry(6), // retry a failed request up to 3 times
169+
catchError(this.handleError) // then handle the error
170+
)
148171

149-
find(id ): Observable<any> {
150-
const url = `${this.baseUrl}/find/${id}?api_key=${this.apiKey}&external_source=imdb_id&adult=false&language=en-US`;
151-
const request = this.httpClient.get(url).pipe(
152-
retry(6), // retry a failed request up to 3 times
153-
catchError(this.handleError) // then handle the error
154-
);
172+
return request
173+
}
155174

156-
return request;
157-
}
175+
find(id): Observable<any> {
176+
const url = `${this.baseUrl}/find/${id}?api_key=${
177+
this.apiKey
178+
}&external_source=imdb_id&adult=false&language=en-US`
179+
const request = this.httpClient.get(url).pipe(
180+
retry(6), // retry a failed request up to 3 times
181+
catchError(this.handleError) // then handle the error
182+
)
158183

184+
return request
185+
}
159186
}

0 commit comments

Comments
 (0)