aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-05-21 12:05:00 +0100
committerGitHub <noreply@github.com>2019-05-21 12:05:00 +0100
commit0acee1bdcc9391ba808b9ed81abbc5cf4a8b11b6 (patch)
treecd3139950059f9204ae9d8e617a56b6d1e546f96
parent1d7ab76498eebadbb554444f39aaf9fa807519b9 (diff)
parente0b5145fa6a2ea4b10d98817a6d739d8f2860b25 (diff)
downloadperlweeklychallenge-club-0acee1bdcc9391ba808b9ed81abbc5cf4a8b11b6.tar.gz
perlweeklychallenge-club-0acee1bdcc9391ba808b9ed81abbc5cf4a8b11b6.tar.bz2
perlweeklychallenge-club-0acee1bdcc9391ba808b9ed81abbc5cf4a8b11b6.zip
Merge pull request #165 from jmaslak/joelle-9-1-1
Add Sharkpost API in Perl 6
-rwxr-xr-xchallenge-009/joelle-maslak/perl6/ch-3.p685
-rw-r--r--challenge-009/joelle-maslak/perl6/ch-3.readme25
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-009/joelle-maslak/perl6/ch-3.p6 b/challenge-009/joelle-maslak/perl6/ch-3.p6
new file mode 100755
index 0000000000..25713d9c0c
--- /dev/null
+++ b/challenge-009/joelle-maslak/perl6/ch-3.p6
@@ -0,0 +1,85 @@
+#!/usr/bin/env perl6
+use v6;
+
+#
+# Copyright © 2019 Joelle Maslak
+# All Rights Reserved - See License
+#
+
+use Cro::HTTP::Client;
+
+sub MAIN(
+ Str:D :$from,
+ Str:D :$to,
+ Str:D :$subject,
+ Str:D :$body-text,
+ Bool :$sandbox,
+ Str:D :$config-file? = $*HOME.add(".sparkpost").Str,
+) {
+ my $api-key = get-api-key($config-file);
+
+ send-email(:$api-key, :$from, :to($to.split(';')), :$subject, :$body-text, :$sandbox);
+}
+
+sub get-api-key(Str:D $config-file -->Str:D) {
+ my @lines = $config-file.IO.lines().grep( *.chars > 0 );
+ die "Config-file ($config-file) must consist of one line" if @lines.elems ≠ 1;
+
+ return @lines[0];
+}
+
+# Usage:
+# $api-key = Your Mailgun API key
+# $from = Source email address
+# @to = Array of destination email addresses
+# $subject = Subject of email
+# $body-text = Text of email
+# $sandbox = Boolean indicating if we're using the "sandbox"
+#
+sub send-email(
+ Str:D :$api-key,
+ Str:D :$from,
+ :@to,
+ Str:D :$subject,
+ Str:D :$body-text,
+ Bool :$sandbox,
+) {
+ my $url = "https://api.sparkpost.com/api/v1/";
+
+ my $client = Cro::HTTP::Client.new(
+ base-uri => $url,
+ content-type => 'application/json',
+ headers => [ Authorization => $api-key ],
+ http => 1.1,
+ );
+
+ my @recipients;
+ for @to.unique -> $addr {
+ @recipients.push: %{ address => $addr }
+ }
+
+ my %json =
+ options => %{ sandbox => True },
+ content => %{
+ from => $from,
+ subject => $subject,
+ text => $body-text
+ },
+ recipients => @recipients,
+ ;
+
+ my $resp = await $client.post('transmissions', body => %json);
+ if $resp.status == 200 { return }
+
+ CATCH {
+ when X::Cro::HTTP::Error {
+ my $body = await .response.body;
+ if $body<errors>[0]<description>:exists {
+ die "Error from API endpoint: $body<errors>[0]<description>";
+ } else {
+ die "Error from API endpoint: $body<errors>[0]<message>";
+ }
+ }
+ }
+}
+
diff --git a/challenge-009/joelle-maslak/perl6/ch-3.readme b/challenge-009/joelle-maslak/perl6/ch-3.readme
new file mode 100644
index 0000000000..6f6db38c43
--- /dev/null
+++ b/challenge-009/joelle-maslak/perl6/ch-3.readme
@@ -0,0 +1,25 @@
+This assumes there is a config file ~/.sparkpost which contains ONE line:
+ api-key (the actual value of the API key, not the string "api-key")
+
+MODULES REQUIRED:
+ Cro::HTTP::Client
+
+To specify multiple email addresses using command line version of script, use
+";" between addresses.
+
+See the source code for how to use the send-email() API implementation.
+
+For command line that uses that API implementation, pass the following:
+
+ --from=<from email address>
+ --subject="<subject of email>"
+ --body="<body of email>"
+ --sandbox (used to enable sandbox mode)
+
+Example:
+
+perl6 ch-3.p6 --from=sandbox@sparkpostbox.com --to=jmaslak@antelope.net \
+ --sandbox --subject=Test --body-text="Test Message"
+
+No output means things went well. :)
+