From cd1f0a2ae32ea36825168cd5ca026a11fd49cb59 Mon Sep 17 00:00:00 2001 From: simonwt Date: Thu, 16 Apr 2026 21:08:54 +0100 Subject: [PATCH] Return token data from handle_token Notes for dev --- development.md | 19 ++++++++++++++++-- pyproject.toml | 2 +- src/trustcafeapiwrapper/apiclient.py | 6 ++++-- testing.py | 4 +++- usethis.py | 30 ++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 usethis.py diff --git a/development.md b/development.md index e50b761..17ec664 100644 --- a/development.md +++ b/development.md @@ -26,9 +26,24 @@ potentially misleading. 2. Make more wrappers 3. Write more documentation 4. Wrappers should also accept actual keys for when you know them -5. Add utility to manage the token easily to encourage reuse over -new tokens every time +5. Use this: +```python +try: + # NOTE: Maybe define self.session = requests.Session() in an __init__ or similar and use it here instead of the 'with' block for better performance. + # Session for connection pooling and performance improvement + with requests.Session() as session: + # Update session headers once instead of every request + session.headers.update(headers) + + # GET requests to fetch normal and removed posts + res_n = session.get(url_normal, timeout=60) + # res_r = session.get(url_removed, timeout=60) + + # Checking if both requests are good before proceeding + res_n.raise_for_status() + # res_r.raise_for_status() +``` Trust diff --git a/pyproject.toml b/pyproject.toml index c2eafcc..bd9908f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "trustcafeapiwrapper" -version = "0.1.0.11" +version = "0.1.0.12" description = "Wraps the Trust Cafe API" readme = "README.md" requires-python = ">=3.11" diff --git a/src/trustcafeapiwrapper/apiclient.py b/src/trustcafeapiwrapper/apiclient.py index 658c862..ef6b664 100644 --- a/src/trustcafeapiwrapper/apiclient.py +++ b/src/trustcafeapiwrapper/apiclient.py @@ -170,9 +170,11 @@ class APIClient(BaseModel): # Get a new one if we don't have one or if the existing one is expired if not self.is_token_valid(): - tokendata = self.sign_in() + token_data = self.sign_in() with open(token_data_path, "w") as f: - json.dump(tokendata, f, indent=2) + json.dump(token_data, f, indent=2) + + return token_data def run_job(self, job_function, *args, **kwargs): """ diff --git a/testing.py b/testing.py index 72c8cc2..a3c6dde 100644 --- a/testing.py +++ b/testing.py @@ -26,7 +26,9 @@ API = APIClient( # Keep a token cache to avoid unnecessary sign-ins during development. # (In production, you'd handle this more robustly and securely) -API.handle_token() # This will load the token from file if it exists and is valid, or sign in to get a new one if not. +token_data = API.handle_token() # This will load the token from file if it exists and is valid, or sign in to get a new one if not. +if token_data is None or 'access_token' not in token_data: + raise Exception("Failed to obtain a valid access token. Please check your credentials and token handling.") ''' END IMPORTANT BIT diff --git a/usethis.py b/usethis.py new file mode 100644 index 0000000..e1f610c --- /dev/null +++ b/usethis.py @@ -0,0 +1,30 @@ + async def grab_latest_posts(self) -> bool: + """ + Fetches the latest posts from the API. + + If it's the first run (_time_last_scan is 0), checks all posts. + Otherwise, checks only posts created after the last scan time. + Looks in the foryou feed. + + Returns: + bool: True if posts are successfully fetched and processed. + """ + # Grabbing latest posts... + print("\nBig Brother is watching you...") + headers = {"Authorization": f"Bearer {self._access_token}"} + url_normal = f"{str(self.api_url_content)}/post/foryou" + # url_removed = f"{str(self.api_url_content)}/post/removed" + try: + # NOTE: Maybe define self.session = requests.Session() in an __init__ or similar and use it here instead of the 'with' block for better performance. + # Session for connection pooling and performance improvement + with requests.Session() as session: + # Update session headers once instead of every request + session.headers.update(headers) + + # GET requests to fetch normal and removed posts + res_n = session.get(url_normal, timeout=60) + # res_r = session.get(url_removed, timeout=60) + + # Checking if both requests are good before proceeding + res_n.raise_for_status() + # res_r.raise_for_status() \ No newline at end of file