@@ -773,12 +773,10 @@ def get_postmark_token() -> str:
773
773
774
774
return ''
775
775
776
- token = get_postmark_token ()
777
-
778
- collections = (RecipientCollection , EntryRecipientCollection )
779
- for collection in collections :
780
- recipients = collection (request .session )
776
+ def get_bounces () -> list [dict [str , Any ]]:
777
+ token = get_postmark_token ()
781
778
yesterday = utcnow () - timedelta (days = 1 )
779
+ r = None
782
780
783
781
try :
784
782
r = requests .get (
@@ -787,21 +785,28 @@ def get_postmark_token() -> str:
787
785
f'{ yesterday .date ()} &inactive=true' ,
788
786
headers = {
789
787
'Accept' : 'application/json' ,
790
- 'X-Postmark-Server-Token' : token
791
- },
792
- timeout = 30
788
+ 'X-Postmark-Server-Token' : token ,
789
+ },
790
+ timeout = 30 ,
793
791
)
794
792
r .raise_for_status ()
795
793
bounces = r .json ().get ('Bounces' , [])
796
794
except requests .exceptions .HTTPError as http_err :
797
- if r .status_code == 401 :
795
+ if r and r .status_code == 401 :
798
796
raise RuntimeWarning (
799
797
f'Postmark API token is not set or invalid: { http_err } '
800
798
) from None
801
799
else :
802
800
raise
803
801
804
- for bounce in bounces :
802
+ return bounces
803
+
804
+ postmark_bounces = get_bounces ()
805
+ collections = (RecipientCollection , EntryRecipientCollection )
806
+ for collection in collections :
807
+ recipients = collection (request .session )
808
+
809
+ for bounce in postmark_bounces :
805
810
email = bounce .get ('Email' , '' )
806
811
inactive = bounce .get ('Inactive' , False )
807
812
recipient = recipients .by_address (email )
0 commit comments