/lib/Net/Hadoop/Oozie/Role/LWP.pm

https://github.com/Perl-Hadoop/Net-Hadoop-Oozie · Perl · 119 lines · 72 code · 18 blank · 29 comment · 15 complexity · a10ddbdb537b2ec56c341fcdc8e628b5 MD5 · raw file

  1. package Net::Hadoop::Oozie::Role::LWP;
  2. use 5.010;
  3. use strict;
  4. use warnings;
  5. use Carp qw( confess );
  6. use Constant::FromGlobal DEBUG => { int => 1, default => 0, env => 1 };
  7. use JSON::XS;
  8. use LWP::UserAgent;
  9. use Moo::Role;
  10. use Scalar::Util qw( blessed );
  11. with 'Net::Hadoop::YARN::Roles::Common';
  12. my $json = JSON::XS->new->pretty(1)->canonical(1);
  13. # TODO: use the one from YARN or migrate there
  14. sub agent_request {
  15. my $self = shift;
  16. my ($uri, $method, $payload) = @_;
  17. print "OOZIE URI: $uri\n" if DEBUG;
  18. my $response;
  19. if (!$method || $method eq 'get') {
  20. $response = $self->ua->get($uri);
  21. }
  22. elsif ($method eq 'post') {
  23. $response = $self->ua->post(
  24. $uri,
  25. 'Content-Type' => "application/xml;charset=UTF-8",
  26. Content => $payload
  27. );
  28. }
  29. elsif ($method eq 'put') {
  30. $response = $self->ua->put( $uri,
  31. 'Content-Type' => "application/xml;charset=UTF-8",
  32. );
  33. }
  34. else {
  35. die "Unknown method";
  36. }
  37. my $content = $response->decoded_content || '';
  38. if ( $response->is_success ) {
  39. return {} if !$content;
  40. my $type = $response->header('content-type') || q{};
  41. return { response => $content } if index( lc $type, 'json' ) == -1;
  42. my $res;
  43. eval {
  44. $res = $json->decode($content);
  45. 1;
  46. } or do {
  47. my $eval_error = $@ || 'Zombie error';
  48. confess q{server response wasn't valid JSON: } . $eval_error;
  49. };
  50. return $res;
  51. }
  52. my $headers = $response->headers;
  53. my $code = $response->code;
  54. # collect additional error info
  55. my @msg;
  56. push @msg, $1
  57. if $content =~ m{\Q<b>description</b>\E\s+<u>(.+?)</u>}xmsi;
  58. push @msg, $headers->{'oozie-error-message'}
  59. if $headers->{'oozie-error-message'};
  60. push @msg, eval { require LWP::Authen::Negotiate; 1; }
  61. ? q{(Did you forget to run kinit?)}
  62. : q{(LWP::Authen::Negotiate doesn't seem available)}
  63. if $code == 401
  64. && ( $headers->{'www-authenticate'} || '' ) eq 'Negotiate';
  65. confess sprintf '%s %s -> %s', "@msg", $response->status_line, $uri;
  66. }
  67. 1;
  68. __END__
  69. =pod
  70. =encoding utf8
  71. =head1 NAME
  72. Net::Hadoop::Oozie::Role::LWP - User agent for Oozie requests
  73. =head1 DESCRIPTION
  74. Part of the Perl Oozie interface.
  75. =head1 SYNOPSIS
  76. with 'Net::Hadoop::Oozie::Role::LWP';
  77. # TODO
  78. =head1 METHODS
  79. =head2 agent_request
  80. TODO.
  81. =head1 SEE ALSO
  82. L<Net::Hadoop::Oozie>.
  83. =cut