line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::SmartSheet; |
2
|
2
|
|
|
2
|
|
82986
|
use Moo; |
|
2
|
|
|
|
|
54453
|
|
|
2
|
|
|
|
|
13
|
|
3
|
2
|
|
|
2
|
|
5539
|
use MooX::late; |
|
2
|
|
|
|
|
62709
|
|
|
2
|
|
|
|
|
18
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# Example error message, (status_line and content): |
8
|
|
|
|
|
|
|
# 400 Bad Request{"errorCode":5026,"message":"Your account has reached the maximum number of sheets allowed for your trial. To save additional sheets, you must upgrade to a paid plan."} |
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
343
|
use Carp (); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
40
|
|
11
|
2
|
|
|
2
|
|
2650
|
use Data::Dumper qw(Dumper); |
|
2
|
|
|
|
|
21102
|
|
|
2
|
|
|
|
|
173
|
|
12
|
2
|
|
|
2
|
|
2177
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
104841
|
|
|
2
|
|
|
|
|
80
|
|
13
|
2
|
|
|
2
|
|
2264
|
use JSON qw(from_json to_json); |
|
2
|
|
|
|
|
27309
|
|
|
2
|
|
|
|
|
13
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has token => (is => 'ro', required => 1); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has sheets => (is => 'rw', isa => 'ArrayRef'); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $API_URL = "https://api.smartsheet.com/1.1"; |
20
|
|
|
|
|
|
|
my @ACCESS_LEVELS = qw(VIEWER EDITOR EDITOR_SHARE ADMIN); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub ua { |
23
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new( agent => "WWW::SmartSheet v$VERSION https://github.com/szabgab/WWW-SmartSheet" ); |
26
|
0
|
|
|
|
|
|
$ua->timeout(10); |
27
|
0
|
|
|
|
|
|
$ua->default_header("Authorization" => "Bearer " . $self->token); |
28
|
0
|
|
|
|
|
|
$ua->default_header("Content-Type" => "application/json"); |
29
|
0
|
|
|
|
|
|
return $ua; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub get_sheets { |
34
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
my $all_sheets = $self->_get('sheets'); |
37
|
0
|
|
|
|
|
|
$self->sheets($all_sheets); |
38
|
0
|
|
|
|
|
|
return $all_sheets; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# TODO how can I make sure that get_sheet is called if sheets is empty ? is that the lazy attribute? |
44
|
|
|
|
|
|
|
sub get_columns { |
45
|
0
|
|
|
0
|
1
|
|
my ($self, $sheet_number) = @_; |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $sheets = $self->sheets; |
48
|
0
|
|
|
|
|
|
$self->_get("sheet/$sheets->[$sheet_number]{id}/columns"); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub share_sheet { |
53
|
0
|
|
|
0
|
1
|
|
my ($self, $sheet_id, $email, $access_level) = @_; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
$self->_post("sheet/$sheet_id/shares?sendEmail=true", {email => $email, accessLevel => $access_level}); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub create_sheet { |
60
|
0
|
|
|
0
|
1
|
|
my ($self, %args) = @_; |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
return $self->_post('sheets', \%args); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub delete_sheet { |
66
|
0
|
|
|
0
|
0
|
|
my ($self, $id) = @_; |
67
|
0
|
|
|
|
|
|
$self->_delete("sheet/$id"); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub add_column { |
72
|
0
|
|
|
0
|
1
|
|
my ($self, $sheet_id, $column) = @_; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
return $self->_post("sheet/$sheet_id/columns", $column ); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub insert_rows { |
79
|
0
|
|
|
0
|
1
|
|
my ($self, $sheet_id, $rows, %args) = @_; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
my $res = $self->ua->get("$API_URL/sheet/$sheet_id/columns"); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
#_post("sheet/$sheet_id/rows", $rows) |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub get_sheet_by_id { |
87
|
0
|
|
|
0
|
0
|
|
my ($self, $id) = @_; |
88
|
0
|
|
|
|
|
|
my $data = $self->_get("sheet/$id"); |
89
|
0
|
|
|
|
|
|
require WWW::SmartSheet::Sheet; |
90
|
0
|
|
|
|
|
|
WWW::SmartSheet::Sheet->new($data); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _post { |
94
|
0
|
|
|
0
|
|
|
my ($self, $path, $data) = @_; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
my $url = "$API_URL/$path"; |
97
|
0
|
|
|
|
|
|
my $ua = $self->ua; |
98
|
0
|
|
|
|
|
|
my $json = to_json($data); |
99
|
|
|
|
|
|
|
#warn $json; |
100
|
|
|
|
|
|
|
#warn Dumper $ua->default_headers; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
my $req = HTTP::Request->new( 'POST', $url ); |
103
|
0
|
|
|
|
|
|
$req->content( $json ); |
104
|
0
|
|
|
|
|
|
my $res = $ua->request( $req ); |
105
|
|
|
|
|
|
|
#my $res = $ua->post($url, Content => $json); |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
Carp::croak $res->status_line . $res->content if not $res->is_success; |
108
|
0
|
|
|
|
|
|
return from_json $res->decoded_content; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _get { |
112
|
0
|
|
|
0
|
|
|
my ($self, $path) = @_; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
my $url = "$API_URL/$path"; |
115
|
0
|
|
|
|
|
|
my $res = $self->ua->get($url); |
116
|
0
|
0
|
|
|
|
|
Carp::croak $res->status_line . $res->content if not $res->is_success; |
117
|
0
|
|
|
|
|
|
return from_json $res->decoded_content; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _delete { |
121
|
0
|
|
|
0
|
|
|
my ($self, $path) = @_; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
my $url = "$API_URL/$path"; |
124
|
0
|
|
|
|
|
|
my $res = $self->ua->delete($url); |
125
|
0
|
0
|
|
|
|
|
Carp::croak $res->status_line . $res->content if not $res->is_success; |
126
|
0
|
|
|
|
|
|
return from_json $res->decoded_content; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
__END__ |