File Coverage

blib/lib/Google/Ads/GoogleAds/LongRunning/OperationService.pm
Criterion Covered Total %
statement 23 53 43.4
branch 0 6 0.0
condition n/a
subroutine 8 12 66.6
pod 4 5 80.0
total 35 76 46.0


line stmt bran cond sub pod time code
1             # Copyright 2019, Google LLC
2             #
3             # Licensed under the Apache License, Version 2.0 (the "License");
4             # you may not use this file except in compliance with the License.
5             # You may obtain a copy of the License at
6             #
7             # http://www.apache.org/licenses/LICENSE-2.0
8             #
9             # Unless required by applicable law or agreed to in writing, software
10             # distributed under the License is distributed on an "AS IS" BASIS,
11             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12             # See the License for the specific language governing permissions and
13             # limitations under the License.
14             #
15             # This module manages long-running operations with an API service.
16              
17              
18             use strict;
19 2     2   1183 use warnings;
  2         3  
  2         48  
20 2     2   9 use base qw(Google::Ads::GoogleAds::BaseService);
  2         3  
  2         58  
21 2     2   9  
  2         4  
  2         436  
22             use Google::Ads::GoogleAds::Constants;
23 2     2   12  
  2         4  
  2         50  
24             use Time::HiRes qw(sleep);
25 2     2   10  
  2         4  
  2         17  
26             use constant DEFAULT_POLL_FREQUENCY_SECONDS => 5;
27 2     2   274 use constant DEFAULT_POLL_TIMEOUT_SECONDS => 60;
  2         3  
  2         154  
28 2     2   11  
  2         5  
  2         782  
29             # Class::Std-style attributes. Need to be kept in the same line.
30             # These need to go in the same line for older Perl interpreters to understand.
31             my %version_of : ATTR(:name<version> :default<>);
32              
33             # Automatically called by Class::Std after the values for all the attributes
34             # have been populated but before the constructor returns the new object.
35             my ($self, $ident) = @_;
36              
37 1     1 0 8 $version_of{$ident} =
38             lc Google::Ads::GoogleAds::Constants::OPERATION_SERVICE_VERSION;
39 1         3 }
40              
41             # Gets the latest state of a long-running operation.
42             my $self = shift;
43             my $request_body = shift;
44             my $http_method = 'GET';
45 0     0 1   my $request_path = sprintf '%s/{+name}', $self->get_version();
46 0           my $response_type = 'Google::Ads::GoogleAds::LongRunning::Operation';
47 0            
48 0           return $self->SUPER::call($http_method, $request_path, $request_body,
49 0           $response_type);
50             }
51 0            
52             # Deletes a long-running operation.
53             my $self = shift;
54             my $request_body = shift;
55             my $http_method = 'DELETE';
56             my $request_path = sprintf '%s/{+name}', $self->get_version();
57 0     0 1   my $response_type = '';
58 0            
59 0           return $self->SUPER::call($http_method, $request_path, $request_body,
60 0           $response_type);
61 0           }
62              
63 0           # Starts asynchronous cancellation on a long-running operation.
64             my $self = shift;
65             my $request_body = shift;
66             my $http_method = 'POST';
67             my $request_path = sprintf '%s/{+name}:cancel', $self->get_version();
68             my $response_type = '';
69 0     0 1    
70 0           return $self->SUPER::call($http_method, $request_path, $request_body,
71 0           $response_type);
72 0           }
73 0            
74             # Polls the specified long-running operation until it is done or reaches at most
75 0           # a specified timeout.
76             my $self = shift;
77             my $request_body = shift;
78              
79             my $poll_frequency_seconds =
80             $request_body->{pollFrequencySeconds}
81             ? $request_body->{pollFrequencySeconds}
82 0     0 1   : DEFAULT_POLL_FREQUENCY_SECONDS;
83 0            
84             my $poll_timeout_seconds =
85             $request_body->{pollTimeoutSeconds}
86             ? $request_body->{pollTimeoutSeconds}
87             : DEFAULT_POLL_TIMEOUT_SECONDS;
88 0 0          
89             my $lro;
90             my $elapsed_seconds = 0;
91             while ($elapsed_seconds < $poll_timeout_seconds) {
92             $lro = $self->get({name => $request_body->{name}});
93 0 0          
94             last if $lro->{done};
95 0            
96 0           sleep($poll_frequency_seconds);
97 0           $elapsed_seconds += $poll_frequency_seconds;
98 0           }
99              
100 0 0         return $lro;
101             }
102 0            
103 0           1;
104              
105             =pod
106 0            
107             =head1 NAME
108              
109             Google::Ads::GoogleAds::LongRunning::OperationService
110              
111             =head1 SYNOPSIS
112              
113             my $operation_service = $api_client->OperationService();
114              
115             $operation_service->poll_until_complete({
116             name => $lro_name,
117             pollFrequencySeconds => 5,
118             pollTimeoutSeconds => 60
119             });
120              
121             =head1 DESCRIPTION
122              
123             This module manages long-running operations with an API service.
124              
125             =head1 METHODS
126              
127             =head2 get
128              
129             Gets the latest state of a long-running operation.
130              
131             =head3 Parameters
132              
133             =over
134              
135             =item *
136              
137             I<request_body>: a L<Google::Ads::GoogleAds::LongRunning::GetOperationRequest> object.
138              
139             =back
140              
141             =head2 delete
142              
143             Deletes a long-running operation.
144              
145             =head3 Parameters
146              
147             =over
148              
149             =item *
150              
151             I<request_body>: a L<Google::Ads::GoogleAds::LongRunning::DeleteOperationRequest> object.
152              
153             =back
154              
155             =head2 cancel
156              
157             Starts asynchronous cancellation on a long-running operation.
158              
159             =head3 Parameters
160              
161             =over
162              
163             =item *
164              
165             I<request_body>: a L<Google::Ads::GoogleAds::LongRunning::CancelOperationRequest> object.
166              
167             =back
168              
169             =head2 poll_until_done
170              
171             Polls the specified long-running operation until it is done or reaches at most a
172             specified timeout.
173              
174             =head3 Parameters
175              
176             =over
177              
178             =item *
179              
180             I<request_body>: a L<Google::Ads::GoogleAds::LongRunning::PollOperationRequest> object.
181              
182             =back
183              
184             =head1 LICENSE AND COPYRIGHT
185              
186             Copyright 2019 Google LLC
187              
188             Licensed under the Apache License, Version 2.0 (the "License");
189             you may not use this file except in compliance with the License.
190             You may obtain a copy of the License at
191              
192             http://www.apache.org/licenses/LICENSE-2.0
193              
194             Unless required by applicable law or agreed to in writing, software
195             distributed under the License is distributed on an "AS IS" BASIS,
196             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
197             See the License for the specific language governing permissions and
198             limitations under the License.
199              
200             =head1 REPOSITORY INFORMATION
201              
202             $Rev: $
203             $LastChangedBy: $
204             $Id: $
205              
206             =cut