@meisens1/lotr-sdk
Version:
Lord of the Rings SDK for the one API v2
72 lines (67 loc) • 2.35 kB
JavaScript
const axios = require('axios');
const Movie = require('../models/movie');
const { baseUrl, accessToken } = require('../config');
//use either the config's access token, or the passed in access token from the client
//depending on if you are using the web API or the SDK
//getMovie returns a list of movies which are paginated by the default values page 1, limit 100
async function getMovies(aToken = accessToken, page = 1, limit = 100) {
try {
const response = await axios.get(`${baseUrl}/movie`, {
headers: {
Authorization: `Bearer ${aToken}`,
},
params: {
page,
limit,
},
});
const { docs, total, limit: responseLimit, offset, page: currentPage, pages } = response.data;
const movies = docs.map(movie => new Movie(
movie._id,
movie.name,
movie.runtimeInMinutes,
movie.budgetInMillions,
movie.boxOfficeRevenueInMillions,
movie.academyAwardNominations,
movie.academyAwardWins,
movie.rottenTomatoesScore
));
return {
movies,
pagination: {
total,
limit: responseLimit,
offset,
page: currentPage,
pages,
},
};
} catch (error) {
console.error('Error retrieving movies:', error);
throw error;
}
}
async function getMovieById(aToken = accessToken, id) {
try {
const response = await axios.get(`${baseUrl}/movie/${id}`, {
headers: {
Authorization: `Bearer ${aToken}`,
},
});
const movie = response.data;
return response.data.docs.map(movie => new Movie(
movie._id,
movie.name,
movie.runtimeInMinutes,
movie.budgetInMillions,
movie.boxOfficeRevenueInMillions,
movie.academyAwardNominations,
movie.academyAwardWins,
movie.rottenTomatoesScore
));
} catch (error) {
console.error(`Error retrieving movie with ID ${id}:`, error);
throw error;
}
}
module.exports = { getMovies, getMovieById };