ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • #2 API LAYER
    초보를 위한 React Native(노마드코더강의)/#2 API LAYER 2020. 6. 24. 18:31

    #전체 파트 요약

    API 레이어를 만들어 보자.

     

    먼저, https://www.themoviedb.org/ <-- 회원 가입 후 해당 사이트의 API를 가져와보자.

     

    The Movie Database (TMDb)

    Welcome. Millions of movies, TV shows and people to discover. Explore now.

    www.themoviedb.org

    1. npm i axios 설치

     

    >api.js

    import axios from "axios";
    
    const TMDB_KEY = "8844d23ef9b87a0a517fde87a9093b7b";
    
    const makeRequest = (path, params) =>
      axios.get(`https://api.themoviedb.org/3${path}`, {
        params: {
          ...params,
          api_key: TMDB_KEY,
        },
      });
    
    const getAnything = async (path, params = {}) => {
      try {
        const {
          data: { results },
          data,
        } = await makeRequest(path, params);
        return [results || data, null];
      } catch (e) {
        console.log(e);
        return [null, e];
      }
    };
    
    export const movieApi = {
      nowPlaying: () => getAnything("/movie/now_playing"),
      popular: () => getAnything("/movie/popular"),
      upcoming: () => getAnything("/movie/upcoming", { region: "kr" }),
      search: (query) => getAnything("/search/movie", { query }),
      movie: (id) => getAnything(`/movie/${id}`),
      discover: () => getAnything("/discover/movie"),
    };
    
    export const tvApi = {
      today: () => getAnything("/tv/airing_today"),
      thisWeek: () => getAnything("/tv/on_the_air"),
      topRated: () => getAnything("/tv/top_rated"),
      popular: () => getAnything("/tv/popular"),
      search: (query) => getAnything("/search/tv", { query }),
      show: (id) => getAnything(`/tv/${id}`),
    };
    

     

    >Movies.js

    import React, { useEffect, useState } from "react";
    import { View, Text } from "react-native";
    import { movieApi } from "../api";
    
    export default () => {
      const [movies, setMovies] = useState({
        nowPlaying: [],
        popular: [],
        upcoming: [],
        nowPlayingError: null,
        popularError: null,
        upcomingError: null,
      });
      const getData = async () => {
        const [nowPlaying, nowPlayingError] = await movieApi.nowPlaying();
        const [popular, popularError] = await movieApi.popular();
        const [upcoming, upcomingError] = await movieApi.upcoming();
        setMovies({
          nowPlaying,
          popular,
          upcoming,
          nowPlayingError,
          popularError,
          upcomingError,
        });
      };
      useEffect(() => {
        getData();
      }, []);
    
      return (
        <View style={{ flex: 1, backgroundColor: "black" }}>
          <Text style={{ color: "white" }}>{movies.nowPlaying?.length}</Text>
        </View>
      );
    };
    

     

    >Tv.js

    import React, { useEffect, useState } from "react";
    import { View, Text } from "react-native";
    import { tvApi } from "../api";
    
    export default () => {
      const [shows, setShows] = useState({
        today: [],
        thisWeek: [],
        topRated: [],
        popular: [],
        todayError: null,
        thisWeekError: null,
        topRatedError: null,
        popularError: null,
      });
      const getData = async () => {
        const [today, todayError] = await tvApi.today();
        const [thisWeek, thisWeekError] = await tvApi.thisWeek();
        const [topRated, topRatedError] = await tvApi.topRated();
        const [popular, popularError] = await tvApi.popular();
        setShows({
          today,
          thisWeek,
          topRated,
          popular,
          todayError,
          thisWeekError,
          topRatedError,
          popularError,
        });
      };
      useEffect(() => {
        getData();
      }, []);
      return (
        <View>
          <Text>{shows.popular?.length}</Text>
        </View>
      );
    };
    

    >Favs.js

    import React, { useEffect, useState } from "react";
    import { View, Text } from "react-native";
    import { movieApi } from "../api";
    
    export default () => {
      const [movies, setMovies] = useState({
        results: [],
        error: null,
      });
      const getData = async () => {
        const [results, error] = await movieApi.discover();
        setMovies({
          results,
          error,
        });
      };
      useEffect(() => {
        getData();
      }, []);
      return (
        <View>
          <Text>{movies.results.length}</Text>
        </View>
      );
    };
    

     

Designed by Tistory.