mirror of
https://github.com/ihabunek/toot.git
synced 2024-09-22 04:25:55 -04:00
Add helper function for stripping None values
This commit is contained in:
parent
57824058e0
commit
7ed28b3bc3
20
toot/api.py
20
toot/api.py
@ -6,7 +6,7 @@ from urllib.parse import urlparse, urlencode, quote
|
|||||||
|
|
||||||
from toot import http, CLIENT_NAME, CLIENT_WEBSITE
|
from toot import http, CLIENT_NAME, CLIENT_WEBSITE
|
||||||
from toot.exceptions import AuthenticationError
|
from toot.exceptions import AuthenticationError
|
||||||
from toot.utils import str_bool, str_bool_nullable
|
from toot.utils import drop_empty_values, str_bool, str_bool_nullable
|
||||||
|
|
||||||
SCOPES = 'read write follow'
|
SCOPES = 'read write follow'
|
||||||
|
|
||||||
@ -85,10 +85,9 @@ def update_account(
|
|||||||
Update account credentials
|
Update account credentials
|
||||||
https://docs.joinmastodon.org/methods/accounts/#update_credentials
|
https://docs.joinmastodon.org/methods/accounts/#update_credentials
|
||||||
"""
|
"""
|
||||||
files = {"avatar": avatar, "header": header}
|
files = drop_empty_values({"avatar": avatar, "header": header})
|
||||||
files = {k: v for k, v in files.items() if v is not None}
|
|
||||||
|
|
||||||
data = {
|
data = drop_empty_values({
|
||||||
"bot": str_bool_nullable(bot),
|
"bot": str_bool_nullable(bot),
|
||||||
"discoverable": str_bool_nullable(discoverable),
|
"discoverable": str_bool_nullable(discoverable),
|
||||||
"display_name": display_name,
|
"display_name": display_name,
|
||||||
@ -97,8 +96,7 @@ def update_account(
|
|||||||
"source[language]": language,
|
"source[language]": language,
|
||||||
"source[privacy]": privacy,
|
"source[privacy]": privacy,
|
||||||
"source[sensitive]": str_bool_nullable(sensitive),
|
"source[sensitive]": str_bool_nullable(sensitive),
|
||||||
}
|
})
|
||||||
data = {k: v for k, v in data.items() if v is not None}
|
|
||||||
|
|
||||||
return http.patch(app, user, "/api/v1/accounts/update_credentials", files=files, data=data)
|
return http.patch(app, user, "/api/v1/accounts/update_credentials", files=files, data=data)
|
||||||
|
|
||||||
@ -182,7 +180,9 @@ def post_status(
|
|||||||
# if the request is retried.
|
# if the request is retried.
|
||||||
headers = {"Idempotency-Key": uuid.uuid4().hex}
|
headers = {"Idempotency-Key": uuid.uuid4().hex}
|
||||||
|
|
||||||
json = {
|
# Strip keys for which value is None
|
||||||
|
# Sending null values doesn't bother Mastodon, but it breaks Pleroma
|
||||||
|
json = drop_empty_values({
|
||||||
'status': status,
|
'status': status,
|
||||||
'media_ids': media_ids,
|
'media_ids': media_ids,
|
||||||
'visibility': visibility,
|
'visibility': visibility,
|
||||||
@ -192,11 +192,7 @@ def post_status(
|
|||||||
'scheduled_at': scheduled_at,
|
'scheduled_at': scheduled_at,
|
||||||
'content_type': content_type,
|
'content_type': content_type,
|
||||||
'spoiler_text': spoiler_text
|
'spoiler_text': spoiler_text
|
||||||
}
|
})
|
||||||
|
|
||||||
# Strip keys for which value is None
|
|
||||||
# Sending null values doesn't bother Mastodon, but it breaks Pleroma
|
|
||||||
json = {k: v for k, v in json.items() if v is not None}
|
|
||||||
|
|
||||||
return http.post(app, user, '/api/v1/statuses', json=json, headers=headers).json()
|
return http.post(app, user, '/api/v1/statuses', json=json, headers=headers).json()
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import unicodedata
|
|||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
from toot.exceptions import ConsoleError
|
from toot.exceptions import ConsoleError
|
||||||
|
|
||||||
@ -154,3 +155,8 @@ def _use_existing_tmp_file(tmp_path) -> bool:
|
|||||||
return char == "o"
|
return char == "o"
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def drop_empty_values(data: Dict) -> Dict:
|
||||||
|
"""Remove keys whose values are null"""
|
||||||
|
return {k: v for k, v in data.items() if v is not None}
|
||||||
|
Loading…
Reference in New Issue
Block a user