Feed pagination

This commit is contained in:
simonwt 2026-03-31 23:13:08 +01:00
parent d8a7f4ebaa
commit 5e477c53cb
4 changed files with 32 additions and 10 deletions

View file

@ -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()