Add helper function for stripping None values

This commit is contained in:
Ivan Habunek 2023-03-02 11:28:24 +01:00
parent 57824058e0
commit 7ed28b3bc3
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
2 changed files with 14 additions and 12 deletions

View File

@ -6,7 +6,7 @@ from urllib.parse import urlparse, urlencode, quote
from toot import http, CLIENT_NAME, CLIENT_WEBSITE
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'
@ -85,10 +85,9 @@ def update_account(
Update account credentials
https://docs.joinmastodon.org/methods/accounts/#update_credentials
"""
files = {"avatar": avatar, "header": header}
files = {k: v for k, v in files.items() if v is not None}
files = drop_empty_values({"avatar": avatar, "header": header})
data = {
data = drop_empty_values({
"bot": str_bool_nullable(bot),
"discoverable": str_bool_nullable(discoverable),
"display_name": display_name,
@ -97,8 +96,7 @@ def update_account(
"source[language]": language,
"source[privacy]": privacy,
"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)
@ -182,7 +180,9 @@ def post_status(
# if the request is retried.
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,
'media_ids': media_ids,
'visibility': visibility,
@ -192,11 +192,7 @@ def post_status(
'scheduled_at': scheduled_at,
'content_type': content_type,
'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()

View File

@ -7,6 +7,7 @@ import unicodedata
import warnings
from bs4 import BeautifulSoup
from typing import Dict
from toot.exceptions import ConsoleError
@ -154,3 +155,8 @@ def _use_existing_tmp_file(tmp_path) -> bool:
return char == "o"
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}