The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/env perl

# use 5.010;

use strict;
use warnings;

use Getopt::Long 2.33 qw{
    :config auto_version pass_through require_order
};
use Pod::Usage;

use blib;
use lib 'inc';

use Astro::SpaceTrack::Dumper;
use File::Glob qw{ bsd_glob };
use JSON;
use Mock::LWP::UserAgent ();

our $VERSION = '0.094';

my %opt = (
    directory		=> $Mock::LWP::UserAgent::CANNED_RESPONSE_DIR,
    retrieval_size	=> $Astro::SpaceTrack::RETRIEVAL_SIZE,
);

GetOptions( \%opt,
    qw{ directory=s list! retrieval_size|retrieval-size=i verbose! },
    help => sub { pod2usage( { -verbose => 2 } ) },
) or pod2usage( { -verbose => 0 } );

$Mock::LWP::UserAgent::CANNED_RESPONSE_DIR = $opt{directory};

$opt{list}
    and list_captures();

{
    local $Astro::SpaceTrack::RETRIEVAL_SIZE = $opt{retrieval_size};
    my $st = Astro::SpaceTrack::Dumper->new(
	prompt => 'SpaceTrack capture> ',
    );
    $opt{verbose}
	and $st->set( dump_headers => Astro::SpaceTrack->DUMP_RESPONSE() );
    $st->shell( @ARGV );
}

sub list_captures {
    my $json = JSON->new()->utf8();
    foreach my $path ( bsd_glob( File::Spec->catfile(
		$Mock::LWP::UserAgent::CANNED_RESPONSE_DIR, '*.json' ) )
    ) {
	local $/ = undef;
	open my $fh, '<:encoding(utf-8)', $path
	    or die "Failed to open $path: $!\n";
##	my ( $code, $message, $headers, $content, $request ) =
	my ( undef, undef, undef, $content, $request ) =
	    @{ $json->decode( <$fh> ) };
	close $fh;
	say $path;
	( my $buffer = $content ) =~ s/ ( \A | [\r\n]+ ) /$1    /smxg;
	$buffer =~ s/ \s+ \z //smx;
	say $buffer;
	if ( $opt{verbose} ) {
	    print <<"EOD";
    $request->{method}
    $request->{uri}
EOD
	}
    }
    exit;
}

__END__

=head1 TITLE

capture - Capture HTTP::Response objects to feed to a mock LWP::UserAgent.

=head1 SYNOPSIS

 capture
 capture -help
 capture -version

=head1 OPTIONS

The following options are supported directly by this script. Any
unrecognized options will be passed through to C<Astro::SpaceTrack>.

=head2 -directory

This option specifies the directory in which the responses are to be
written. The default is the default for C<Mock::LWP::UserAgent>.

=head2 -help

This option displays the documentation for this script. The script then
exits.

=head2 -retrieval-size

This option specifies the maximum number of OIDs to retrieve in a single
query. The default is C<200>.

=head2 -verbose

If this option is asserted, file creation will be logged to standard
error.

=head2 -version

This option displays the version of this script. The script then exits.

=head1 DETAILS

This Perl script is unsupported and may be modified or retracted without
notice at any time. The documentation is simply for the benefit of the
author.

This Perl script is essentially a wrapper for the Astro::SpaceTrack
C<shell()> method. But it uses a subclass of
L<Astro::SpaceTrack|Astro::SpaceTrack> to dump HTTP responses to JSON
files whose name is an MD5 digest of the URL that generated the
response. The files are censored heavily by this subclass.

=head1 AUTHOR

Thomas R. Wyant, III F<wyant at cpan dot org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2014-2016 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.

=cut

# ex: set textwidth=72 :