Created
April 6, 2016 19:55
-
-
Save phil21/a09ce3e7b1061383999d66476eca2e86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $ua = Mojo::UserAgent->new(); | |
# Add completely non-standard Authorization: HTTP header to each request | |
$ua->on(start => sub { | |
my ($ua, $tx) = @_; | |
$tx->req->headers->header('Authorization' => $auth_header); | |
# Print (naive) curl output if desired | |
if ($curl_debug) { | |
my $h = $tx->req->headers->to_hash; | |
my @curl = ('curl'); | |
for my $k (keys %$h) { | |
# Skip printing any superflous headers | |
next if $k =~ /User-Agent/i; | |
next if $k =~ /Accept-Encoding/i; | |
push @curl, "-H" => qq("$k: $h->{$k}"); | |
} | |
if (my $body = $tx->req->body) { | |
$body =~ s!\n!\\n!g; | |
push @curl, "-d" => qq("$body"); | |
} | |
say join ' ', @curl, $tx->req->url->to_abs; | |
} | |
}); | |
sub Query { | |
my %json = @_; | |
my $tx = $ua->post($url => json => \%json); | |
if ($tx->res->json->{error}) { | |
# Ran into some error on the remote side | |
print "API returned error:\n"; | |
print $tx->res->json->{error}{message}, "\n"; | |
exit 0; | |
} | |
return($tx); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment