Skip to content

Commit 0cdfedd

Browse files
committed
fix: add fallback books when Google API returns 429
1 parent ceed0fd commit 0cdfedd

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import { HttpClient } from '@angular/common/http';
22
import { inject, Injectable } from '@angular/core';
33

4-
import { Observable } from 'rxjs';
5-
import { map } from 'rxjs/operators';
4+
import { Observable, of } from 'rxjs';
5+
import { catchError, map } from 'rxjs/operators';
66
import { Book } from './book.model';
77

8+
const FALLBACK_BOOKS: Book[] = [
9+
{ id: '1', volumeInfo: { title: 'Awakenings', authors: ['Oliver Sacks'] } },
10+
{ id: '2', volumeInfo: { title: 'The Man Who Mistook His Wife for a Hat', authors: ['Oliver Sacks'] } },
11+
{ id: '3', volumeInfo: { title: 'An Anthropologist on Mars', authors: ['Oliver Sacks'] } },
12+
{ id: '4', volumeInfo: { title: 'Musicophilia', authors: ['Oliver Sacks'] } },
13+
{ id: '5', volumeInfo: { title: 'The Island of the Colourblind', authors: ['Oliver Sacks'] } },
14+
];
15+
816
@Injectable({ providedIn: 'root' })
917
export class GoogleBooksService {
1018
private readonly http = inject(HttpClient);
@@ -14,6 +22,9 @@ export class GoogleBooksService {
1422
.get<{ items: Book[] }>(
1523
'https://www.googleapis.com/books/v1/volumes?maxResults=5&orderBy=relevance&q=oliver%20sacks'
1624
)
17-
.pipe(map((books) => books.items || []));
25+
.pipe(
26+
map((books) => books.items || []),
27+
catchError(() => of(FALLBACK_BOOKS))
28+
);
1829
}
1930
}

0 commit comments

Comments
 (0)