File Coverage

lib/Net/Upwork/API.pm
Criterion Covered Total %
statement 15 49 30.6
branch 0 6 0.0
condition 0 3 0.0
subroutine 5 12 41.6
pod 7 7 100.0
total 27 77 35.0


line stmt bran cond sub pod time code
1             # Licensed under the Upwork's API Terms of Use;
2             # you may not use this file except in compliance with the Terms.
3             #
4             # Unless required by applicable law or agreed to in writing, software
5             # distributed under the License is distributed on an "AS IS" BASIS,
6             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7             # See the License for the specific language governing permissions and
8             # limitations under the License.
9             #
10             # Author:: Maksym Novozhylov (mnovozhilov@upwork.com)
11             # Copyright:: Copyright 2015(c) Upwork.com
12             # License:: See LICENSE.txt and TOS - https://developers.upwork.com/api-tos.html
13              
14             package Net::Upwork::API;
15              
16 33     33   426208 use strict;
  33         89  
  33         1304  
17 33     33   212 use warnings;
  33         67  
  33         1833  
18              
19 33     33   13593 use Net::Upwork::API::Config;
  33         113  
  33         1249  
20 33     33   13957 use Net::Upwork::API::Client;
  33         127  
  33         2027  
21              
22             our $VERSION = '2.3.0';
23              
24 33     33   225 use constant TOKEN_TYPE_BEARER => 'Bearer';
  33         60  
  33         20898  
25              
26             =pod
27              
28             =head1 NAME
29              
30             Net::Upwork::API - Perl bindings for Upwork API (OAuth2).
31              
32             =head1 FUNCTIONS
33              
34             =over 4
35              
36             =item new($config)
37              
38             Create a new Config
39              
40             B
41              
42             $config
43              
44             Config object
45              
46             =cut
47              
48             sub new {
49 0     0 1   my $class = shift;
50 0           my $config = shift;
51 0           my %opts = @_;
52 0           $opts{config} = $config;
53              
54 0           my $client = Net::Upwork::API::Client->new($config);
55 0           $opts{client} = $client;
56              
57 0           my $self = bless \%opts, $class;
58              
59 0           return $self;
60             }
61              
62             =item init_router
63              
64             Initialize router
65              
66             B
67              
68             $class
69              
70             Class
71              
72             $api
73              
74             API object
75              
76             $epoint
77              
78             Entry point for the router
79              
80             B
81              
82             Object
83              
84             =cut
85              
86             sub init_router {
87 0     0 1   my $class = shift;
88 0           my $api = shift;
89 0           my $epoint = shift;
90 0           my %opts = @_;
91              
92 0           $opts{client} = $api->{client};
93 0           $opts{client}{epoint} = $epoint;
94 0           my $self = bless \%opts, $class;
95              
96 0           return $self;
97             }
98              
99             =item get_access_token()
100              
101             Get access token key/secret pair
102              
103             B
104              
105             $code
106              
107             Authorization Code, see https://tools.ietf.org/html/rfc6749.html#section-1.3.1
108              
109             B
110              
111             Net::OAuth2::AccessToken object
112              
113             =cut
114              
115             sub get_access_token {
116 0     0 1   my $self = shift;
117 0           my ($code) = @_;
118              
119 0 0         if (defined $code) {
120 0           chomp($code);
121             }
122              
123 0           $self->{client}{access_token_session} = $self->{client}{oauth_client}->get_access_token($code);
124              
125 0           return $self->{client}{access_token_session};
126             }
127              
128             =item get_authorization_url()
129              
130             Get Authorization Url and request token
131              
132             B
133              
134             A string for authorization in the browser
135              
136             =cut
137              
138             sub get_authorization_url {
139 0     0 1   my $self = shift;
140              
141 0           return $self->{client}{request_token} = $self->{client}{oauth_client}->authorize_response->as_string;
142             }
143              
144             =item has_access_token()
145              
146             Check if access token has been already received
147              
148             B
149              
150             Boolean
151              
152             =cut
153              
154             sub has_access_token {
155 0     0 1   my $self = shift;
156              
157             return defined $self->{client}{access_token} ||
158 0   0       (!($self->{config}{access_token} eq "") && !($self->{config}{refresh_token} eq ""));
159             }
160              
161             =item set_access_token_session()
162              
163             Sets the AccessToken session based on the provided config
164              
165             B
166              
167             Net::OAuth2::AccessToken object
168              
169             =cut
170              
171             sub set_access_token_session() {
172 0     0 1   my $self = shift;
173 0           my $auto_refresh = shift;
174              
175             $self->{client}{access_token_session} = Net::OAuth2::AccessToken->new(
176             profile => $self->{client}->get_oauth_client,
177             auto_refresh => $auto_refresh ? 1 : 0,
178             (
179             access_token => $self->{config}{access_token},
180             refresh_token => $self->{config}{refresh_token},
181             token_type => TOKEN_TYPE_BEARER,
182             expires_in => $self->{config}{expires_in},
183             expires_at => $self->{config}{expires_at}
184             )
185 0 0         );
186              
187             # expire? then refresh
188 0 0         if ($self->{config}{expires_at} < time()) {
189 0           $self->{client}{access_token_session}->refresh();
190             }
191              
192 0           return $self->{client}{access_token_session};
193             }
194              
195             =item client()
196              
197             Get client object
198              
199             B
200              
201             Object
202              
203             =cut
204              
205             sub client {
206 0     0 1   my $self = shift;
207 0           return $self->{client};
208             }
209              
210             =back
211              
212             =head1 LICENSE
213              
214             This is released under the Apache Version 2.0
215             License. See L.
216              
217             =head1 AUTHOR
218              
219             Maksym Novozhylov C<< >>
220              
221             =head1 COPYRIGHT
222              
223             Copyright E Upwork Global Corp., 2018
224              
225             =cut
226              
227             1;