Coverage for rfpy/mail/postmark.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-24 10:52 +0000

1import logging 

2 

3import requests 

4 

5from rfpy import conf 

6from rfpy.utils import configure_rfpy 

7 

8 

9log = logging.getLogger(__name__) 

10 

11 

12def api_headers(key): 

13 return { 

14 "X-Postmark-Server-Token": key, 

15 "Accept": "application/json", 

16 "Content-Type": "application/json", 

17 } 

18 

19 

20def _handle_response(to_addr, response): 

21 if not response.ok: 

22 log.warning("Mail delivery to %s failed: %s", to_addr, response.text) 

23 response.raise_for_status() 

24 return response.json()["MessageID"] 

25 

26 

27def send_simple_email(model, headers): 

28 # model['To'] = conf.CONF.email_to_override 

29 resp = requests.post( 

30 "https://api.postmarkapp.com/email", json=model, headers=headers 

31 ) 

32 return _handle_response(model["To"], resp) 

33 

34 

35def send_template_email(model, headers): 

36 model["To"] = conf.CONF.email_to_override 

37 resp = requests.post( 

38 "https://api.postmarkapp.com/email/withTemplate", json=model, headers=headers 

39 ) 

40 return _handle_response(model["To"], resp) 

41 

42 

43if __name__ == "__main__": # pragma: no cover 

44 from rfpy.mail.schemas import PostmarkSimpleMessage 

45 

46 configure_rfpy(env_file="/etc/postrfp/rfpy-settings.env") 

47 headers = api_headers("POSTMARK_API_TEST") 

48 headers = api_headers(conf.CONF.postmark_api_key) 

49 

50 score_comment_model = { 

51 "system_name": "PostRFP", 

52 "host_url_root": "localhost", 

53 "recipient": { 

54 "fullname": "[email protected]", 

55 "email": "[email protected]", 

56 "organisation": {"name": "Tarty Buyer"}, 

57 }, 

58 "event": { 

59 "id": 1667, 

60 "timestamp": "2021-03-16T00:37:47", 

61 "event_type": "SCORE_COMMENT_ADDED", 

62 "user": { 

63 "fullname": "[email protected]", 

64 "email": "[email protected]", 

65 "organisation": {"name": "Genius Consulting"}, 

66 }, 

67 "organisation": {"name": "Genius Consulting"}, 

68 "project": { 

69 "id": 1, 

70 "title": "Choose Cheese", 

71 "status": "Live", 

72 "owner_organisation": {"name": "Tarty Buyer"}, 

73 "author": { 

74 "fullname": "[email protected]", 

75 "email": "[email protected]", 

76 "organisation": {"name": "Genius Consulting"}, 

77 }, 

78 }, 

79 "issue": { 

80 "id": 2, 

81 "status": "Submitted", 

82 "respondent": {"name": "Chevres"}, 

83 }, 

84 }, 

85 "score": { 

86 "comment": "Comment by Jake", 

87 "respondent": "Chevres", 

88 "question_number": "1.1", 

89 "value": 3.0, 

90 }, 

91 } 

92 

93 model = PostmarkSimpleMessage( 

94 To="[email protected]", 

95 From="[email protected]", 

96 Subject="Test blackhole", 

97 TextBody="Just a test", 

98 Tag="Testing", 

99 ).model_dump() 

100 resp = send_simple_email(model, headers) 

101 print(resp)