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
43 changes: 1 addition & 42 deletions src/colony_sdk/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ async def main():
from pathlib import Path
from types import TracebackType
from typing import Any
from urllib.parse import quote, urlencode

from colony_sdk.client import (
_UUID_RE,
Expand Down Expand Up @@ -473,8 +474,6 @@ async def get_posts(
search: str | None = None,
) -> dict:
"""List posts with optional filtering. See :meth:`ColonyClient.get_posts`."""
from urllib.parse import urlencode

params: dict[str, str] = {"sort": sort, "limit": str(limit)}
if offset:
params["offset"] = str(offset)
Expand Down Expand Up @@ -629,8 +628,6 @@ async def get_post_conversation(self, post_id: str) -> dict:

async def get_comments(self, post_id: str, page: int = 1) -> dict:
"""Get comments on a post (20 per page)."""
from urllib.parse import urlencode

params = urlencode({"page": str(page)})
return await self._raw_request("GET", f"/posts/{post_id}/comments?{params}")

Expand Down Expand Up @@ -788,8 +785,6 @@ async def create_group_conversation(
members: list[str],
) -> dict:
"""Create a new group conversation. See ColonyClient counterpart."""
from urllib.parse import urlencode

params = urlencode([("title", title), *(("members", m) for m in members)])
return await self._raw_request("POST", f"/messages/groups?{params}")

Expand All @@ -804,8 +799,6 @@ async def create_group_from_template(
title_override: str | None = None,
) -> dict:
"""Create a group from a pre-configured template."""
from urllib.parse import urlencode

pairs: list[tuple[str, str]] = [("template", template), *(("members", m) for m in members)]
if title_override is not None:
pairs.append(("title_override", title_override))
Expand All @@ -818,8 +811,6 @@ async def get_group_conversation(
offset: int = 0,
) -> dict:
"""Fetch a group conversation and its recent messages."""
from urllib.parse import urlencode

params = urlencode({"limit": str(limit), "offset": str(offset)})
return await self._raw_request("GET", f"/messages/groups/{conv_id}?{params}")

Expand All @@ -830,8 +821,6 @@ async def update_group_conversation(
description: str | None = None,
) -> dict:
"""Rename a group and/or change its description."""
from urllib.parse import urlencode

pairs: list[tuple[str, str]] = []
if title is not None:
pairs.append(("title", title))
Expand Down Expand Up @@ -873,8 +862,6 @@ async def list_group_members(self, conv_id: str) -> dict:

async def add_group_member(self, conv_id: str, username: str) -> dict:
"""Invite a user to a group conversation."""
from urllib.parse import urlencode

params = urlencode({"username": username})
return await self._raw_request("POST", f"/messages/groups/{conv_id}/members?{params}")

Expand All @@ -884,22 +871,16 @@ async def remove_group_member(self, conv_id: str, user_id: str) -> dict:

async def set_group_admin(self, conv_id: str, user_id: str, is_admin: bool) -> dict:
"""Promote or demote a group member to/from admin."""
from urllib.parse import urlencode

params = urlencode({"is_admin": "true" if is_admin else "false"})
return await self._raw_request("PUT", f"/messages/groups/{conv_id}/members/{user_id}/admin?{params}")

async def transfer_group_creator(self, conv_id: str, new_creator_username: str) -> dict:
"""Transfer the creator role to another current member."""
from urllib.parse import urlencode

params = urlencode({"new_creator_username": new_creator_username})
return await self._raw_request("POST", f"/messages/groups/{conv_id}/transfer-creator?{params}")

async def respond_to_group_invite(self, conv_id: str, accept: bool) -> dict:
"""Accept or decline a pending group invite."""
from urllib.parse import urlencode

params = urlencode({"accept": "true" if accept else "false"})
return await self._raw_request("POST", f"/messages/groups/{conv_id}/invite/respond?{params}")

Expand All @@ -915,8 +896,6 @@ async def mute_group_conversation(self, conv_id: str, until: str | None = None)
"""Mute a group conversation for the caller."""
suffix = ""
if until is not None:
from urllib.parse import urlencode

suffix = f"?{urlencode({'until': until})}"
return await self._raw_request("POST", f"/messages/groups/{conv_id}/mute{suffix}")

Expand All @@ -926,8 +905,6 @@ async def unmute_group_conversation(self, conv_id: str) -> dict:

async def snooze_group_conversation(self, conv_id: str, duration: str) -> dict:
"""Snooze a group conversation for the caller."""
from urllib.parse import urlencode

params = urlencode({"duration": duration})
return await self._raw_request("POST", f"/messages/groups/{conv_id}/snooze?{params}")

Expand All @@ -939,8 +916,6 @@ async def set_group_read_receipts(self, conv_id: str, show: bool | None = None)
"""Per-group read-receipt override."""
suffix = ""
if show is not None:
from urllib.parse import urlencode

suffix = f"?{urlencode({'show': 'true' if show else 'false'})}"
return await self._raw_request("PATCH", f"/messages/groups/{conv_id}/receipts{suffix}")

Expand All @@ -960,8 +935,6 @@ async def search_group_messages(
offset: int = 0,
) -> dict:
"""Full-text search inside a single group conversation."""
from urllib.parse import urlencode

params = urlencode({"q": q, "limit": str(limit), "offset": str(offset)})
return await self._raw_request("GET", f"/messages/groups/{conv_id}/search?{params}")

Expand All @@ -987,8 +960,6 @@ async def add_message_reaction(self, message_id: str, emoji: str) -> dict:

async def remove_message_reaction(self, message_id: str, emoji: str) -> dict:
"""Remove the caller's reaction with this emoji."""
from urllib.parse import quote

return await self._raw_request("DELETE", f"/messages/{message_id}/reactions/{quote(emoji, safe='')}")

async def edit_message(self, message_id: str, body: str) -> dict:
Expand All @@ -1010,8 +981,6 @@ async def toggle_star_message(self, message_id: str) -> dict:

async def list_saved_messages(self, limit: int = 50, offset: int = 0) -> dict:
"""List the caller's starred messages, newest-saved first."""
from urllib.parse import urlencode

params = urlencode({"limit": str(limit), "offset": str(offset)})
return await self._raw_request("GET", f"/messages/saved?{params}")

Expand All @@ -1022,8 +991,6 @@ async def forward_message(
comment: str = "",
) -> dict:
"""Forward a DM to another user as a new 1:1 message."""
from urllib.parse import urlencode

params = urlencode({"recipient_username": recipient_username, "comment": comment})
data = await self._raw_request("POST", f"/messages/{message_id}/forward?{params}")
return self._wrap(data, Message)
Expand Down Expand Up @@ -1181,8 +1148,6 @@ async def search(

Mirrors :meth:`ColonyClient.search` — see that for full param docs.
"""
from urllib.parse import urlencode

params: dict[str, str] = {"q": query, "limit": str(limit)}
if offset:
params["offset"] = str(offset)
Expand Down Expand Up @@ -1244,8 +1209,6 @@ async def directory(

Mirrors :meth:`ColonyClient.directory`.
"""
from urllib.parse import urlencode

params: dict[str, str] = {
"user_type": user_type,
"sort": sort,
Expand All @@ -1271,8 +1234,6 @@ async def unfollow(self, user_id: str) -> dict:

async def get_notifications(self, unread_only: bool = False, limit: int = 50) -> dict:
"""Get notifications (replies, mentions, etc.)."""
from urllib.parse import urlencode

params: dict[str, str] = {"limit": str(limit)}
if unread_only:
params["unread_only"] = "true"
Expand All @@ -1297,8 +1258,6 @@ async def mark_notification_read(self, notification_id: str) -> dict:

async def get_colonies(self, limit: int = 50) -> dict:
"""List all colonies, sorted by member count."""
from urllib.parse import urlencode

params = urlencode({"limit": str(limit)})
return await self._raw_request("GET", f"/colonies?{params}")

Expand Down
16 changes: 1 addition & 15 deletions src/colony_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pathlib import Path
from typing import Any
from urllib.error import HTTPError, URLError
from urllib.parse import urlencode
from urllib.parse import quote, urlencode
from urllib.request import Request, urlopen

from colony_sdk.colonies import COLONIES
Expand Down Expand Up @@ -1959,8 +1959,6 @@ def mute_group_conversation(self, conv_id: str, until: str | None = None) -> dic
"""
suffix = ""
if until is not None:
from urllib.parse import urlencode

suffix = f"?{urlencode({'until': until})}"
return self._raw_request("POST", f"/messages/groups/{conv_id}/mute{suffix}")

Expand Down Expand Up @@ -1994,8 +1992,6 @@ def snooze_group_conversation(self, conv_id: str, duration: str) -> dict:
Raises:
ColonyValidationError: 400 for invalid duration tokens.
"""
from urllib.parse import urlencode

params = urlencode({"duration": duration})
return self._raw_request("POST", f"/messages/groups/{conv_id}/snooze?{params}")

Expand All @@ -2022,8 +2018,6 @@ def set_group_read_receipts(self, conv_id: str, show: bool | None = None) -> dic
"""
suffix = ""
if show is not None:
from urllib.parse import urlencode

suffix = f"?{urlencode({'show': 'true' if show else 'false'})}"
return self._raw_request("PATCH", f"/messages/groups/{conv_id}/receipts{suffix}")

Expand Down Expand Up @@ -2080,8 +2074,6 @@ def search_group_messages(
ColonyAuthError: 403 if the caller is not a member.
ColonyValidationError: 400 for ``q`` < 2 chars.
"""
from urllib.parse import urlencode

params = urlencode({"q": q, "limit": str(limit), "offset": str(offset)})
return self._raw_request("GET", f"/messages/groups/{conv_id}/search?{params}")

Expand Down Expand Up @@ -2151,8 +2143,6 @@ def remove_message_reaction(self, message_id: str, emoji: str) -> dict:
Idempotent — removing a reaction the caller never placed is a
no-op (returns ``{removed: False, ...}``).
"""
from urllib.parse import quote

return self._raw_request("DELETE", f"/messages/{message_id}/reactions/{quote(emoji, safe='')}")

def edit_message(self, message_id: str, body: str) -> dict:
Expand Down Expand Up @@ -2217,8 +2207,6 @@ def list_saved_messages(self, limit: int = 50, offset: int = 0) -> dict:
``other_username`` (for 1:1) or ``conversation_title``
(for groups) so clients can render a "Go to thread" link.
"""
from urllib.parse import urlencode

params = urlencode({"limit": str(limit), "offset": str(offset)})
return self._raw_request("GET", f"/messages/saved?{params}")

Expand All @@ -2244,8 +2232,6 @@ def forward_message(
Returns:
The created :class:`Message` envelope (the forwarded copy).
"""
from urllib.parse import urlencode

params = urlencode({"recipient_username": recipient_username, "comment": comment})
data = self._raw_request("POST", f"/messages/{message_id}/forward?{params}")
return self._wrap(data, Message)
Expand Down