Skip to content

Commit

Permalink
Update vgde.py
Browse files Browse the repository at this point in the history
  • Loading branch information
veddevv authored Nov 5, 2024
1 parent 83c7708 commit bf18ad5
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions vgde.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,38 @@ def check_api_key():
if not API_KEY:
raise MissingAPIKeyError("API key not found. Please set the RAWG_API_KEY environment variable.")

def get_game_info(game_name):
def fetch_game_info_from_api(game_name):
"""
Fetches information about a game from the RAWG API.
Makes the API request to fetch game information.
Parameters:
game_name (str): The name of the game to search for.
Returns:
dict: A dictionary containing the game's information, or None if the game is not found.
requests.Response: The response object from the API request.
"""
url = f"{BASE_URL}/games"
params = {
'key': API_KEY,
'search': game_name
}

response = requests.get(url, params=params, timeout=REQUEST_TIMEOUT)
response.raise_for_status() # Raise an HTTPError for bad responses
return response

def get_game_info(game_name):
"""
Fetches information about a game from the RAWG API.
Parameters:
game_name (str): The name of the game to search for.
Returns:
dict: A dictionary containing the game's information, or None if the game is not found.
"""
try:
response = requests.get(url, params=params, timeout=REQUEST_TIMEOUT)
response.raise_for_status() # Raise an HTTPError for bad responses
response = fetch_game_info_from_api(game_name)
except requests.Timeout:
logging.error("The request timed out while trying to fetch game information.")
return None
Expand Down Expand Up @@ -136,7 +149,7 @@ def display_game_info(game_info):
else:
logging.warning("No game information to display.")

def main() -> object:
def main():
"""
Main function to run the script.
Prompts the user to enter the name of a game and displays its information.
Expand All @@ -155,12 +168,10 @@ def main() -> object:
sanitized_game_name = sanitize_game_name(args.game_name)
game_info = get_game_info(sanitized_game_name)
display_game_info(game_info)
return game_info # Return the game information
except InvalidInputError as e:
logging.error(f"Input validation error: {e}")
except Exception as e:
logging.error(f"An unexpected error occurred: {e}")
return None # Return None in case of an error

if __name__ == "__main__":
main()

0 comments on commit bf18ad5

Please sign in to comment.