Skip to content
Browse files

Log all calls made, give --raw option to get_json

  • Loading branch information...
1 parent 0ed5c2b commit 766c070bde784a5a48f2367e2a7b1b700999aec1 T. Alexander Popiel committed Dec 28, 2010
Showing with 42 additions and 1 deletion.
  1. +32 −0 Client.pm
  2. +10 −1 get_json
View
32 Client.pm
@@ -80,6 +80,37 @@ sub format_time {
sprintf("%4d-%02d-%02d %02d:%02d:%02d", @elems);
}
+sub log_call {
+ my $api = shift;
+ my $message = shift;
+ my $response = shift;
+ our $time;
+ our $count;
+
+ my $dir = "log";
+ -d $dir or mkpath($dir) or croak "Could not make path $dir: $!";
+ my $now = time();
+ if ($time ne $now) {
+ $time = $now;
+ $count = 0;
+ } else {
+ $count++;
+ }
+
+ my $filename = join(".", format_time($time), sprintf("%03d", $count), $api, $message->{method});
+ $filename =~ s-/--g;
+ $filename =~ s- -_-g;
+ my $file;
+ open($file, ">", "log/$filename") or croak "Could not log call: $!";
+ print $file encode_json({
+ api => $api,
+ message => $message,
+ status => $response->status_line,
+ response => $response->content,
+ });
+ close($file);
+}
+
sub call {
my $self = shift;
my $api = shift;
@@ -93,6 +124,7 @@ sub call {
# warn "Posting to ".($self->{uri} . $api)."\n";
# warn "Content: ".encode_json($message)."\n";
my $response = $self->{ua}->post($self->{uri} . $api, Content => encode_json($message));
+ log_call($api, $message, $response);
my $result = decode_json($response->content);
croak join(": ", $result->{error}{code}, $result->{error}{message},
JSON::XS->new->allow_nonref->canonical->pretty->encode($result->{error}{data}))
View
11 get_json
@@ -5,6 +5,11 @@ use strict;
use JSON::XS;
my $path = shift(@ARGV);
+my $raw = 0;
+if ($path eq "--raw") {
+ $raw = 1;
+ $path = shift(@ARGV);
+}
my @lines = (<>);
@@ -18,4 +23,8 @@ if ($path) {
$hash = $hash + 0 if !ref($hash) && $hash eq ($hash + 0);
-print JSON::XS->new->allow_nonref->canonical->pretty->encode($hash);
+if ($raw && !ref($hash)) {
+ print $hash;
+} else {
+ print JSON::XS->new->allow_nonref->canonical->pretty->encode($hash);
+}

0 comments on commit 766c070

Please sign in to comment.
Something went wrong with that request. Please try again.