| 1 | package Google::Cloud::REST::Client; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Moo; | ||
| 6 | use LWP::UserAgent; | ||
| 7 | use HTTP::Request; | ||
| 8 | use JSON::MaybeXS qw(encode_json decode_json); | ||
| 9 | use Carp qw(croak); | ||
| 10 | use Log::Any qw($log); | ||
| 11 | use Time::HiRes qw(sleep); | ||
| 12 | |||
| 13 | our $VERSION = '0.01'; | ||
| 14 | |||
| 15 | has target => ( | ||
| 16 | is => 'ro', | ||
| 17 | required => 1, | ||
| 18 | ); | ||
| 19 | |||
| 20 | has auth_token => ( | ||
| 21 | is => 'ro', | ||
| 22 | required => 0, | ||
| 23 | ); | ||
| 24 | |||
| 25 | has timeout => ( | ||
| 26 | is => 'ro', | ||
| 27 | default => sub { 30 }, | ||
| 28 | ); | ||
| 29 | |||
| 30 | has max_retries => ( | ||
| 31 | is => 'ro', | ||
| 32 | default => sub { 3 }, | ||
| 33 | ); | ||
| 34 | |||
| 35 | around BUILDARGS => sub { | ||
| 36 | my ($orig, $class, @args) = @_; | ||
| 37 | my $h = $class->$orig(@args); | ||
| 38 | if (exists $h->{ua} && !exists $h->{user_agent}) { | ||
| 39 | $h->{user_agent} = delete $h->{ua}; | ||
| 40 | } | ||
| 41 | return $h; | ||
| 42 | }; | ||
| 43 | |||
| 44 | has user_agent => ( | ||
| 45 | is => 'rw', | ||
| 46 | lazy => 1, | ||
| 47 | builder => '_build_user_agent', | ||
| 48 | ); | ||
| 49 | |||
| 50 | sub _build_user_agent { | ||
| 51 | my ($self) = @_; | ||
| 52 | my $ua = LWP::UserAgent->new( | ||
| 53 | timeout => $self->timeout, | ||
| 54 | agent => 'google-cloud-perl-rest/' . $VERSION, | ||
| 55 | ); | ||
| 56 | return $ua; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub ua { my $s = shift; return $s->user_agent(@_); } | ||
| 60 | |||
| 61 | sub call { | ||
| 62 | my ($self, $args) = @_; | ||
| 63 | if (@_ > 2 && scalar(@_) % 2 == 1) { | ||
| 64 | my ($s, %kw) = @_; | ||
| 65 | $args = \%kw; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $http_method = uc($args->{http_method} || $args->{method} || 'POST'); | ||
| 69 | my $path = $args->{path} || $args->{url} || ''; | ||
| 70 | my $request_obj = $args->{request}; | ||
| 71 | my $response_class = $args->{response_class}; | ||
| 72 | my $query_params = $args->{query_params} || {}; | ||
| 73 | |||
| 74 | # Build full URL | ||
| 75 | my $base_url = $self->target; | ||
| 76 | unless ($base_url =~ /^https?:\/\//) { | ||
| 77 | $base_url = 'https://' . $base_url; | ||
| 78 | } | ||
| 79 | $base_url =~ s/\/+$//; | ||
| 80 | |||
| 81 | if ($path) { | ||
| 82 | $path =~ s/^\/+//; | ||
| 83 | $base_url .= '/' . $path; | ||
| 84 | } | ||
| 85 | |||
| 86 | # Append query parameters if any | ||
| 87 | if (keys %$query_params) { | ||
| 88 | my @pairs; | ||
| 89 | for my $k (sort keys %$query_params) { | ||
| 90 | push @pairs, sprintf('%s=%s', $k, $query_params->{$k}); | ||
| 91 | } | ||
| 92 | $base_url .= '?' . join('&', @pairs); | ||
| 93 | } | ||
| 94 | |||
| 95 | # Build HTTP request | ||
| 96 | my $req = HTTP::Request->new($http_method => $base_url); | ||
| 97 | $req->header('Content-Type' => 'application/json'); | ||
| 98 | |||
| 99 | # Add Authorization token | ||
| 100 | my $token = $self->auth_token; | ||
| 101 | if ($token) { | ||
| 102 | if (ref($token) && eval { $token->can('get_token') }) { | ||
| 103 | $token = $token->get_token(); | ||
| 104 | } | ||
| 105 | $req->header('Authorization' => 'Bearer ' . $token); | ||
| 106 | } | ||
| 107 | |||
| 108 | # Encode body | ||
| 109 | if ($request_obj) { | ||
| 110 | my $payload = {}; | ||
| 111 | if (ref($request_obj) && eval { $request_obj->can('to_hash') }) { | ||
| 112 | $payload = $request_obj->to_hash(); | ||
| 113 | } elsif (ref($request_obj) eq 'HASH') { | ||
| 114 | $payload = $request_obj; | ||
| 115 | } | ||
| 116 | if (keys %$payload) { | ||
| 117 | $req->content(encode_json($payload)); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | # Execute HTTP request with retries | ||
| 122 | my $retries = 0; | ||
| 123 | my $res; | ||
| 124 | my $backoff = 0.1; | ||
| 125 | |||
| 126 | while (1) { | ||
| 127 | $log->debugf('REST Request: %s %s', $http_method, $base_url); | ||
| 128 | $res = $self->user_agent->request($req); | ||
| 129 | |||
| 130 | if ($res->is_success) { | ||
| 131 | last; | ||
| 132 | } | ||
| 133 | |||
| 134 | my $code = $res->code; | ||
| 135 | if (($code == 502 || $code == 503 || $code == 504) && $retries < $self->max_retries) { | ||
| 136 | $retries++; | ||
| 137 | $log->warnf('REST Transient HTTP %d response, retrying (%d/%d) in %.2fs...', $code, $retries, $self->max_retries, $backoff); | ||
| 138 | sleep($backoff); | ||
| 139 | $backoff *= 2.0; | ||
| 140 | next; | ||
| 141 | } | ||
| 142 | |||
| 143 | # Unrecoverable error | ||
| 144 | my $err_msg = sprintf('REST API HTTP Error %d: %s', $code, $res->status_line); | ||
| 145 | if ($res->content) { | ||
| 146 | $err_msg .= ' - ' . $res->content; | ||
| 147 | } | ||
| 148 | croak $err_msg; | ||
| 149 | } | ||
| 150 | |||
| 151 | # Parse response | ||
| 152 | my $content = $res->content; | ||
| 153 | return unless defined $content && length $content; | ||
| 154 | |||
| 155 | my $decoded_json = eval { decode_json($content) }; | ||
| 156 | if ($@) { | ||
| 157 | croak 'REST Error decoding JSON response: ' . $@; | ||
| 158 | } | ||
| 159 | |||
| 160 | if ($response_class) { | ||
| 161 | if (eval { $response_class->can('from_hash') }) { | ||
| 162 | return $response_class->from_hash($decoded_json); | ||
| 163 | } elsif (eval { $response_class->can('new') }) { | ||
| 164 | return $response_class->new(ref($decoded_json) eq 'HASH' ? %$decoded_json : ()); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | return $decoded_json; | ||
| 169 | } | ||
| 170 | |||
| 171 | *request = \&call; | ||
| 172 | |||
| 173 | 1; |