diff --git a/apiclient.py b/apiclient.py index 9962d3c..52731bb 100644 --- a/apiclient.py +++ b/apiclient.py @@ -2,6 +2,8 @@ import os import token import requests import simplejson as json +from urllib.parse import urlencode + from pydantic import ( PrivateAttr, BaseModel, SecretStr, HttpUrl ) @@ -27,23 +29,33 @@ class APIClient(BaseModel): _access_token_timeout: int = PrivateAttr(default=0) - def make_request(self, method: str, endpoint: str, path: str, data: dict = None, authenticate: bool = True) -> dict: + def make_request(self, method: str, endpoint: str, path: str, data: dict = None, authenticate: bool = True, query_params: dict = None) -> dict: + + # Make sure the endpoint is defined in the endpoints dictionary if endpoint not in endpoints[self.environment]: raise ValueError(f"Endpoint '{endpoint}' is not defined in the API client.") + # Make sure the HTTP method is valid if method.upper() not in ["GET", "POST", "PUT", "DELETE"]: raise ValueError(f"HTTP method '{method}' is not supported.") - method = method.lower() - url = f"{endpoints[self.environment][endpoint]}{path}" + # Construct the full URL for the API request + url = f"{endpoints[self.environment][endpoint]}{path}" + # Append query parameters to the URL if provided + if query_params is not None and isinstance(query_params, dict): + url += f"?{urlencode(query_params)}" + + # Set up headers for the request headers = { "Content-Type": "application/json", } + # Add the Authorization header with the access token if authentication is required if authenticate: token = self._access_token headers["Authorization"] = f"Bearer {token}" + # Debugging output to show the request details before making the API call if self.debug: print({ "method": method.upper(), @@ -51,6 +63,8 @@ class APIClient(BaseModel): "headers": headers, "payload": data }) + + # Make the API request and handle potential exceptions try: response = requests.request(method.upper(), url, json=data, headers=headers, timeout=20) return_json = response.json() diff --git a/jobs/feed/cafefeed.py b/jobs/feed/cafefeed.py index e42bee9..dad797c 100644 --- a/jobs/feed/cafefeed.py +++ b/jobs/feed/cafefeed.py @@ -1,9 +1,9 @@ -def cafefeed(API): +def cafefeed(API, lastEvaluatedKey=None): """ - List all of a token's user's feed items. + List all of a token's user's feed items from their Cafe Feed Returns: A list of feed items. """ - feed = API.make_request("GET", "audrey", "feed/foryou", authenticate=True) + feed = API.make_request("GET", "audrey", "feed/foryou", authenticate=True, query_params=lastEvaluatedKey) return feed diff --git a/jobs/feed/following.py b/jobs/feed/following.py index 8aec0bf..87ed162 100644 --- a/jobs/feed/following.py +++ b/jobs/feed/following.py @@ -1,9 +1,11 @@ -def followingfeed(API): +def followingfeed(API, lastEvaluatedKey=None): """ - List all of a token's user's feed items from the users they are following. + List all of a token's user's feed items from the users and branches they are following. Returns: A list of feed items. """ - feed = API.make_request("GET", "audrey", "feed/following", authenticate=True) + + + feed = API.make_request("GET", "audrey", "feed/following", authenticate=True, query_params=lastEvaluatedKey) return feed diff --git a/testing.py b/testing.py index c30cab5..5526497 100644 --- a/testing.py +++ b/testing.py @@ -104,6 +104,12 @@ if not API.is_token_valid(): # feed = API.run_job('feed.cafefeed') # print(feed) # print("-----------------------------") -feed = API.run_job('feed.followingfeed') +# feed = API.run_job('feed.followingfeed') +# print(feed) +# print("-----------------------------") +feed = API.run_job('feed.followingfeed', { + 'pk': 'maintrunk#maintrunk', + 'sk': 'post#1774951384-98fe38df' +}) print(feed) print("-----------------------------") \ No newline at end of file