0% found this document useful (0 votes)
16 views1 page

API

Uploaded by

riyasaha9398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

API

Uploaded by

riyasaha9398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# src/api.

py
import os
import traceback
from dataclasses import dataclass
import requests
from dotenv import load_dotenv

load_dotenv()

import logging
logger = logging.getLogger(__name__)

headers = {
"content-type": "application/octet-stream",
"X-RapidAPI-Host": "moviesdatabase.p.rapidapi.com",
"X-RapidAPI-Key": str(os.getenv('MOVIE_API_KEY')) # add candidate's api key to
.env
}

@dataclass
class APIRunner:
titles: list
n_pages: int

def fetch_response(self):
"""Fetch API responses

:return: List of page-wise responses


"""
responses = []
for title in self.titles:
for page in range(1, self.n_pages + 1):
try:
url =
f"https://moviesdatabase.p.rapidapi.com/titles/search/title/{title}"
params = {'pages': page}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
responses.append(data)
else:
logger.error(f"Request failed with status code
{response.status_code}")
except Exception as e:
logger.error(traceback.format_exc())
return responses

You might also like