Feed pagination
This commit is contained in:
parent
d8a7f4ebaa
commit
5e477c53cb
4 changed files with 32 additions and 10 deletions
20
apiclient.py
20
apiclient.py
|
|
@ -2,6 +2,8 @@ import os
|
||||||
import token
|
import token
|
||||||
import requests
|
import requests
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
from pydantic import (
|
from pydantic import (
|
||||||
PrivateAttr, BaseModel, SecretStr, HttpUrl
|
PrivateAttr, BaseModel, SecretStr, HttpUrl
|
||||||
)
|
)
|
||||||
|
|
@ -27,23 +29,33 @@ class APIClient(BaseModel):
|
||||||
_access_token_timeout: int = PrivateAttr(default=0)
|
_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]:
|
if endpoint not in endpoints[self.environment]:
|
||||||
raise ValueError(f"Endpoint '{endpoint}' is not defined in the API client.")
|
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"]:
|
if method.upper() not in ["GET", "POST", "PUT", "DELETE"]:
|
||||||
raise ValueError(f"HTTP method '{method}' is not supported.")
|
raise ValueError(f"HTTP method '{method}' is not supported.")
|
||||||
|
|
||||||
method = method.lower()
|
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 = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
|
# Add the Authorization header with the access token if authentication is required
|
||||||
if authenticate:
|
if authenticate:
|
||||||
token = self._access_token
|
token = self._access_token
|
||||||
headers["Authorization"] = f"Bearer {token}"
|
headers["Authorization"] = f"Bearer {token}"
|
||||||
|
|
||||||
|
# Debugging output to show the request details before making the API call
|
||||||
if self.debug:
|
if self.debug:
|
||||||
print({
|
print({
|
||||||
"method": method.upper(),
|
"method": method.upper(),
|
||||||
|
|
@ -51,6 +63,8 @@ class APIClient(BaseModel):
|
||||||
"headers": headers,
|
"headers": headers,
|
||||||
"payload": data
|
"payload": data
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# Make the API request and handle potential exceptions
|
||||||
try:
|
try:
|
||||||
response = requests.request(method.upper(), url, json=data, headers=headers, timeout=20)
|
response = requests.request(method.upper(), url, json=data, headers=headers, timeout=20)
|
||||||
return_json = response.json()
|
return_json = response.json()
|
||||||
|
|
|
||||||
|
|
@ -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:
|
Returns:
|
||||||
A list of feed items.
|
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
|
return feed
|
||||||
|
|
|
||||||
|
|
@ -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:
|
Returns:
|
||||||
A list of feed items.
|
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
|
return feed
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,12 @@ if not API.is_token_valid():
|
||||||
# feed = API.run_job('feed.cafefeed')
|
# feed = API.run_job('feed.cafefeed')
|
||||||
# print(feed)
|
# print(feed)
|
||||||
# print("-----------------------------")
|
# 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(feed)
|
||||||
print("-----------------------------")
|
print("-----------------------------")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue