-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move api calls to private api server
- Loading branch information
1 parent
f2b2527
commit 7e70167
Showing
4 changed files
with
216 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const axios = require('axios'); | ||
|
||
router.get('/', async (req, res) => { | ||
const author = req.query.author; | ||
const apiKey = process.env.API_KEY; | ||
try { | ||
const books = await fetchBooks(author, apiKey); | ||
res.json(books); | ||
} catch (error) { | ||
console.error('There has been a problem with your fetch operation:', error); | ||
res.status(500).json({ error: 'An error occurred while fetching books' }); | ||
} | ||
}); | ||
|
||
router.get('/book', async (req, res) => { | ||
const bookId = req.query.bookId; | ||
const apiKey = process.env.API_KEY; | ||
console.log( | ||
`https://www.googleapis.com/books/v1/volumes/${bookId}?key=${apiKey}`, | ||
); | ||
try { | ||
const response = await axios.get( | ||
`https://www.googleapis.com/books/v1/volumes/${bookId}?key=${apiKey}`, | ||
); | ||
res.json(response.data); | ||
} catch (error) { | ||
console.error('There has been a problem with your fetch operation:', error); | ||
res | ||
.status(500) | ||
.json({ error: 'An error occurred while fetching book details' }); | ||
} | ||
}); | ||
|
||
function fetchBooks(author, apiKey) { | ||
let request = `https://www.googleapis.com/books/v1/volumes?q=inauthor:${author}&key=${apiKey}&orderBy=newest&maxResults=40`; | ||
|
||
return axios | ||
.get(request) | ||
.then((response) => response.data.items) | ||
.catch((error) => | ||
console.error( | ||
'There has been a problem with your fetch operation:', | ||
error, | ||
), | ||
); | ||
} | ||
|
||
module.exports = router; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters