{"size": 1497, "ext": "js", "max_stars_repo_path": "src/services/MovieDbAPI.js", "max_stars_repo_name": "IsmaelB83/keepcoding-react-tvmoviedb", "max_stars_repo_stars_event_min_datetime": "2020-05-29T18:27:51.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-14T14:39:06.000Z", "max_issues_repo_path": "src/services/MovieDbAPI.js", "max_issues_repo_name": "IsmaelB83/keepcoding-react-tvmoviedb", "max_issues_repo_issues_event_min_datetime": "2019-10-16T22:03:43.000Z", "max_issues_repo_issues_event_max_datetime": "2019-10-16T22:03:43.000Z", "max_forks_repo_path": "src/services/MovieDbAPI.js", "max_forks_repo_name": "IsmaelB83/keepcoding-react-tvmoviedb", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 23.0307692308, "max_line_length": 95, "alphanum_fraction": 0.5731462926}
/**
 * Own modules
 */
import Movie from '../models/Movie';

//  '3471742b3361211db42df61f0446ef6b'

/**
 * Objeto API
 */
class MovieDbAPI {

  /**
   * Constructor
   */
  constructor(KEY) {
    this.API_URL = 'https://api.themoviedb.org/3';
    this.API_KEY = KEY;
  }
  
  getRequest = (url) => {
    return fetch(url,
      { method: "GET" },
      { Accept: "application/json, text/plain, */*" }
    ).then(res => res.json());
  }

  checkApìKey = () => {
    try {
      return this.getRequest(`${this.API_URL}/discover/movie?api_key=${this.API_KEY}`)
      .then(res => {
        return res.status_code !== 7;
      });
    } catch (error) {
      return false;
    }
  }

  discoverMovies = (year) => {
    let baseURL = `${this.API_URL}/discover/movie?api_key=${this.API_KEY}`;
    if (year)  { 
      baseURL = `${baseURL}&primary_release_year=${year}`; 
    }
    return this.getRequest(baseURL)
    .then(res => res.results.map(mov => new Movie(mov)))
  }

  getMovie =  (movieID) => {
      return this.getRequest(`${this.API_URL}/movie/${movieID}?api_key=${this.API_KEY}`)
      .then(res => {
        return new Movie(res)
      });
  }

  searchMovies = (query, year) => {
    let baseURL = `${this.API_URL}/search/movie?api_key=${this.API_KEY}&query=${query}&page=1`;
    if (year)  { 
      baseURL = `${baseURL}&primary_release_year=${year}`; 
    }
    return this.getRequest(baseURL)
    .then(res => res.results.map(mov => new Movie(mov)));
  }
}

export default MovieDbAPI;