BigQuery/V2.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 181 CC 18 SCAR 54.3
CC
18
Cov
0%
request
342.0
new
2.0
DESTROY
2.0
CRAP 228.7
1 package Google::BigQuery::V2;
2 use 5.010001;
3 use strict;
4 use warnings;
5  
6 use base qw(Google::BigQuery);
7  
8 use HTTP::Request;
9 use JSON qw(decode_json encode_json);
10 use URI::Escape;
11  
12 sub new {
13 my ($class, %args) = @_;
14  
15 my $self = $class->SUPER::new(
16 %args,
17 GOOGLE_BIGQUERY_REST_DESCRIPTION => 'https://www.googleapis.com/discovery/v1/apis/bigquery/v2/rest',
18 GOOGLE_BIGQUERY_API_BASE_URL => 'https://www.googleapis.com/bigquery/v2'
19 );
20  
21 bless $self, $class;
22 }
23  
24 sub DESTROY {
25 }
26  
27 sub request {
28 my ($self, %args) = @_;
29  
30 my $resource = $args{resource} || die;
31 my $method = $args{method} || die;
32 my $project_id = defined $args{project_id} ? $args{project_id} : $self->{project_id};
33 my $dataset_id = defined $args{dataset_id} ? $args{dataset_id} : $self->{dataset_id};
34 my $table_id = $args{table_id} || '';
35 my $job_id = $args{job_id} || '';
36  
37 my $header = HTTP::Headers->new(Authorization => "Bearer $self->{access_token}{access_token}");
38 my $rest_description = $self->{rest_description}{resources}{$resource}{methods}{$method} || die;
39 my $http_method = $rest_description->{httpMethod};
40 my $path = join('/', $self->{GOOGLE_BIGQUERY_API_BASE_URL}, $rest_description->{path});
41 $path =~ s/{projectId}/$project_id/;
42 $path =~ s/{datasetId}/$dataset_id/;
43 $path =~ s/{tableId}/$table_id/;
44 $path =~ s/{jobId}/$job_id/;
45  
46 if ($self->{exp} < time + 60) {
47 $self->_auth;
48 }
49  
50 my $request;
51 if ($resource eq 'jobs' && $method eq 'insert') {
52 my $sub_method = (keys %{$args{content}->{configuration}})[0];
53  
54 if ($sub_method !~ /^load$/ || defined $args{content}->{configuration}{load}{sourceUris}) {
55 $request = HTTP::Request->new($http_method, $path, $header);
56 if ($http_method =~ /^(?:POST|PUT|PATCH)$/) {
57 $request->header('Content-Type' => 'application/json');
58 $request->content(encode_json($args{content}));
59 }
60 } else {
61 my $upload_path = $rest_description->{mediaUpload}{protocols}{simple}{path};
62 $upload_path = "https://www.googleapis.com" . $upload_path;
63 $upload_path =~ s/{projectId}/$project_id/;
64 $upload_path .= "?uploadType=multipart";
65  
66 $header->header('Content-Type' => 'multipart/related');
67 $request = HTTP::Request->new($http_method, $upload_path, $header);
68  
69 $request->add_part(
70 HTTP::Message->new(
71 ['Content-Type' => 'application/json; charset=UTF-8'],
72 encode_json($args{content})
73 )
74 );
75  
76 my $data;
77 open my $in, "<", $args{data} or die "can't open $args{data} : $!";
78 $data = join('', <$in>);
79 close $in;
80  
81 $request->add_part(
82 HTTP::Message->new(
83 ['Content-Type' => 'application/octet-stream'],
84 $data
85 )
86 );
87 }
88  
89 my $response = $self->{ua}->request($request);
90 if (defined $response->content) {
91 my $content = decode_json($response->content);
92 if (defined $content->{error}) {
93 return { error => { message => $content->{error}{message} } };
94 } elsif ($response->code == 200) {
95 my $json_response = decode_json($response->decoded_content);
96  
97 # return if async is true
98 return $json_response if $args{async};
99  
100 my $job_id = $json_response->{jobReference}{jobId};
101 while (1) {
102 my $json_response = $self->request(method => 'get', resource => 'jobs', job_id => $job_id);
103 if ($json_response->{status}{state} eq 'DONE') {
104 return $json_response;
105 } else {
106 print "Wating...(state: $json_response->{status}{state})\n" if defined $self->{verbose};
107 sleep(1);
108 }
109 }
110 } else {
111 die;
112 }
113 } else {
114 die;
115 }
116 } else {
117 if (defined $args{query_string} && %{$args{query_string}}) {
118 my @query_string;
119 while (my ($key, $value) = each %{$args{query_string}}) {
120 push @query_string, join('=', uri_escape($key), uri_escape($value));
121 }
122 $path = join('?', $path, join('&', @query_string));
123 }
124  
125 my $request = HTTP::Request->new($http_method, $path, $header);
126 if ($http_method =~ /^(?:POST|PUT|PATCH)$/) {
127 $request->header('Content-Type' => 'application/json');
128 $request->content(encode_json($args{content}));
129 }
130 my $response = $self->{ua}->request($request);
131  
132 if ($response->code == 204) {
133 return {};
134 } elsif (defined $response->content) {
135 # easy json check
136 if ($response->content =~ /^\s*[{\[]/) {
137 return decode_json($response->decoded_content);
138 } else {
139 return { error => { message => $response->content }};
140 }
141 } else {
142 return { error => { message => 'Unknown Error' }};
143 }
144 }
145 }
146  
147 1;