| 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::Client; | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 33 |  |  | 33 |  | 223 | use strict; | 
|  | 33 |  |  |  |  | 57 |  | 
|  | 33 |  |  |  |  | 1039 |  | 
| 17 | 33 |  |  | 33 |  | 195 | use warnings; | 
|  | 33 |  |  |  |  | 61 |  | 
|  | 33 |  |  |  |  | 907 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 33 |  |  | 33 |  | 16862 | use Net::OAuth2::Profile::WebServer; | 
|  | 33 |  |  |  |  | 2639032 |  | 
|  | 33 |  |  |  |  | 1317 |  | 
| 20 | 33 |  |  | 33 |  | 29146 | use IO::Socket::SSL qw( SSL_VERIFY_NONE ); | 
|  | 33 |  |  |  |  | 3055859 |  | 
|  | 33 |  |  |  |  | 297 |  | 
| 21 | 33 |  |  | 33 |  | 3471 | use LWP::UserAgent; | 
|  | 33 |  |  |  |  | 72 |  | 
|  | 33 |  |  |  |  | 1143 |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 33 |  |  | 33 |  | 181 | use constant BASE_HOST      => "https://www.upwork.com"; | 
|  | 33 |  |  |  |  | 67 |  | 
|  | 33 |  |  |  |  | 2230 |  | 
| 24 | 33 |  |  | 33 |  | 188 | use constant DEFAULT_EPOINT => "api"; | 
|  | 33 |  |  |  |  | 60 |  | 
|  | 33 |  |  |  |  | 1646 |  | 
| 25 | 33 |  |  | 33 |  | 173 | use constant GRAPHQL_EPOINT => "https://api.upwork.com/graphql"; | 
|  | 33 |  |  |  |  | 59 |  | 
|  | 33 |  |  |  |  | 1436 |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 33 |  |  | 33 |  | 167 | use constant DATA_FORMAT  => "json"; | 
|  | 33 |  |  |  |  | 66 |  | 
|  | 33 |  |  |  |  | 1351 |  | 
| 28 | 33 |  |  | 33 |  | 180 | use constant OVERLOAD_VAR => "http_method"; | 
|  | 33 |  |  |  |  | 55 |  | 
|  | 33 |  |  |  |  | 2040 |  | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 33 |  |  | 33 |  | 178 | use constant URI_AUTH    => "/ab/account-security/oauth2/authorize"; | 
|  | 33 |  |  |  |  | 70 |  | 
|  | 33 |  |  |  |  | 1480 |  | 
| 31 | 33 |  |  | 33 |  | 187 | use constant URI_ATOKEN  => "/api/v3/oauth2/token"; | 
|  | 33 |  |  |  |  | 48 |  | 
|  | 33 |  |  |  |  | 1639 |  | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 33 |  |  | 33 |  | 199 | use constant ENTRY_POINT_API => "api"; | 
|  | 33 |  |  |  |  | 70 |  | 
|  | 33 |  |  |  |  | 1815 |  | 
| 34 | 33 |  |  | 33 |  | 557 | use constant ENTRY_POINT_GDS => "gds"; | 
|  | 33 |  |  |  |  | 102 |  | 
|  | 33 |  |  |  |  | 1951 |  | 
| 35 | 33 |  |  | 33 |  | 197 | use constant ENTRY_POINT_GQL => "graphql"; | 
|  | 33 |  |  |  |  | 76 |  | 
|  | 33 |  |  |  |  | 1767 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 33 |  |  | 33 |  | 182 | use constant UPWORK_LIBRARY_USER_AGENT => "Github Upwork API Perl Client"; | 
|  | 33 |  |  |  |  | 72 |  | 
|  | 33 |  |  |  |  | 30301 |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | our $lwp; | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | =pod | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =head1 NAME | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | Client | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =head1 FUNCTIONS | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =over 4 | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =item new($config) | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | Create a new Client | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | B | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | $config | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | Config object | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =cut | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub new { | 
| 64 | 0 |  |  | 0 | 1 |  | my $class = shift; | 
| 65 | 0 |  |  |  |  |  | my $config = shift; | 
| 66 | 0 |  |  |  |  |  | my %opts = @_; | 
| 67 | 0 |  |  |  |  |  | $opts{config} = $config; | 
| 68 | 0 |  |  |  |  |  | my $self = bless \%opts, $class; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | $self->get_oauth_client(); | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 |  |  |  |  |  | return $self; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =item get_oauth_client | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | Initialize OAuth client | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =cut | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub get_oauth_client { | 
| 82 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 |  |  |  |  |  | $lwp = LWP::UserAgent->new(); | 
| 85 | 0 |  |  |  |  |  | $lwp->agent(UPWORK_LIBRARY_USER_AGENT); | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | $self->{oauth_client} = Net::OAuth2::Profile::WebServer->new( | 
| 88 |  |  |  |  |  |  | client_id	   => $self->{config}{client_id}, | 
| 89 |  |  |  |  |  |  | client_secret	   => $self->{config}{client_secret}, | 
| 90 |  |  |  |  |  |  | access_token       => $self->{config}{access_token}, | 
| 91 |  |  |  |  |  |  | refresh_token      => $self->{config}{refresh_token}, | 
| 92 |  |  |  |  |  |  | expires_in         => $self->{config}{expires_in}, | 
| 93 |  |  |  |  |  |  | expires_at         => $self->{config}{expires_at}, | 
| 94 |  |  |  |  |  |  | site		   => BASE_HOST, | 
| 95 |  |  |  |  |  |  | authorize_path     => URI_AUTH, | 
| 96 |  |  |  |  |  |  | access_token_path  => URI_ATOKEN, | 
| 97 |  |  |  |  |  |  | refresh_token_path => URI_ATOKEN, | 
| 98 |  |  |  |  |  |  | redirect_uri       => $self->{config}{redirect_uri}, | 
| 99 | 0 |  |  |  |  |  | user_agent         => $lwp | 
| 100 |  |  |  |  |  |  | ); | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =item get | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | GET request to protected resource | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | B | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | $uri | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | Resource URL | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | $params | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | Hash of parameters | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | B | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | String | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =cut | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | sub get { | 
| 124 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 125 | 0 |  |  |  |  |  | my $uri = shift; | 
| 126 | 0 |  |  |  |  |  | my %params = @_; | 
| 127 |  |  |  |  |  |  |  | 
| 128 | 0 |  |  |  |  |  | return $self->send_request($uri, "GET", \%params); | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =item post | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | POST request to protected resource | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | B | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | $uri | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | Resource URL | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | $params | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | Hash of parameters | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | B | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | String | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =cut | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | sub post { | 
| 152 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 153 | 0 |  |  |  |  |  | my $uri = shift; | 
| 154 | 0 |  |  |  |  |  | my %params = @_; | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 0 |  |  |  |  |  | return $self->send_request($uri, "POST", \%params); | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | =item put | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | PUT request to protected resource | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | B | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | $uri | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | Resource URL | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | $params | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | Hash of parameters | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | B | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | String | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =cut | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | sub put { | 
| 180 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 181 | 0 |  |  |  |  |  | my $uri = shift; | 
| 182 | 0 |  |  |  |  |  | my %params = @_; | 
| 183 |  |  |  |  |  |  |  | 
| 184 | 0 |  |  |  |  |  | $params{&OVERLOAD_VAR} = 'put'; | 
| 185 |  |  |  |  |  |  |  | 
| 186 | 0 |  |  |  |  |  | return $self->send_request($uri, "POST", \%params); | 
| 187 |  |  |  |  |  |  | } | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | =item delete | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | DELETE request to protected resource | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | B | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | $uri | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | Resource URL | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | $params | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | Hash of parameters | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | B | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | String | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | =cut | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | sub delete { | 
| 210 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 211 | 0 |  |  |  |  |  | my $uri = shift; | 
| 212 | 0 |  |  |  |  |  | my %params = @_; | 
| 213 |  |  |  |  |  |  |  | 
| 214 | 0 |  |  |  |  |  | $params{&OVERLOAD_VAR} = 'delete'; | 
| 215 |  |  |  |  |  |  |  | 
| 216 | 0 |  |  |  |  |  | return $self->send_request($uri, "POST", \%params); | 
| 217 |  |  |  |  |  |  | } | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | =item send_request | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | Send a signed OAuth request to a specific protected resource | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | B | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | $uri | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | Resource URI | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | $method | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | Request method | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | $params | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | API parameters | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | B | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | String, a response content | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | =cut | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | sub send_request { | 
| 244 | 0 |  |  | 0 | 1 |  | my ($self, $uri, $method, $params) = @_; | 
| 245 |  |  |  |  |  |  |  | 
| 246 | 0 |  |  |  |  |  | my $_method = lc $method; | 
| 247 | 0 |  |  |  |  |  | my $response = $self->{access_token_session}->$_method($self->{oauth_client}->site_url(format_uri($uri, $self->{epoint}), %$params)); | 
| 248 |  |  |  |  |  |  |  | 
| 249 | 0 |  |  |  |  |  | return $response->decoded_content; | 
| 250 |  |  |  |  |  |  | } | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | =item graphql_request | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | Send a signed GraphQL request to a specific OAuth2 protected resource | 
| 255 |  |  |  |  |  |  |  | 
| 256 |  |  |  |  |  |  | B | 
| 257 |  |  |  |  |  |  |  | 
| 258 |  |  |  |  |  |  | $params | 
| 259 |  |  |  |  |  |  |  | 
| 260 |  |  |  |  |  |  | API parameters | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | B | 
| 263 |  |  |  |  |  |  |  | 
| 264 |  |  |  |  |  |  | String, a response content | 
| 265 |  |  |  |  |  |  |  | 
| 266 |  |  |  |  |  |  | =cut | 
| 267 |  |  |  |  |  |  |  | 
| 268 |  |  |  |  |  |  | sub graphql_request { | 
| 269 | 0 |  |  | 0 | 1 |  | my ($self, $json, $tenant_id) = @_; | 
| 270 |  |  |  |  |  |  |  | 
| 271 | 0 |  |  |  |  |  | my $req = HTTP::Request->new('POST', GRAPHQL_EPOINT); | 
| 272 |  |  |  |  |  |  | $req->header( | 
| 273 |  |  |  |  |  |  | 'Content-Type' => 'application/json', | 
| 274 | 0 |  |  |  |  |  | 'Authorization' => sprintf("%s %s", $self->{access_token_session}->token_type(), $self->{access_token_session}->access_token()) | 
| 275 |  |  |  |  |  |  | ); | 
| 276 | 0 | 0 |  |  |  |  | if (defined($tenant_id)) { | 
| 277 | 0 |  |  |  |  |  | $req->header('X-Upwork-API-TenantId' => $tenant_id); | 
| 278 |  |  |  |  |  |  | } | 
| 279 | 0 |  |  |  |  |  | $req->content($json); | 
| 280 |  |  |  |  |  |  |  | 
| 281 | 0 |  |  |  |  |  | my $res = $lwp->request($req); | 
| 282 |  |  |  |  |  |  |  | 
| 283 | 0 |  |  |  |  |  | return $res->decoded_content; | 
| 284 |  |  |  |  |  |  | } | 
| 285 |  |  |  |  |  |  |  | 
| 286 |  |  |  |  |  |  | =item format_uri | 
| 287 |  |  |  |  |  |  |  | 
| 288 |  |  |  |  |  |  | Create a path to a specific resource | 
| 289 |  |  |  |  |  |  |  | 
| 290 |  |  |  |  |  |  | B | 
| 291 |  |  |  |  |  |  |  | 
| 292 |  |  |  |  |  |  | $uri | 
| 293 |  |  |  |  |  |  |  | 
| 294 |  |  |  |  |  |  | URI to the protected resource | 
| 295 |  |  |  |  |  |  |  | 
| 296 |  |  |  |  |  |  | $epoint | 
| 297 |  |  |  |  |  |  |  | 
| 298 |  |  |  |  |  |  | Specific epoint | 
| 299 |  |  |  |  |  |  |  | 
| 300 |  |  |  |  |  |  | B | 
| 301 |  |  |  |  |  |  |  | 
| 302 |  |  |  |  |  |  | String | 
| 303 |  |  |  |  |  |  |  | 
| 304 |  |  |  |  |  |  | =cut | 
| 305 |  |  |  |  |  |  |  | 
| 306 |  |  |  |  |  |  | sub format_uri { | 
| 307 | 0 |  |  | 0 | 1 |  | my ($uri, $epoint) = @_; | 
| 308 | 0 |  | 0 |  |  |  | $epoint ||= DEFAULT_EPOINT; | 
| 309 |  |  |  |  |  |  |  | 
| 310 | 0 | 0 |  |  |  |  | return $epoint . $uri . (($epoint eq DEFAULT_EPOINT) ? "." . DATA_FORMAT : ""); | 
| 311 |  |  |  |  |  |  | } | 
| 312 |  |  |  |  |  |  |  | 
| 313 |  |  |  |  |  |  | =item version | 
| 314 |  |  |  |  |  |  |  | 
| 315 |  |  |  |  |  |  | Get version of native OAuth client | 
| 316 |  |  |  |  |  |  |  | 
| 317 |  |  |  |  |  |  | =cut | 
| 318 |  |  |  |  |  |  |  | 
| 319 |  |  |  |  |  |  | sub version { | 
| 320 | 0 |  |  | 0 | 1 |  | return $Net::OAuth::VERSION; | 
| 321 |  |  |  |  |  |  | } | 
| 322 |  |  |  |  |  |  |  | 
| 323 |  |  |  |  |  |  | =back | 
| 324 |  |  |  |  |  |  |  | 
| 325 |  |  |  |  |  |  | =head1 AUTHOR | 
| 326 |  |  |  |  |  |  |  | 
| 327 |  |  |  |  |  |  | Maksym Novozhylov C<<  >> | 
| 328 |  |  |  |  |  |  |  | 
| 329 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 330 |  |  |  |  |  |  |  | 
| 331 |  |  |  |  |  |  | Copyright E Upwork Global Corp., 2018 | 
| 332 |  |  |  |  |  |  |  | 
| 333 |  |  |  |  |  |  | =cut | 
| 334 |  |  |  |  |  |  |  | 
| 335 |  |  |  |  |  |  | 1; |