Skip to content

Commit

Permalink
add services
Browse files Browse the repository at this point in the history
const.js
api.js
  • Loading branch information
danretegan committed Feb 10, 2024
1 parent 94b5873 commit 458661d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
Empty file added src/components/Test.jsx
Empty file.
Empty file added src/components/Test2.jsx
Empty file.
64 changes: 64 additions & 0 deletions src/services/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import axios from 'axios';
import { API_KEY, API_BASE_URL } from './const';

axios.defaults.baseURL = API_BASE_URL;

const params = {
params: {
api_key: API_KEY,
language: 'en-US',
},
};

export const fetchTrendingMovies = async () => {
try {
const response = await axios.get(`trending/movie/day`, params);
return response.data.results;
} catch (error) {
console.error('Error fetching trending movies:', error);
throw error; // Rethrow the error to propagate it to the caller
}
};

export const handleSearch = async movieName => {
try {
const response = await axios.get(
`/search/movie?query=${movieName}`,
params
);
return response.data.results;
} catch (error) {
console.error(`Error searching for "${movieName}":`, error);
throw error;
}
};

export const fetchMovieDetails = async movieId => {
try {
const response = await axios.get(`/movie/${movieId}`, params);
return response.data;
} catch (error) {
console.error('Error fetching movie details:', error);
throw error;
}
};

export const fetchMovieCast = async movieId => {
try {
const response = await axios.get(`movie/${movieId}/credits?`, params);
return response.data.cast;
} catch (error) {
console.error('Error fetching movie cast:', error);
throw error;
}
};

export const fetchMovieReviews = async movieId => {
try {
const response = await axios.get(`movie/${movieId}/reviews?`, params);
return response.data.results;
} catch (error) {
console.error('Error fetching movie reviews:', error);
throw error;
}
};
4 changes: 4 additions & 0 deletions src/services/const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const API_KEY = '2593d85031c68d42eea847806b363736';
const API_BASE_URL = 'https://api.themoviedb.org/3';

export { API_KEY, API_BASE_URL };

0 comments on commit 458661d

Please sign in to comment.