Skip to content

Commit

Permalink
Fix the threads endpoint to accept a date rather than a string
Browse files Browse the repository at this point in the history
  • Loading branch information
marclove committed Jul 15, 2024
1 parent 0f35768 commit 4193fef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
17 changes: 14 additions & 3 deletions src/pythreads/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: MIT

from dataclasses import dataclass
from datetime import datetime
from datetime import date, datetime
from enum import Enum
from json import JSONEncoder
from typing import Any, Dict, List, Literal, Optional, Union
Expand Down Expand Up @@ -648,13 +648,18 @@ async def thread(self, thread_id: str):

async def threads(
self,
since: Optional[str] = None,
until: Optional[str] = None,
since: Optional[Union[date, str]] = None,
until: Optional[Union[date, str]] = None,
limit: Optional[int] = None,
):
"""A paginated list of all threads created by the user
https://developers.facebook.com/docs/threads/threads-media#retrieve-a-list-of-all-a-user-s-threads
Args:
since: [optional] The starting `date` of the time window you are requesting
until: [optional] The ending `date` of the time window you are requesting
limit: [optional] The maximum number of threads to return. Defaults to 25.
Returns:
The JSON response as a dict
Expand Down Expand Up @@ -687,10 +692,16 @@ async def threads(
)
}

# Handling string is legacy. Need to deprecate and remove.
if since:
if isinstance(since, date):
since = since.isoformat()
params[PARAMS__SINCE] = since

# Handling string is legacy. Need to deprecate and remove.
if until:
if isinstance(until, date):
until = until.isoformat()
params[PARAMS__UNTIL] = until

if limit:
Expand Down
9 changes: 5 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: MIT

import unittest
from datetime import datetime, timedelta, timezone
from datetime import date, datetime, timedelta, timezone
from typing import Any
from unittest.mock import ANY, AsyncMock, MagicMock, call, patch

Expand Down Expand Up @@ -925,9 +925,10 @@ async def test_threads_with_options(self, mock_build_graph_api_url, mock_get):
mock_response(mock_get, expected)
mock_build_graph_api_url.return_value = "https://some-uri.com"

actual = await self.api.threads(
since="2024-05-01", until="2024-06-01", limit=10
)
since = date(2024, 5, 1)
until = date(2024, 6, 1)

actual = await self.api.threads(since=since, until=until, limit=10)

mock_build_graph_api_url.assert_called_once_with(
f"{self.credentials.user_id}/threads",
Expand Down

0 comments on commit 4193fef

Please sign in to comment.