From edfb4c9faed3dde9454f4800d5a1b9a2523c68a9 Mon Sep 17 00:00:00 2001 From: jeanahan Date: Mon, 7 Jul 2025 23:25:17 -0700 Subject: [PATCH 1/3] Create BestBuyApi.py --- webscraper/api/BestBuyApi.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 webscraper/api/BestBuyApi.py diff --git a/webscraper/api/BestBuyApi.py b/webscraper/api/BestBuyApi.py new file mode 100644 index 0000000..606bbfd --- /dev/null +++ b/webscraper/api/BestBuyApi.py @@ -0,0 +1,32 @@ +import os +from dotenv import load_dotenv +import requests +from webscraper.api.interface import EbayABC + +load_dotenv() + +class BestBuyAPI(EbayABC): + def __init__(self): + self.api_key = os.getenv("BESTBUY_API_KEY") + self.base_url = "https://api.bestbuy.com/v1/products" + + if not self.api_key: + raise ValueError("BESTBUY_API_KEY not found. Check your .env file.") + + def retrieve_access_token(self) -> str: + return self.api_key + + def retrieve_response(self, httprequest: str, query: str) -> dict: + token = self.retrieve_access_token() + url = f"{httprequest}((categoryPath.id{query}))" + params = { + "apiKey": token, + "format": "json" + } + + response = requests.get(url, params=params) + response.raise_for_status() + return response.json() + + + From 9027a2379db4e6f16736031ca9758ede00339462 Mon Sep 17 00:00:00 2001 From: jeanahan Date: Fri, 25 Jul 2025 09:34:27 -0700 Subject: [PATCH 2/3] Wrapped init function with a try/finally --- webscraper/api/BestBuyApi.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/webscraper/api/BestBuyApi.py b/webscraper/api/BestBuyApi.py index 606bbfd..dbd45d8 100644 --- a/webscraper/api/BestBuyApi.py +++ b/webscraper/api/BestBuyApi.py @@ -7,12 +7,13 @@ class BestBuyAPI(EbayABC): def __init__(self): - self.api_key = os.getenv("BESTBUY_API_KEY") - self.base_url = "https://api.bestbuy.com/v1/products" - - if not self.api_key: - raise ValueError("BESTBUY_API_KEY not found. Check your .env file.") - + try: + self.api_key = os.getenv("BESTBUY_API_KEY") + self.base_url = "https://api.bestbuy.com/v1/products" + finally: + if not self.api_key: + raise Exception ("BESTBUY_API_KEY not found.") + def retrieve_access_token(self) -> str: return self.api_key From 034274d2bcbf2bb6b2ff610fd02701cca30661fc Mon Sep 17 00:00:00 2001 From: jeanahan Date: Sun, 10 Aug 2025 22:27:10 -0700 Subject: [PATCH 3/3] Update BestBuyApi.py to Try/Except --- webscraper/api/BestBuyApi.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/webscraper/api/BestBuyApi.py b/webscraper/api/BestBuyApi.py index dbd45d8..7c8cda1 100644 --- a/webscraper/api/BestBuyApi.py +++ b/webscraper/api/BestBuyApi.py @@ -10,9 +10,10 @@ def __init__(self): try: self.api_key = os.getenv("BESTBUY_API_KEY") self.base_url = "https://api.bestbuy.com/v1/products" - finally: if not self.api_key: raise Exception ("BESTBUY_API_KEY not found.") + except Exception as e: + raise Exception(f"Error initializing BestBuyAPI: {e}") from e def retrieve_access_token(self) -> str: return self.api_key