Paubox_Email_SDK.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 203 CC 12 SCAR 22.9
CC
12
Cov
0%
_returnforceSecureNotificationValue
20.0
getEmailDisposition
20.0
new
12.0
CRAP 9.9
1 package Paubox_Email_SDK;
2
3 use strict;
4 use warnings;
5
6 require Exporter;
7
8 our @ISA = qw(Exporter);
9
10 our @EXPORT_OK = qw(
11 getEmailDisposition
12 sendMessage
13 );
14
15 our $VERSION = '1.2';
16
17 use Paubox_Email_SDK::ApiHelper;
18 use Paubox_Email_SDK::Message;
19
20 use JSON;
21 use Config::General;
22 use TryCatch;
23 use String::Util qw(trim);
24 use MIME::Base64;
25
26 my $apiKey ="";
27 my $apiUser="";
28 my $baseURL = "https://api.paubox.net:443/v1/";
29
30 #
31 # Default Constructor
32 #
33 sub new{
34 my $this = {};
35 try{
36
37 my $conf = Config::General -> new(
38 -ConfigFile => 'config.cfg',
39 -InterPolateVars => 1
40 );
41
42 my %config = $conf -> getall;
43 if(not defined $config{'apiKey'} or
44 $config{'apiKey'} eq ""
45 ) {
46 die "apiKey is missing.";
47 }
48
49 if(
50 not defined $config{'apiUsername'} or
51 $config{'apiUsername'} eq ""
52 ) {
53 die "apiUsername is missing.";
54 }
55
56 $apiKey = $config{'apiKey'};
57 $apiUser = $config{'apiUsername'};
58
59 bless $this;
60
61 } catch($err) {
62 die "Error: " .$err;
63 };
64 return $this;
65 }
66
67 #
68 # Private methods
69 #
70
71 sub _getAuthHeader {
72 return "Token token=" .$apiKey;
73 }
74
75 sub _returnforceSecureNotificationValue {
76
77 my ($forceSecureNotification) = @_;
78 my $forceSecureNotificationValue = "";
79
80 if( !defined($forceSecureNotification) || $forceSecureNotification eq "" ) {
81 return "";
82 }
83 else {
84 $forceSecureNotificationValue = trim ( lc $forceSecureNotification );
85 if ($forceSecureNotificationValue eq "true") {
86 return 1;
87 } elsif ($forceSecureNotificationValue eq "false") {
88 return 0;
89 } else {
90 return "";
91 }
92 }
93 }
94
95 sub _convertMsgObjtoJSONReqObj {
96
97 my ($msg) = @_;
98
99 my %reqObject;
100 my $encodedHtmlContent = undef;
101 my $forceSecureNotification = $msg -> {'forceSecureNotification'};
102 my $forceSecureNotificationValue = _returnforceSecureNotificationValue($forceSecureNotification);
103
104 if ( defined($msg -> {'html_content'}) and $msg -> {'html_content'} ne "" ) {
105 $encodedHtmlContent = trim (encode_base64($msg -> {'html_content'}) );
106 }
107
108 if($forceSecureNotificationValue eq "" ) {
109
110 %reqObject = (
111 data => {
112 message => {
113 recipients => $msg -> {'to'},
114 cc => $msg -> {'cc'},
115 bcc => $msg -> {'bcc'},
116 headers => {
117 subject => $msg -> {'subject'},
118 from => $msg -> {'from'},
119 'reply-to' => $msg -> {'replyTo'}
120 },
121 allowNonTLS => $msg -> {'allowNonTLS'},
122 content => {
123 'text/plain' => $msg -> {'text_content'},
124 'text/html' => $encodedHtmlContent
125 },
126 attachments => $msg -> {'attachments'},
127 },
128
129 });
130 }
131 else {
132
133 %reqObject = (
134 data => {
135 message => {
136 recipients => $msg -> {'to'},
137 cc => $msg -> {'cc'},
138 bcc => $msg -> {'bcc'},
139 headers => {
140 subject => $msg -> {'subject'},
141 from => $msg -> {'from'},
142 'reply-to' => $msg -> {'replyTo'}
143 },
144 allowNonTLS => $msg -> {'allowNonTLS'},
145 forceSecureNotification => $forceSecureNotificationValue,
146 content => {
147 'text/plain' => $msg -> {'text_content'},
148 'text/html' => $encodedHtmlContent
149 },
150 attachments => $msg -> {'attachments'},
151 },
152 });
153 }
154
155 return encode_json (\%reqObject);
156 }
157
158 #
159 # Public methods
160 #
161
162
163 #
164 # Get Email Disposition
165 #
166
167 sub getEmailDisposition {
168 my ($class,$sourceTrackingId) = @_;
169 my $apiResponseJSON = "";
170 try{
171 my $authHeader = _getAuthHeader() ;
172 my $apiUrl = "/message_receipt?sourceTrackingId=" . $sourceTrackingId;
173 my $apiHelper = Paubox_Email_SDK::ApiHelper -> new();
174 $apiResponseJSON = $apiHelper -> callToAPIByGet($baseURL.$apiUser, $apiUrl, $authHeader);
175
176 # Converting JSON api response to perl
177 my $apiResponsePERL = from_json($apiResponseJSON);
178
179 if (
180 !length $apiResponsePERL -> {'data'}
181 && !length $apiResponsePERL -> {'sourceTrackingId'}
182 && !length $apiResponsePERL -> {'errors'}
183 )
184 {
185 die $apiResponseJSON;
186 }
187
188 if (
189 defined $apiResponsePERL && defined $apiResponsePERL -> {'data'} && defined $apiResponsePERL -> {'data'} -> {'message'}
190 && defined $apiResponsePERL -> {'data'} -> {'message'} -> {'message_deliveries'}
191 && (scalar( @{ $apiResponsePERL -> {'data'} -> {'message'} -> {'message_deliveries'} } ) > 0 )
192 ) {
193 foreach my $message_deliveries ( @{ $apiResponsePERL -> {'data'} -> {'message'} -> {'message_deliveries'} } ) {
194
195 if( $message_deliveries -> {'status'} -> {'openedStatus'} eq "" ) {
196
197 $message_deliveries -> {'status'} -> {'openedStatus'} = "unopened";
198 # Converting perl api response back to JSON
199 $apiResponseJSON = to_json($apiResponsePERL);
200 }
201 }
202 }
203 } catch($err) {
204 die $err;
205 };
206
207
208 return $apiResponseJSON;
209 }
210
211 #
212 # Send Email Message
213 #
214
215 sub sendMessage {
216 my ($class,$msgObj) = @_;
217 my $apiResponseJSON = "";
218 try{
219
220 my $apiUrl = "/messages";
221 my $reqBody = _convertMsgObjtoJSONReqObj($msgObj);
222 my $apiHelper = Paubox_Email_SDK::ApiHelper -> new();
223 $apiResponseJSON = $apiHelper -> callToAPIByPost($baseURL.$apiUser, $apiUrl, _getAuthHeader() , $reqBody);
224 # Converting JSON api response to perl
225 my $apiResponsePERL = from_json($apiResponseJSON);
226
227 if (
228 !length $apiResponsePERL -> {'data'}
229 && !length $apiResponsePERL -> {'sourceTrackingId'}
230 && !length $apiResponsePERL -> {'errors'}
231 )
232 {
233 die $apiResponseJSON;
234 }
235
236 } catch($err) {
237 die $err;
238 };
239
240 return $apiResponseJSON;
241 }
242
243 1;
244 __END__