Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
__pycache__/
dist/
*.egg-info/
21 changes: 18 additions & 3 deletions src/colony_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,27 @@ def delete_post(self, post_id: str) -> dict:

# ── Comments ─────────────────────────────────────────────────────

def create_comment(self, post_id: str, body: str) -> dict:
"""Comment on a post."""
def create_comment(
self,
post_id: str,
body: str,
parent_id: str | None = None,
) -> dict:
"""Comment on a post, optionally as a reply to another comment.

Args:
post_id: The post to comment on.
body: Comment text.
parent_id: If set, this comment is a reply to the comment
with this ID (threaded comments).
"""
payload: dict[str, str] = {"body": body, "client": "colony-sdk-python"}
if parent_id:
payload["parent_id"] = parent_id
return self._raw_request(
"POST",
f"/posts/{post_id}/comments",
body={"body": body, "client": "colony-sdk-python"},
body=payload,
)

def get_comments(self, post_id: str, page: int = 1) -> dict:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_api_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,26 @@ def test_create_comment_payload(self, mock_urlopen: MagicMock) -> None:
body = _last_body(mock_urlopen)
assert body == {"body": "Nice post!", "client": "colony-sdk-python"}

@patch("colony_sdk.client.urlopen")
def test_create_comment_with_parent_id(self, mock_urlopen: MagicMock) -> None:
mock_urlopen.return_value = _mock_response({"id": "c2"})
client = _authed_client()

client.create_comment("post-1", "I agree!", parent_id="c1")

body = _last_body(mock_urlopen)
assert body == {"body": "I agree!", "client": "colony-sdk-python", "parent_id": "c1"}

@patch("colony_sdk.client.urlopen")
def test_create_comment_without_parent_id(self, mock_urlopen: MagicMock) -> None:
mock_urlopen.return_value = _mock_response({"id": "c3"})
client = _authed_client()

client.create_comment("post-1", "Top-level comment")

body = _last_body(mock_urlopen)
assert "parent_id" not in body

@patch("colony_sdk.client.urlopen")
def test_get_comments(self, mock_urlopen: MagicMock) -> None:
mock_urlopen.return_value = _mock_response({"comments": [], "total": 0})
Expand Down
Loading