Skip to content

Commit

Permalink
Merge pull request #37 from pnewstein/main
Browse files Browse the repository at this point in the history
Enum for languages
  • Loading branch information
NicKoehler authored Jul 1, 2023
2 parents 2ebd106 + 80f5e38 commit ecba5e1
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 22 deletions.
22 changes: 14 additions & 8 deletions pythorhead/comment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, List, Literal, Optional
from typing import Any, List, Literal, Optional, Union

from pythorhead.requestor import Request, Requestor
from pythorhead.types import CommentSortType, ListingType
from pythorhead.types import CommentSortType, ListingType, LanguageType


class Comment:
Expand Down Expand Up @@ -72,7 +72,7 @@ def create(
content: str,
form_id: Optional[str] = None,
parent_id: Optional[int] = None,
language_id: Optional[int] = None,
language_id: Union[int, LanguageType, None] = None,
) -> Optional[dict]:
"""
Create a comment.
Expand All @@ -82,7 +82,7 @@ def create(
content (str)
form_id (Optional[int], optional): Defaults to None.
parent_id (Optional[int], optional): Defaults to None.
language_id (Optional[int], optional): Defaults to None.
language_id (Union[int, LanguageType], optional): Defaults to None.
Returns:
Optional[dict]: created comment data if successful
Expand All @@ -97,7 +97,10 @@ def create(
if parent_id is not None:
create_comment["parent_id"] = parent_id
if language_id is not None:
create_comment["language_id"] = language_id
if isinstance(language_id, LanguageType):
create_comment["language_id"] = language_id.value
else:
create_comment["language_id"] = language_id

return self._requestor.api(
Request.POST,
Expand All @@ -111,7 +114,7 @@ def edit(
content: Optional[str] = None,
distinguished: Optional[bool] = None,
form_id: Optional[str] = None,
language_id: Optional[int] = None,
language_id: Union[int, LanguageType, None] = None,
) -> Optional[dict]:
"""
Edit a comment.
Expand All @@ -121,7 +124,7 @@ def edit(
content (Optional[str], optional): Defaults to None.
distinguished (Optional[bool], optional): Defaults to None.
form_id (Optional[str], optional): Defaults to None.
language_id (Optional[int], optional): Defaults to None.
language_id (Union[int, LanguageType], optional): Defaults to None.
Returns:
Optional[dict]: edited comment data if successful
Expand All @@ -136,7 +139,10 @@ def edit(
if form_id is not None:
edit_comment["form_id"] = form_id
if language_id is not None:
edit_comment["language_id"] = language_id
if isinstance(language_id, LanguageType):
edit_comment["language_id"] = language_id.value
else:
edit_comment["language_id"] = language_id

return self._requestor.api(
Request.PUT,
Expand Down
10 changes: 8 additions & 2 deletions pythorhead/community.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Any, List, Optional
from typing import List, Optional, Union


from pythorhead.requestor import Request, Requestor
from pythorhead.types import ListingType, SortType
from pythorhead.types import ListingType, SortType, LanguageType


class Community:
Expand All @@ -16,6 +17,7 @@ def create(
icon: Optional[str] = None,
nsfw: Optional[bool] = None,
posting_restricted_to_mods: Optional[bool] = None,
discussion_languages: Optional[List[Union[int, LanguageType]]] = None,
) -> Optional[dict]:
"""
Create a community
Expand All @@ -27,6 +29,7 @@ def create(
icon (str, optional): Defaults to None
nsfw (bool, optional): Defaults to None
posting_restricted_to_mods (bool, optional): Defaults to None
discussion_languages: (List[Union[int, LanguageType]], optional): Defaults to None
Returns:
Optional[dict]: post data if successful
Expand All @@ -43,6 +46,9 @@ def create(
new_community["nsfw"] = nsfw
if [posting_restricted_to_mods] is not None:
new_community["[posting_restricted_to_mods]"] = [posting_restricted_to_mods]
if discussion_languages is not None:
new_community["discussion_languages"] = [l.value for l in discussion_languages
if isinstance(l, LanguageType)]

return self._requestor.api(Request.POST, "/community", json=new_community)

Expand Down
22 changes: 14 additions & 8 deletions pythorhead/post.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, List, Literal, Optional
from typing import Any, List, Literal, Optional, Union

from pythorhead.requestor import Request, Requestor
from pythorhead.types import FeatureType, ListingType, SortType
from pythorhead.types import FeatureType, ListingType, SortType, LanguageType


class Post:
Expand Down Expand Up @@ -86,7 +86,7 @@ def create(
body: Optional[str] = None,
nsfw: Optional[bool] = None,
honeypot: Optional[str] = None,
language_id: Optional[int] = None,
language_id: Union[int, LanguageType, None] = None,
) -> Optional[dict]:
"""
Create a post
Expand All @@ -98,7 +98,7 @@ def create(
body (str, optional): Defaults to None.
nsfw (bool, optional): Defaults to None.
honeypot (str, optional): Defaults to None.
language_id (int, optional): Defaults to None.
language_id (Union[int, LanguageType], optional): Defaults to None.
Returns:
Optional[dict]: post data if successful
Expand All @@ -117,7 +117,10 @@ def create(
if honeypot is not None:
new_post["honeypot"] = honeypot
if language_id is not None:
new_post["language_id"] = language_id
if isinstance(language_id, LanguageType):
new_post["language_id"] = language_id.value
else:
new_post["language_id"] = language_id

return self._requestor.api(Request.POST, "/post", json=new_post)

Expand Down Expand Up @@ -167,7 +170,7 @@ def edit(
url: Optional[str] = None,
body: Optional[str] = None,
nsfw: Optional[bool] = None,
language_id: Optional[int] = None,
language_id: Union[int, LanguageType, None] = None,
) -> Optional[dict]:
"""
Expand All @@ -179,7 +182,7 @@ def edit(
url (str, optional): Defaults to None.
body (str, optional): Defaults to None.
nsfw (bool, optional): Defaults to None.
language_id (int, optional): Defaults to None.
language_id (Union[int, LanguageType], optional): Defaults to None.
Returns:
Optional[dict]: post data if successful
Expand All @@ -196,7 +199,10 @@ def edit(
if nsfw is not None:
edit_post["nsfw"] = nsfw
if language_id is not None:
edit_post["language_id"] = language_id
if isinstance(language_id, LanguageType):
edit_post["language_id"] = language_id.value
else:
edit_post["language_id"] = language_id

return self._requestor.api(Request.PUT, "/post", json=edit_post)

Expand Down
9 changes: 5 additions & 4 deletions pythorhead/site.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, List, Optional
from typing import Any, List, Optional, Union

from pythorhead.requestor import Request, Requestor
from pythorhead.types import ListingType
from pythorhead.types import ListingType, LanguageType


class Site:
Expand Down Expand Up @@ -37,7 +37,7 @@ def edit(
legal_information: Optional[str] = None,
application_email_admins: Optional[bool] = None,
hide_modlog_mod_names: Optional[bool] = None,
discussion_languages=None,
discussion_languages: Optional[List[Union[int, LanguageType]]] = None,
slur_filter_regex: Optional[str] = None,
actor_name_max_length: Optional[int] = None,
rate_limit_message: Optional[int] = None,
Expand Down Expand Up @@ -84,7 +84,7 @@ def edit(
legal_information (str, optional): Defaults to None.
application_email_admins (bool, optional): Defaults to None.
hide_modlog_mod_names (bool, optional): Defaults to None.
discussion_languages (LanguageType, optional): Defaults to None.
discussion_languages: (List[Union[int, LanguageType]], optional): Defaults to None
slur_filter_regex (str, optional): Defaults to None.
actor_name_max_length (int, optional): Defaults to None.
rate_limit_message (int, optional): Defaults to None.
Expand Down Expand Up @@ -112,6 +112,7 @@ def edit(
Returns:
Optional[dict]: post data if successful
"""
discussion_languages = [l.value for l in discussion_languages if isinstance(l, LanguageType)]
edit_site: dict[str, Any] = {
key: value for key, value in locals().items() if value is not None and key != "self"
}
Expand Down
1 change: 1 addition & 0 deletions pythorhead/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .feature import FeatureType
from .listing import ListingType
from .sort import CommentSortType, SortType
from .language import LanguageType
Loading

0 comments on commit ecba5e1

Please sign in to comment.