Return http codes from get_json

main
Bob Mottram 2023-08-13 10:58:02 +01:00
parent 58f110e65d
commit ad7bf3c033
15 changed files with 79 additions and 35 deletions

View File

@ -11,6 +11,7 @@ import os
import json
import time
from datetime import datetime
from session import get_json_valid
from utils import remove_eol
from utils import has_object_string
from utils import has_object_string_object
@ -1744,7 +1745,7 @@ def get_blocks_via_server(session, nickname: str, password: str,
print('Blocked collection request to: ' + url)
blocked_json = get_json(signing_priv_key_pem, session, url, headers, None,
debug, version, http_prefix, None)
if not blocked_json:
if not get_json_valid(blocked_json):
if debug:
print('DEBUG: GET blocked collection failed for c2s to ' + url)
# return 5

View File

@ -11,6 +11,7 @@ import os
import datetime
from session import url_exists
from session import get_json
from session import get_json_valid
from utils import load_json
from utils import save_json
from utils import get_file_case_insensitive
@ -237,7 +238,7 @@ def get_person_pub_key(base_dir: str, session, person_url: str,
get_json(signing_priv_key_pem,
session, person_url, as_header, None, debug,
project_version, http_prefix, person_domain)
if not person_json:
if not get_json_valid(person_json):
return None
pub_key, _ = get_actor_public_key_from_id(person_json, original_person_url)
if not pub_key:

View File

@ -17,6 +17,7 @@ from utils import load_json
from utils import harmless_markup
from keys import get_instance_actor_key
from session import get_json
from session import get_json_valid
def _get_conversation_filename(base_dir: str, nickname: str, domain: str,
@ -137,9 +138,9 @@ def download_conversation_posts(authorized: bool, session,
as_header, None, debug, __version__,
http_prefix, domain)
if debug:
if not post_json_object:
if not get_json_valid(post_json_object):
print(post_id + ' returned no json')
while post_json_object:
while get_json_valid(post_json_object):
if not isinstance(post_json_object, dict):
break
if not has_object_dict(post_json_object):
@ -205,6 +206,6 @@ def download_conversation_posts(authorized: bool, session,
as_header, None, debug, __version__,
http_prefix, domain)
if debug:
if not post_json_object:
if not get_json_valid(post_json_object):
print(post_id + ' returned no json')
return conversation_view

View File

@ -38,6 +38,7 @@ from acceptreject import create_reject
from webfinger import webfinger_handle
from auth import create_basic_auth_header
from session import get_json
from session import get_json_valid
from session import post_json
from followerSync import remove_followers_sync
@ -1221,7 +1222,7 @@ def get_following_via_server(session, nickname: str, password: str,
following_json = \
get_json(signing_priv_key_pem, session, url, headers, {}, debug,
project_version, http_prefix, domain, 10, True)
if not following_json:
if not get_json_valid(following_json):
if debug:
print('DEBUG: GET following list failed for c2s to ' + url)
return 5
@ -1259,7 +1260,7 @@ def get_followers_via_server(session, nickname: str, password: str,
followers_json = \
get_json(signing_priv_key_pem, session, url, headers, {}, debug,
project_version, http_prefix, domain, 10, True)
if not followers_json:
if not get_json_valid(followers_json):
if debug:
print('DEBUG: GET followers list failed for c2s to ' + url)
return 5
@ -1298,7 +1299,7 @@ def get_follow_requests_via_server(session,
followers_json = \
get_json(signing_priv_key_pem, session, url, headers, {}, debug,
project_version, http_prefix, domain, 10, True)
if not followers_json:
if not get_json_valid(followers_json):
if debug:
print('DEBUG: GET follow requests list failed for c2s to ' + url)
return 5
@ -1338,7 +1339,7 @@ def approve_follow_request_via_server(session,
approve_html = \
get_json(signing_priv_key_pem, session, url, headers, {}, debug,
project_version, http_prefix, domain, 10, True)
if not approve_html:
if not get_json_valid(approve_html):
if debug:
print('DEBUG: GET approve follow request failed for c2s to ' + url)
return 5
@ -1378,7 +1379,7 @@ def deny_follow_request_via_server(session,
deny_html = \
get_json(signing_priv_key_pem, session, url, headers, {}, debug,
project_version, http_prefix, domain, 10, True)
if not deny_html:
if not get_json_valid(deny_html):
if debug:
print('DEBUG: GET deny follow request failed for c2s to ' + url)
return 5

View File

@ -67,6 +67,7 @@ from utils import get_group_paths
from utils import local_actor_url
from utils import dangerous_svg
from utils import text_in_file
from session import get_json_valid
from session import create_session
from session import get_json
from webfinger import webfinger_handle
@ -1748,7 +1749,7 @@ def get_actor_json(host_domain: str, handle: str, http: bool, gnunet: bool,
get_json(signing_priv_key_pem, session, person_url, as_header,
None, debug, __version__, http_prefix, host_domain,
20, quiet)
if person_json:
if get_json_valid(person_json):
if not quiet:
pprint(person_json)
return person_json, as_header

View File

@ -29,6 +29,7 @@ from session import get_json
from session import post_json
from session import post_json_string
from session import post_image
from session import get_json_valid
from webfinger import webfinger_handle
from httpsig import create_signed_header
from siteactive import site_is_active
@ -242,7 +243,7 @@ def parse_user_feed(signing_priv_key_pem: str,
feed_json = \
get_json(signing_priv_key_pem, session, feed_url, as_header, None,
debug, project_version, http_prefix, origin_domain)
if not feed_json:
if not get_json_valid(feed_json):
profile_str = 'https://www.w3.org/ns/activitystreams'
accept_str = 'application/ld+json; profile="' + profile_str + '"'
if as_header['Accept'] != accept_str:
@ -252,7 +253,7 @@ def parse_user_feed(signing_priv_key_pem: str,
feed_json = get_json(signing_priv_key_pem, session, feed_url,
as_header, None, debug, project_version,
http_prefix, origin_domain)
if not feed_json:
if not get_json_valid(feed_json):
if debug:
print('No user feed was returned')
return None
@ -315,7 +316,7 @@ def _get_person_box_actor(session, base_dir: str, actor: str,
person_json = \
get_json(signing_priv_key_pem, session, actor, as_header, None,
debug, project_version, http_prefix, origin_domain)
if person_json:
if get_json_valid(person_json):
return person_json
as_header = {
'Accept': 'application/ld+json; profile="' + profile_str + '"'
@ -323,7 +324,7 @@ def _get_person_box_actor(session, base_dir: str, actor: str,
person_json = \
get_json(signing_priv_key_pem, session, actor, as_header, None,
debug, project_version, http_prefix, origin_domain)
if person_json:
if get_json_valid(person_json):
return person_json
print('Unable to get actor for ' + actor + ' ' + str(source_id))
if not signing_priv_key_pem:
@ -604,7 +605,7 @@ def _get_posts(session, outbox_url: str, max_posts: int,
get_json(signing_priv_key_pem, session, this_item,
as_header2, None, debug, __version__,
http_prefix, origin_domain)
if not this_item:
if not get_json_valid(this_item):
continue
content = get_base_content_from_post(this_item, system_language)
@ -5114,7 +5115,7 @@ def download_follow_collection(signing_priv_key_pem: str,
followers_json = \
get_json(signing_priv_key_pem, session, url, session_headers, None,
debug, __version__, http_prefix, None)
if followers_json:
if get_json_valid(followers_json):
if followers_json.get('orderedItems'):
for follower_actor in followers_json['orderedItems']:
if follower_actor not in result:
@ -5509,7 +5510,7 @@ def download_announce(session, base_dir: str, http_prefix: str,
as_header, None, debug, project_version,
http_prefix, domain)
if not announced_json:
if not get_json_valid(announced_json):
return None
if not isinstance(announced_json, dict):
@ -6163,7 +6164,7 @@ def c2s_box_json(session, nickname: str, password: str,
box_json = get_json(signing_priv_key_pem, session, url, headers, None,
debug, __version__, http_prefix, None)
if box_json is not None and debug:
if get_json_valid(box_json) and debug:
print('DEBUG: GET c2s_box_json success')
return box_json

View File

@ -105,14 +105,34 @@ def _get_json_request(session, url: str, domain_full: str, session_headers: {},
if result.status_code != 200:
if result.status_code == 401:
print("WARN: get_json " + url + ' rejected by secure mode')
return {
"error": 401
}
elif result.status_code == 403:
print('WARN: get_json Forbidden url: ' + url)
return {
"error": 403
}
elif result.status_code == 404:
print('WARN: get_json Not Found url: ' + url)
return {
"error": 404
}
elif result.status_code == 410:
print('WARN: get_json no longer available url: ' + url)
return {
"error": 410
}
elif result.status_code == 303:
print('WARN: get_json redirect not permitted: ' + url)
return {
"error": 303
}
elif result.status_code == 301:
print('WARN: get_json moved permanently: ' + url)
return {
"error": 301
}
else:
session_headers2 = session_headers.copy()
if session_headers2.get('Authorization'):
@ -223,6 +243,16 @@ def _get_json_signed(session, url: str, domain_full: str, session_headers: {},
debug, return_json)
def get_json_valid(test_json: {}) -> bool:
"""Is the given get_json result valid?
"""
if not test_json:
return False
if 'error' in test_json:
return False
return True
def get_json(signing_priv_key_pem: str,
session, url: str, headers: {}, params: {}, debug: bool,
version: str = __version__, http_prefix: str = 'https',

View File

@ -22,6 +22,7 @@ from posts import get_person_box
from session import post_json
from session import post_image
from session import create_session
from session import get_json_valid
from utils import acct_handle_dir
from utils import remove_eol
from utils import has_object_string_type
@ -1021,7 +1022,7 @@ def get_shared_items_catalog_via_server(session, nickname: str, password: str,
print('Shared items catalog request to: ' + url)
catalog_json = get_json(signing_priv_key_pem, session, url, headers, None,
debug, __version__, http_prefix, None)
if not catalog_json:
if not get_json_valid(catalog_json):
if debug:
print('DEBUG: GET shared items catalog failed for c2s to ' + url)
# return 5
@ -1056,7 +1057,7 @@ def get_offers_via_server(session, nickname: str, password: str,
print('Offers collection request to: ' + url)
offers_json = get_json(signing_priv_key_pem, session, url, headers, None,
debug, __version__, http_prefix, None)
if not offers_json:
if not get_json_valid(offers_json):
if debug:
print('DEBUG: GET offers collection failed for c2s to ' + url)
# return 5
@ -1091,7 +1092,7 @@ def get_wanted_via_server(session, nickname: str, password: str,
print('Wanted collection request to: ' + url)
wanted_json = get_json(signing_priv_key_pem, session, url, headers, None,
debug, __version__, http_prefix, None)
if not wanted_json:
if not get_json_valid(wanted_json):
if debug:
print('DEBUG: GET wanted collection failed for c2s to ' + url)
# return 5
@ -1658,7 +1659,7 @@ def _update_federated_shares_cache(session, shared_items_federated_domains: [],
as_header['Authorization'] = tokens_json[federated_domain_full]
catalog_json = get_json(session, url, as_header, None,
debug, __version__, http_prefix, None)
if not catalog_json:
if not get_json_valid(catalog_json):
print('WARN: failed to download shared items catalog for ' +
federated_domain_full)
continue

View File

@ -34,6 +34,7 @@ from cache import store_person_in_cache
from cache import get_person_from_cache
from threads import thread_with_trace
from daemon import run_daemon
from session import get_json_valid
from session import create_session
from session import get_json
from posts import convert_post_content_to_html
@ -1885,7 +1886,7 @@ def test_shared_items_federation(base_dir: str) -> None:
get_json(signing_priv_key_pem, session_client,
'http://' + bob_address + '/@actor', test_headers, {}, True,
__version__, 'http', 'somedomain.or.other', 10, False)
if not bob_instance_actor_json:
if not get_json_valid(bob_instance_actor_json):
print('Unable to get json for ' + 'http://' + bob_address + '/@actor')
assert bob_instance_actor_json
pprint(bob_instance_actor_json)
@ -2202,7 +2203,7 @@ def test_shared_items_federation(base_dir: str) -> None:
signing_priv_key_pem = None
catalog_json = get_json(signing_priv_key_pem, session_alice, url, headers,
None, True)
assert catalog_json
assert get_json_valid(catalog_json)
pprint(catalog_json)
assert 'DFC:supplies' in catalog_json
assert len(catalog_json.get('DFC:supplies')) == 3
@ -2353,7 +2354,7 @@ def test_group_follow(base_dir: str) -> None:
signing_priv_key_pem = None
outbox_json = get_json(signing_priv_key_pem, session, alice_outbox,
as_header, None, True, __version__, 'http', None)
assert outbox_json
assert get_json_valid(outbox_json)
pprint(outbox_json)
assert outbox_json['type'] == 'OrderedCollection'
assert 'first' in outbox_json
@ -2365,7 +2366,7 @@ def test_group_follow(base_dir: str) -> None:
outbox_json = get_json(signing_priv_key_pem, session,
first_page, as_header,
None, True, __version__, 'http', None)
assert outbox_json
assert get_json_valid(outbox_json)
pprint(outbox_json)
assert 'orderedItems' in outbox_json
assert outbox_json['type'] == 'OrderedCollectionPage'

View File

@ -20,6 +20,7 @@ from webapp_utils import get_broken_link_substitute
from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer
from webapp_utils import html_keyboard_navigation
from session import get_json_valid
from session import get_json
@ -62,7 +63,7 @@ def _html_podcast_chapters(link_url: str,
get_json(None, curr_session, chapters_url,
as_header, None, debug, __version__,
http_prefix, domain)
if not chapters_json:
if not get_json_valid(chapters_json):
return ''
if not chapters_json.get('chapters'):
return ''

View File

@ -109,6 +109,7 @@ from maps import set_map_preferences_coords
from maps import set_map_preferences_url
from maps import geocoords_from_map_link
from maps import get_location_from_post
from session import get_json_valid
from session import get_json
@ -1543,7 +1544,7 @@ def _get_post_title_reply_html(base_dir: str,
session, reply_url,
headers, None, debug,
__version__, http_prefix, domain)
if reply_post_json:
if get_json_valid(reply_post_json):
if isinstance(reply_post_json, dict):
obj_json = reply_post_json
if has_object_dict(reply_post_json):

View File

@ -94,6 +94,7 @@ from blocking import get_account_blocks
from blocking import is_blocked
from content import bold_reading_string
from roles import is_devops
from session import get_json_valid
from session import site_is_verified
from session import get_json
@ -460,7 +461,7 @@ def html_profile_after_search(recent_posts_cache: {}, max_recent_posts: int,
if debug:
print('DEBUG: resolved public feed announce ' +
str(item))
if not item:
if not get_json_valid(item):
continue
show_item, post_json_object = \
_valid_profile_preview_post(item, person_url)

View File

@ -49,6 +49,7 @@ from webapp_utils import html_search_result_share
from webapp_post import individual_post_as_html
from webapp_hashtagswarm import html_hash_tag_swarm
from maps import html_hashtag_maps
from session import get_json_valid
from session import get_json
@ -1100,7 +1101,7 @@ def html_hashtag_search_remote(nickname: str, domain: str, port: int,
session, hashtag_url_with_page, as_header, None, debug,
__version__, http_prefix, domain)
lines = []
if hashtag_json:
if get_json_valid(hashtag_json):
if 'orderedItems' in hashtag_json:
lines = hashtag_json['orderedItems']
else:
@ -1171,7 +1172,7 @@ def html_hashtag_search_remote(nickname: str, domain: str, port: int,
get_json(signing_priv_key_pem,
session, post_id, as_header, None, debug,
__version__, http_prefix, domain)
if not post_json_object:
if not get_json_valid(post_json_object):
print('No hashtag post for ' + post_id)
continue
if not isinstance(post_json_object, dict):

View File

@ -11,6 +11,7 @@ import os
from shutil import copyfile
from collections import OrderedDict
from session import get_json
from session import get_json_valid
from utils import local_network_host
from utils import get_media_extensions
from utils import dangerous_markup
@ -420,7 +421,7 @@ def update_avatar_image_cache(signing_priv_key_pem: str,
get_json(signing_priv_key_pem, session, actor,
session_headers, None,
debug, __version__, http_prefix, None)
if person_json:
if get_json_valid(person_json):
if not person_json.get('id'):
return None
pub_key, _ = get_actor_public_key_from_id(person_json, None)

View File

@ -10,6 +10,7 @@ __module_group__ = "ActivityPub"
import os
import urllib.parse
from session import get_json
from session import get_json_valid
from cache import store_webfinger_in_cache
from cache import get_webfinger_from_cache
from utils import remove_html
@ -110,7 +111,7 @@ def webfinger_handle(session, handle: str, http_prefix: str,
# if the first attempt fails then try specifying the webfinger
# resource in a different way
if not result:
if not get_json_valid(result):
resource = handle
if handle == wf_handle:
# reconstruct the actor
@ -128,7 +129,7 @@ def webfinger_handle(session, handle: str, http_prefix: str,
print('ERROR: webfinger_handle ' + wf_handle + ' ' + str(ex))
return None
if result:
if get_json_valid(result):
store_webfinger_in_cache(wf_handle, result, cached_webfingers)
else:
print("WARN: Unable to webfinger " + str(url) + ' ' +