| 1 | package Google::RestApi::GmailApi1; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | our $VERSION = '2.2.5'; | |||||||||||
| 4 | |||||||||||||
| 5 | 2 | ▶ | use Google::RestApi::Setup; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | ▶ | use Encode qw(encode); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use MIME::Lite; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use Readonly; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use URI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 2 | ▶ | use aliased 'Google::RestApi::GmailApi1::Message'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 2 | ▶ | use aliased 'Google::RestApi::GmailApi1::Thread'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 2 | ▶ | use aliased 'Google::RestApi::GmailApi1::Draft'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 2 | ▶ | use aliased 'Google::RestApi::GmailApi1::Label'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 2 | Readonly our $Gmail_Endpoint => 'https://gmail.googleapis.com/gmail/v1/users'; | |||||||||||
| 19 | |||||||||||||
| 20 | 31 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 21 | 31 | my $class = shift; | |||||||||||
| 22 | 31 | state $check = signature( | |||||||||||
| 23 | bless => !!0, | ||||||||||||
| 24 | named => [ | ||||||||||||
| 25 | api => HasApi, | ||||||||||||
| 26 | user_id => Str, { default => 'me' }, | ||||||||||||
| 27 | endpoint => Str, { default => $Gmail_Endpoint }, | ||||||||||||
| 28 | ], | ||||||||||||
| 29 | ); | ||||||||||||
| 30 | 31 | return bless $check->(@_), $class; | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | 29 | ▶ | sub api { | ||||||||||
|
Subroutine
api: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 34 | 29 | my $self = shift; | |||||||||||
| 35 | 29 | state $check = signature( | |||||||||||
| 36 | bless => !!0, | ||||||||||||
| 37 | named => [ | ||||||||||||
| 38 | uri => Str, { optional => 1 }, | ||||||||||||
| 39 | _extra_ => slurpy HashRef, | ||||||||||||
| 40 | ], | ||||||||||||
| 41 | ); | ||||||||||||
| 42 | 29 | my $p = named_extra($check->(@_)); | |||||||||||
| 43 | 29 | my $uri = "$self->{endpoint}/$self->{user_id}/"; | |||||||||||
| 44 | 29 | ▶ | $uri .= delete $p->{uri} if defined $p->{uri}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 29 | return $self->{api}->api(%$p, uri => $uri); | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 5 | ▶ | sub message { | ||||||||||
|
Subroutine
message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 49 | 5 | my $self = shift; | |||||||||||
| 50 | 5 | state $check = signature( | |||||||||||
| 51 | bless => !!0, | ||||||||||||
| 52 | named => [ | ||||||||||||
| 53 | id => Str, { optional => 1 }, | ||||||||||||
| 54 | ], | ||||||||||||
| 55 | ); | ||||||||||||
| 56 | 5 | my $p = $check->(@_); | |||||||||||
| 57 | 5 | return Message->new(gmail_api => $self, %$p); | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 3 | ▶ | sub thread { | ||||||||||
|
Subroutine
thread: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 61 | 3 | my $self = shift; | |||||||||||
| 62 | 3 | state $check = signature( | |||||||||||
| 63 | bless => !!0, | ||||||||||||
| 64 | named => [ | ||||||||||||
| 65 | id => Str, { optional => 1 }, | ||||||||||||
| 66 | ], | ||||||||||||
| 67 | ); | ||||||||||||
| 68 | 3 | my $p = $check->(@_); | |||||||||||
| 69 | 3 | return Thread->new(gmail_api => $self, %$p); | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | 2 | ▶ | sub draft { | ||||||||||
|
Subroutine
draft: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 73 | 2 | my $self = shift; | |||||||||||
| 74 | 2 | state $check = signature( | |||||||||||
| 75 | bless => !!0, | ||||||||||||
| 76 | named => [ | ||||||||||||
| 77 | id => Str, { optional => 1 }, | ||||||||||||
| 78 | ], | ||||||||||||
| 79 | ); | ||||||||||||
| 80 | 2 | my $p = $check->(@_); | |||||||||||
| 81 | 2 | return Draft->new(gmail_api => $self, %$p); | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 2 | ▶ | sub label { | ||||||||||
|
Subroutine
label: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 85 | 2 | my $self = shift; | |||||||||||
| 86 | 2 | state $check = signature( | |||||||||||
| 87 | bless => !!0, | ||||||||||||
| 88 | named => [ | ||||||||||||
| 89 | id => Str, { optional => 1 }, | ||||||||||||
| 90 | ], | ||||||||||||
| 91 | ); | ||||||||||||
| 92 | 2 | my $p = $check->(@_); | |||||||||||
| 93 | 2 | return Label->new(gmail_api => $self, %$p); | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 4 | ▶ | sub profile { | ||||||||||
|
Subroutine
profile: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 97 | 4 | my $self = shift; | |||||||||||
| 98 | 4 | return $self->api(uri => 'profile'); | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 2 | ▶ | sub messages { | ||||||||||
|
Subroutine
messages: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 102 | 2 | my $self = shift; | |||||||||||
| 103 | 2 | state $check = signature( | |||||||||||
| 104 | bless => !!0, | ||||||||||||
| 105 | named => [ | ||||||||||||
| 106 | max_pages => Int, { default => 1 }, | ||||||||||||
| 107 | page_callback => CodeRef, { optional => 1 }, | ||||||||||||
| 108 | params => HashRef, { default => {} }, | ||||||||||||
| 109 | ], | ||||||||||||
| 110 | ); | ||||||||||||
| 111 | 2 | my $p = $check->(@_); | |||||||||||
| 112 | |||||||||||||
| 113 | return paginated_list( | ||||||||||||
| 114 | api => $self, | ||||||||||||
| 115 | uri => 'messages', | ||||||||||||
| 116 | result_key => 'messages', | ||||||||||||
| 117 | default_fields => 'messages(id, threadId)', | ||||||||||||
| 118 | fields_prefix => 'nextPageToken, resultSizeEstimate', | ||||||||||||
| 119 | max_pages => $p->{max_pages}, | ||||||||||||
| 120 | params => $p->{params}, | ||||||||||||
| 121 | 2 | ▶ | ($p->{page_callback} ? (page_callback => $p->{page_callback}) : ()), | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | ); | ||||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | 1 | ▶ | sub threads { | ||||||||||
|
Subroutine
threads: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 126 | 1 | my $self = shift; | |||||||||||
| 127 | 1 | state $check = signature( | |||||||||||
| 128 | bless => !!0, | ||||||||||||
| 129 | named => [ | ||||||||||||
| 130 | max_pages => Int, { default => 1 }, | ||||||||||||
| 131 | page_callback => CodeRef, { optional => 1 }, | ||||||||||||
| 132 | params => HashRef, { default => {} }, | ||||||||||||
| 133 | ], | ||||||||||||
| 134 | ); | ||||||||||||
| 135 | 1 | my $p = $check->(@_); | |||||||||||
| 136 | |||||||||||||
| 137 | return paginated_list( | ||||||||||||
| 138 | api => $self, | ||||||||||||
| 139 | uri => 'threads', | ||||||||||||
| 140 | result_key => 'threads', | ||||||||||||
| 141 | default_fields => 'threads(id, snippet)', | ||||||||||||
| 142 | fields_prefix => 'nextPageToken, resultSizeEstimate', | ||||||||||||
| 143 | max_pages => $p->{max_pages}, | ||||||||||||
| 144 | params => $p->{params}, | ||||||||||||
| 145 | 1 | ▶ | ($p->{page_callback} ? (page_callback => $p->{page_callback}) : ()), | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 146 | ); | ||||||||||||
| 147 | } | ||||||||||||
| 148 | |||||||||||||
| 149 | 1 | ▶ | sub labels { | ||||||||||
|
Subroutine
labels: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 150 | 1 | my $self = shift; | |||||||||||
| 151 | 1 | my $result = $self->api(uri => 'labels'); | |||||||||||
| 152 | 1 | ▶ | return $result->{labels} ? $result->{labels}->@* : (); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | 3 | ▶ | sub send_message { | ||||||||||
|
Subroutine
send_message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 156 | 3 | my $self = shift; | |||||||||||
| 157 | 3 | state $check = signature( | |||||||||||
| 158 | bless => !!0, | ||||||||||||
| 159 | named => [ | ||||||||||||
| 160 | to => Str, | ||||||||||||
| 161 | subject => Str, | ||||||||||||
| 162 | body => Str, | ||||||||||||
| 163 | from => Str, { optional => 1 }, | ||||||||||||
| 164 | cc => Str, { optional => 1 }, | ||||||||||||
| 165 | bcc => Str, { optional => 1 }, | ||||||||||||
| 166 | content_type => Str, { default => 'text/plain' }, | ||||||||||||
| 167 | ], | ||||||||||||
| 168 | ); | ||||||||||||
| 169 | 3 | my $p = $check->(@_); | |||||||||||
| 170 | |||||||||||||
| 171 | 3 | my $raw = $self->_build_mime(%$p); | |||||||||||
| 172 | |||||||||||||
| 173 | 3 | DEBUG("Sending message to '$p->{to}'"); | |||||||||||
| 174 | 3 | my $result = $self->api( | |||||||||||
| 175 | uri => 'messages/send', | ||||||||||||
| 176 | method => 'post', | ||||||||||||
| 177 | content => { raw => $raw }, | ||||||||||||
| 178 | ); | ||||||||||||
| 179 | 3 | return Message->new(gmail_api => $self, id => $result->{id}); | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | 0 | ▶ | sub send_raw_message { | ||||||||||
|
Subroutine
send_raw_message: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 183 | 0 | my $self = shift; | |||||||||||
| 184 | 0 | state $check = signature( | |||||||||||
| 185 | bless => !!0, | ||||||||||||
| 186 | named => [ | ||||||||||||
| 187 | raw => Str, | ||||||||||||
| 188 | ], | ||||||||||||
| 189 | ); | ||||||||||||
| 190 | 0 | my $p = $check->(@_); | |||||||||||
| 191 | |||||||||||||
| 192 | 0 | DEBUG("Sending raw message"); | |||||||||||
| 193 | my $result = $self->api( | ||||||||||||
| 194 | uri => 'messages/send', | ||||||||||||
| 195 | method => 'post', | ||||||||||||
| 196 | content => { raw => $p->{raw} }, | ||||||||||||
| 197 | 0 | ); | |||||||||||
| 198 | 0 | return Message->new(gmail_api => $self, id => $result->{id}); | |||||||||||
| 199 | } | ||||||||||||
| 200 | |||||||||||||
| 201 | 0 | ▶ | sub batch_modify_messages { | ||||||||||
|
Subroutine
batch_modify_messages: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 202 | 0 | my $self = shift; | |||||||||||
| 203 | 0 | state $check = signature( | |||||||||||
| 204 | bless => !!0, | ||||||||||||
| 205 | named => [ | ||||||||||||
| 206 | ids => ArrayRef[Str], | ||||||||||||
| 207 | add_label_ids => ArrayRef[Str], { default => [] }, | ||||||||||||
| 208 | remove_label_ids => ArrayRef[Str], { default => [] }, | ||||||||||||
| 209 | ], | ||||||||||||
| 210 | ); | ||||||||||||
| 211 | 0 | my $p = $check->(@_); | |||||||||||
| 212 | |||||||||||||
| 213 | my %content = ( | ||||||||||||
| 214 | ids => $p->{ids}, | ||||||||||||
| 215 | 0 | ); | |||||||||||
| 216 | 0 | ▶ | $content{addLabelIds} = $p->{add_label_ids} if $p->{add_label_ids}->@*; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 217 | 0 | ▶ | $content{removeLabelIds} = $p->{remove_label_ids} if $p->{remove_label_ids}->@*; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 218 | |||||||||||||
| 219 | 0 | DEBUG(sprintf("Batch modifying %d messages", scalar $p->{ids}->@*)); | |||||||||||
| 220 | 0 | return $self->api( | |||||||||||
| 221 | uri => 'messages/batchModify', | ||||||||||||
| 222 | method => 'post', | ||||||||||||
| 223 | content => \%content, | ||||||||||||
| 224 | ); | ||||||||||||
| 225 | } | ||||||||||||
| 226 | |||||||||||||
| 227 | 0 | ▶ | sub batch_delete_messages { | ||||||||||
|
Subroutine
batch_delete_messages: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 228 | 0 | my $self = shift; | |||||||||||
| 229 | 0 | state $check = signature( | |||||||||||
| 230 | bless => !!0, | ||||||||||||
| 231 | named => [ | ||||||||||||
| 232 | ids => ArrayRef[Str], | ||||||||||||
| 233 | ], | ||||||||||||
| 234 | ); | ||||||||||||
| 235 | 0 | my $p = $check->(@_); | |||||||||||
| 236 | |||||||||||||
| 237 | 0 | DEBUG(sprintf("Batch deleting %d messages", scalar $p->{ids}->@*)); | |||||||||||
| 238 | return $self->api( | ||||||||||||
| 239 | uri => 'messages/batchDelete', | ||||||||||||
| 240 | method => 'post', | ||||||||||||
| 241 | content => { ids => $p->{ids} }, | ||||||||||||
| 242 | 0 | ); | |||||||||||
| 243 | } | ||||||||||||
| 244 | |||||||||||||
| 245 | 4 | ▶ | sub _build_mime { | ||||||||||
|
Subroutine
_build_mime: called CC=4 SCAR=14.1
| |||||||||||||
| 246 | 4 | my $self = shift; | |||||||||||
| 247 | 4 | my %p = @_; | |||||||||||
| 248 | |||||||||||||
| 249 | my $mime = MIME::Lite->new( | ||||||||||||
| 250 | From => $p{from} // '', | ||||||||||||
| 251 | To => $p{to}, | ||||||||||||
| 252 | Subject => $p{subject}, | ||||||||||||
| 253 | Type => $p{content_type}, | ||||||||||||
| 254 | 4 | ▶ | Data => encode('UTF-8', $p{body}), | ||||||||||
|
Condition50%1/2
$p{"from"} // ""
MC/DC0%0/1
$p{"from"} // ""
$p{"from"}
Truth table50%1/2
A
A= $p{"from"}
| |||||||||||||
| 255 | ); | ||||||||||||
| 256 | 4 | $mime->attr('content-type.charset' => 'UTF-8'); | |||||||||||
| 257 | 4 | ▶ | $mime->add(Cc => $p{cc}) if $p{cc}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 258 | 4 | ▶ | $mime->add(Bcc => $p{bcc}) if $p{bcc}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 259 | |||||||||||||
| 260 | 4 | my $raw = $mime->as_string; | |||||||||||
| 261 | 4 | my $encoded = encode_base64($raw, ''); | |||||||||||
| 262 | 4 | $encoded =~ tr{+/}{-_}; | |||||||||||
| 263 | 4 | $encoded =~ s/=+$//; | |||||||||||
| 264 | |||||||||||||
| 265 | 4 | return $encoded; | |||||||||||
| 266 | } | ||||||||||||
| 267 | |||||||||||||
| 268 | 0 | ▶ | sub rest_api { shift->{api}; } | ||||||||||
|
Subroutine
rest_api: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 269 | 0 | ▶ | sub transaction { shift->rest_api()->transaction(); } | ||||||||||
|
Subroutine
transaction: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 270 | 0 | ▶ | sub stats { shift->rest_api()->stats(); } | ||||||||||
|
Subroutine
stats: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 271 | 0 | ▶ | sub reset_stats { shift->rest_api->reset_stats(); } | ||||||||||
|
Subroutine
reset_stats: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 272 | |||||||||||||
| 273 | 2 | 1; | |||||||||||
| 274 | |||||||||||||
| 275 | __END__ | ||||||||||||