App-Presto

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    do things like auto-append a file extension to all URLs. For instance:

            bash$ presto http://my-server.com*.json
            http://my-server.com> GET /product/1

    In this case, the full URL would be
    http://my-server.com/product/1.json. If no * is found in the URL, the
    URL fragment is simply appended at the end of the endpoint.

    All arguments after the first will be treated as query parameters (for
    GET/HEAD/DELETE requests) or request content (for POST/PUT requests).
    For instance:

            http://my-server.com> GET /products limit=10 offset=20
      # request goes to http://my-sever.com/products?limit=10&offset=20
    
            http://my-server.com> POST /products '{"name":"A New Product"}'
      # request goes to http://my-sever.com/products with the body as specified

    You can also specify additional headers you would like included in the
    request:

README  view on Meta::CPAN

    configuration. If it is using a different character set, you can
    specify that in a second bracketed parameter:

            http://my-server.com> POST /products $(FILE[my-product.xml][latin-1])

    The contents of the file will be slurped, decoded and included as an
    argument to the command as if you had typed it on the command-line
    directly.

    TODO: Allow data structure references (from STASH or even BODY) to be
    passed to a POST or PUT command which is then serialized based on the
    content-type of the request before being sent over the wire.

 (EXPERIMENTAL) Data::DPath integration

    As an add-on to the variable interpolated described above, you can use
    dpath expressions to further process the data returned from the REST
    service. Another very contrived example:

            http://my-server.com> GET /products.json
            [{"id":"1","name":"My Product"},{"id":"2","name":"Another Product"}]

README.mkdn  view on Meta::CPAN

do things like auto-append a file extension to all URLs.  For instance:

        bash$ presto http://my-server.com*.json
        http://my-server.com> GET /product/1

In this case, the full URL would be
`http://my-server.com/product/1.json`.  If no `*` is found in the URL,
the URL fragment is simply appended at the end of the endpoint.

All arguments after the first will be treated as query parameters (for
GET/HEAD/DELETE requests) or request content (for POST/PUT requests). For
instance:

          http://my-server.com> GET /products limit=10 offset=20
    # request goes to http://my-sever.com/products?limit=10&offset=20

          http://my-server.com> POST /products '{"name":"A New Product"}'
    # request goes to http://my-sever.com/products with the body as specified

You can also specify additional headers you would like included in the request:

README.mkdn  view on Meta::CPAN

configuration.  If it is using a different character set, you can specify
that in a second bracketed parameter:

        http://my-server.com> POST /products $(FILE[my-product.xml][latin-1])

The contents of the file will be slurped, decoded and included as an
argument to the command as if you had typed it on the command-line
directly.

**TODO:** Allow data structure references (from `STASH` or even `BODY`)
to be passed to a POST or PUT command which is then serialized based
on the content-type of the request before being sent over the wire.

## (EXPERIMENTAL) Data::DPath integration

As an add-on to the variable interpolated described above, you can
use dpath expressions to further process the data returned from the
REST service.  Another very contrived example:

        http://my-server.com> GET /products.json
        [{"id":"1","name":"My Product"},{"id":"2","name":"Another Product"}]

bin/presto  view on Meta::CPAN

do things like auto-append a file extension to all URLs.  For instance:

	bash$ presto http://my-server.com*.json
	http://my-server.com> GET /product/1

In this case, the full URL would be
C<http://my-server.com/product/1.json>.  If no C<*> is found in the URL,
the URL fragment is simply appended at the end of the endpoint.

All arguments after the first will be treated as query parameters (for
GET/HEAD/DELETE requests) or request content (for POST/PUT requests). For
instance:

	http://my-server.com> GET /products limit=10 offset=20
  # request goes to http://my-sever.com/products?limit=10&offset=20

	http://my-server.com> POST /products '{"name":"A New Product"}'
  # request goes to http://my-sever.com/products with the body as specified

You can also specify additional headers you would like included in the request:

bin/presto  view on Meta::CPAN

configuration.  If it is using a different character set, you can specify
that in a second bracketed parameter:

	http://my-server.com> POST /products $(FILE[my-product.xml][latin-1])

The contents of the file will be slurped, decoded and included as an
argument to the command as if you had typed it on the command-line
directly.

B<TODO:> Allow data structure references (from C<STASH> or even C<BODY>)
to be passed to a POST or PUT command which is then serialized based
on the content-type of the request before being sent over the wire.

=head2 (EXPERIMENTAL) Data::DPath integration

As an add-on to the variable interpolated described above, you can
use dpath expressions to further process the data returned from the
REST service.  Another very contrived example:

	http://my-server.com> GET /products.json
	[{"id":"1","name":"My Product"},{"id":"2","name":"Another Product"}]

lib/App/Presto/Client.pm  view on Meta::CPAN

	my $response = $self->_rest_client->request('DELETE',$uri, shift);
	return $response;
}

sub POST {
	my $self = shift;
	my $uri  = $self->_make_uri(shift);
	$self->_rest_client->POST( $uri, shift );
}

sub PUT {
	my $self = shift;
	my $uri  = $self->_make_uri(shift);
	$self->_rest_client->PUT( $uri, shift );
}

sub _make_uri {
	my $self      = shift;
	my $local_uri = shift;
	my @args      = @_;
	my $config    = $self->config;

	my $endpoint;
	$local_uri = '/' if ! defined $local_uri;

lib/App/Presto/Command/HTTP.pm  view on Meta::CPAN

                    print "$uri\n";
                  }
            },
            map {
                my $m = $_;
                $m => {
                    desc => "perform a $m HTTP action",
                    args => [ sub { urls_for($m) } ],
                    proc => $self->_mk_proc_for($m)
                  }
              } qw(GET POST PUT DELETE HEAD)
        }
    );
}

sub _mk_proc_for {
    my $self = shift;
    my $method = shift;
    my $client = $self->client;
    return sub {
        add_url($method => $_[0]);

lib/App/Presto/Command/HTTP.pm  view on Meta::CPAN

    return $message->content_type =~ m{\b(?:xml|^text|application/json|application/x-www-form-urlencoded)\b} || do {
        my $content = substr($message->decoded_content, 0, 1000);
        my $non_printable =()= $content =~ m/([^[:print:]])/g;
        $content eq '' || ($non_printable / length($content)) > 0.3;
    };
}

sub help_categories {
    return {
        desc => 'Various HTTP verb commands',
        cmds => [qw(GET POST HEAD PUT DELETE)],
    };
}

1;

__END__

=pod

=encoding UTF-8

t/04-client.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Test::MockObject;
use App::Presto::Client;

my $config = Test::MockObject->new;
$config->set_always( endpoint => 'http://my-server.com');
my $rest_client = Test::MockObject->new;
$rest_client->set_true('GET','DELETE','PUT','POST','HEAD','request');

my %headers;
$rest_client->mock('addHeader', sub { shift; my($k,$v) = @_; $headers{$k} = $v; });
$rest_client->{_headers} = \%headers;

my $client = App::Presto::Client->new(config=>$config, _rest_client => $rest_client);

isa_ok($client, 'App::Presto::Client');

$client->GET('/foo');

t/04-client.t  view on Meta::CPAN

	is $args->[1], 'http://my-server.com/foo', 'constructs correct URI';
}

$client->DELETE('http://another-server.com/blah');
{
	my ( $m, $args ) = $rest_client->next_call;
	is $m, 'request', 'rest_client request';
	is $args->[2], 'http://another-server.com/blah', 'allows URI override';
}

$client->PUT('/bar', 'foobar');
{
	my ( $m, $args ) = $rest_client->next_call;
	is $m, 'PUT', 'rest_client PUT';
	is $args->[2], 'foobar', 'PUT body';
}

$config->set_always( endpoint => 'http://my-server.com*.json');
$client->PUT('/bar?blah=1', 'foobar');
{
	my ( $m, $args ) = $rest_client->next_call;
	is $m, 'PUT', 'rest_client PUT';
	is $args->[1], 'http://my-server.com/bar.json?blah=1', 'has suffix + query params';
}
$config->set_always( endpoint => 'http://my-server.com');

$client->HEAD;
{
	my ( $m, $args ) = $rest_client->next_call;
	is $m, 'HEAD', 'rest_client HEAD (no uri)';
	is $args->[1], 'http://my-server.com/', 'default URI';
}



( run in 0.589 second using v1.01-cache-2.11-cpan-4e96b696675 )