Activiti-Rest-Client

 view release on metacpan or  search on metacpan

lib/Activiti/Rest/UserAgent/LWP.pm  view on Meta::CPAN

package Activiti::Rest::UserAgent::LWP;
use Activiti::Sane;
use Carp qw(confess);
use Moo;
use LWP::UserAgent;
use URI::Escape qw(uri_escape);
use Data::Util qw(:check :validate);

with qw(Activiti::Rest::UserAgent);

has ua => (
  is => 'ro',
  lazy => 1,
  builder => '_build_ua'
);
sub _build_ua {
  my $self = $_[0];
  my $ua = LWP::UserAgent->new(
    cookie_jar => {},
    timeout => $self->timeout(),
    keep_alive => $self->keep_alive()
  );
  if(is_string($ENV{LWP_TRACE})){
    $ua->add_handler("request_send",  sub { print STDERR shift->as_string; return });
    $ua->add_handler("response_done", sub { print STDERR shift->as_string; return });
  }
  for my $header(@{ $self->default_headers() }){
    $ua->default_header($header->[0] => $header->[1]);
  }
  $ua;
}

sub request {
  my($self,%args) = @_;

  my $params = $args{params};
  my $method = $args{method} || "GET";
  my $path = $args{path} || "";
  my $headers = $args{headers};

  my $url = $self->url().$path;

  my $res;
  if(uc($method) eq "GET"){
    $res = $self->_get($url,$params,$headers);
  }elsif(uc($method) eq "POST"){
    $res = $self->_post($url,$params,$headers);
  }elsif(uc($method) eq "PUT"){
    $res = $self->_put($url,$params,$headers);
  }elsif(uc($method) eq "DELETE"){
    $res = $self->_delete($url,$params,$headers);
  }else{
    confess "method $method not supported";
  }

  $res;
}
sub _post {
  my($self,$url,$params,$headers)=@_;
  my @args = ($url,_construct_params_as_array($params));
  my @headers;
  @headers = @{ _construct_params_as_array($headers) } if is_hash_ref($headers);
  push @args,@headers;
  $self->ua->post(@args);
}
sub _put {
  my($self,$url,$params,$headers)=@_;



( run in 1.877 second using v1.01-cache-2.11-cpan-39bf76dae61 )