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

19 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-31 16:00 +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__": 

44 from rfpy.mail.schemas import PostmarkSimpleMessage 

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

46 headers = api_headers('POSTMARK_API_TEST') 

47 headers = api_headers(conf.CONF.postmark_api_key) 

48 

49 score_comment_model = { 

50 "system_name": "PostRFP", 

51 "host_url_root": "localhost", 

52 "recipient": { 

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

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

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

56 }, 

57 "event": { 

58 "id": 1667, 

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

60 "event_type": "SCORE_COMMENT_ADDED", 

61 "user": { 

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

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

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

65 }, 

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

67 "project": { 

68 "id": 1, 

69 "title": "Choose Cheese", 

70 "status": "Live", 

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

72 "author": { 

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

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

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

76 }, 

77 }, 

78 "issue": { 

79 "id": 2, 

80 "status": "Submitted", 

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

82 }, 

83 }, 

84 "score": { 

85 "comment": "Comment by Jake", 

86 "respondent": "Chevres", 

87 "question_number": "1.1", 

88 "value": 3.0, 

89 }, 

90 } 

91 

92 model = PostmarkSimpleMessage( 

93 To="[email protected]", 

94 From="[email protected]", 

95 Subject="Test blackhole", 

96 TextBody="Just a test", 

97 Tag='Testing' 

98 ).dict() 

99 resp = send_simple_email(model, headers) 

100 print(resp)