line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Box::Request; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
36
|
use strict; |
|
7
|
|
|
|
|
18
|
|
|
7
|
|
|
|
|
258
|
|
4
|
7
|
|
|
7
|
|
35
|
use warnings; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
193
|
|
5
|
|
|
|
|
|
|
|
6
|
7
|
|
|
7
|
|
35
|
use Moo; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
41
|
|
7
|
7
|
|
|
7
|
|
2282
|
use Types::Standard qw(InstanceOf Dict Str); |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
52
|
|
8
|
7
|
|
|
7
|
|
6279
|
use HTTP::Tiny; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
156
|
|
9
|
7
|
|
|
7
|
|
6034
|
use HTTP::Tiny::Multipart; |
|
7
|
|
|
|
|
19724
|
|
|
7
|
|
|
|
|
229
|
|
10
|
7
|
|
|
7
|
|
57
|
use JSON; |
|
7
|
|
|
|
|
22
|
|
|
7
|
|
|
|
|
78
|
|
11
|
|
|
|
|
|
|
|
12
|
7
|
|
|
7
|
|
1234
|
use WebService::Box::Types::Library qw(OptionalStr); |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
71
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = 0.01; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has session => (is => 'ro', isa => InstanceOf["WebService::Box::Session"], required => 1); |
17
|
0
|
|
|
0
|
|
|
has agent => (is => 'ro', isa => InstanceOf["HTTP::Tiny"], lazy => 1, builder => sub { HTTP::Tiny->new } ); |
18
|
|
|
|
|
|
|
has error => (is => 'rwp', isa => OptionalStr ); |
19
|
0
|
|
|
0
|
|
|
has jsonp => (is => 'ro', isa => InstanceOf["JSON"], lazy => 1, builder => sub { JSON->new->allow_nonref } ); |
20
|
|
|
|
|
|
|
has auth_header => ( |
21
|
|
|
|
|
|
|
is => 'ro', |
22
|
|
|
|
|
|
|
isa => Dict[ |
23
|
|
|
|
|
|
|
Authorization => Str, |
24
|
|
|
|
|
|
|
], |
25
|
|
|
|
|
|
|
lazy => 1, |
26
|
|
|
|
|
|
|
builder => sub { |
27
|
0
|
|
|
0
|
|
|
my $self = shift; |
28
|
0
|
|
|
|
|
|
{ Authorization => 'Bearer ' . $self->session->auth_token }; |
29
|
|
|
|
|
|
|
}, |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub do { |
33
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
34
|
0
|
|
|
|
|
|
my %params = @_; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$self->_check_token; |
37
|
0
|
|
|
|
|
|
my $method = sprintf "_%s_%s", delete $params{qw/ressource action/}; |
38
|
0
|
|
|
|
|
|
return $self->$method( %params ); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _check_token { |
42
|
0
|
|
|
0
|
|
|
my $self = shift; |
43
|
|
|
|
|
|
|
|
44
|
0
|
0
|
|
|
|
|
if ( $self->session->expires < time ) { |
45
|
0
|
|
|
|
|
|
$self->session->refresh; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _files_get { |
50
|
0
|
|
|
0
|
|
|
my ($self, %params) = @_; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
$self->_set_error( undef ); |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
if ( !$params{id} ) { |
55
|
0
|
|
|
|
|
|
$self->session->box->error( 'Need id for request' ); |
56
|
0
|
|
|
|
|
|
$self->_set_error( 'Need id for request' ); |
57
|
0
|
|
|
|
|
|
return; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
my $url = sprintf "%sfiles/%s/", |
61
|
|
|
|
|
|
|
$self->session->box->api_url, |
62
|
|
|
|
|
|
|
$params{id}; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
my $result = $self->agent->get( $url, { headers => $self->auth_header } ); |
65
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
|
if ( !$result->{success} ) { |
67
|
0
|
|
|
|
|
|
$self->_set_error( $result->{content} ); |
68
|
0
|
|
|
|
|
|
return; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
0
|
0
|
0
|
|
|
|
my %data = %{ $self->jsonp->decode( $result->{content} || "{}" ) || {} }; |
|
0
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
return %data; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _files_upload { |
76
|
0
|
|
|
0
|
|
|
my ($self, %params) = @_; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
$self->_set_error( undef ); |
79
|
|
|
|
|
|
|
|
80
|
0
|
0
|
0
|
|
|
|
if ( !$params{file} || !$params{parent_id} ) { |
81
|
0
|
|
|
|
|
|
$self->session->box->error( 'Need file and parent id for request' ); |
82
|
0
|
|
|
|
|
|
$self->_set_error( 'Need file and parent id for request' ); |
83
|
0
|
|
|
|
|
|
return; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my %opt_param; |
87
|
0
|
|
|
|
|
|
for my $key ( qw/content_created_at content_modified_at/ ) { |
88
|
0
|
0
|
0
|
|
|
|
$opt_param{$key} = $params{$key} if $params{$key} and $params{$key} =~ m{ |
89
|
|
|
|
|
|
|
\A |
90
|
|
|
|
|
|
|
[0-9]{4} - [0-9]{2} - [0-9]{2} |
91
|
|
|
|
|
|
|
T |
92
|
|
|
|
|
|
|
[0-9]{2} : [0-9]{2} : [0-9]{2} |
93
|
|
|
|
|
|
|
(?:[+-] [0-9]{2} : [0-9]{2})? |
94
|
|
|
|
|
|
|
\z |
95
|
|
|
|
|
|
|
}xms; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
my $url = sprintf "%sfiles/content", $self->session->box->upload_url; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
my $result = $self->agent->post_multipart( |
101
|
|
|
|
|
|
|
$url, |
102
|
|
|
|
|
|
|
{ |
103
|
|
|
|
|
|
|
file => $params{file}, |
104
|
|
|
|
|
|
|
parent_id => $params{parent_id}, |
105
|
|
|
|
|
|
|
%opt_param, |
106
|
|
|
|
|
|
|
}, |
107
|
|
|
|
|
|
|
{ headers => $self->auth_header } |
108
|
|
|
|
|
|
|
); |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
if ( !$result->{success} ) { |
111
|
0
|
|
|
|
|
|
$self->_set_error( $result->{content} ); |
112
|
0
|
|
|
|
|
|
return; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
0
|
|
|
|
my %data = %{ $self->jsonp->decode( $result->{content} || "{}" ) || {} }; |
|
0
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
return %data; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
__END__ |