| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #  Copyright 2013 Digital River, Inc. | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | #  Licensed under the Apache License, Version 2.0 (the "License"); | 
| 4 |  |  |  |  |  |  | #  you may not use this file except in compliance with the License. | 
| 5 |  |  |  |  |  |  | #  You may obtain a copy of the License at | 
| 6 |  |  |  |  |  |  | # | 
| 7 |  |  |  |  |  |  | #      http://www.apache.org/licenses/LICENSE-2.0 | 
| 8 |  |  |  |  |  |  | # | 
| 9 |  |  |  |  |  |  | #  Unless required by applicable law or agreed to in writing, software | 
| 10 |  |  |  |  |  |  | #  distributed under the License is distributed on an "AS IS" BASIS, | 
| 11 |  |  |  |  |  |  | #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 12 |  |  |  |  |  |  | #  See the License for the specific language governing permissions and | 
| 13 |  |  |  |  |  |  | #  limitations under the License. | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | package Net::MyCommerce::API::Resource; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 2 |  |  | 2 |  | 53683 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 72 |  | 
| 18 | 2 |  |  | 2 |  | 14 | use warnings; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 51 |  | 
| 19 | 2 |  |  | 2 |  | 1304 | use Net::MyCommerce::API::Client; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 83 |  | 
| 20 | 2 |  |  | 2 |  | 1401 | use Net::MyCommerce::API::Token; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 624 |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | =pod | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | =head1 NAME | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | Net::MyCommerce::API::Resource | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =head1 VERSION | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | version 1.0.1 | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =cut | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | our $VERSION = '1.0.1'; | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | =head1 METHODS | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | =head2 new (%args) | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | Create a new resource object. | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | $args{timeout}: timeout in seconds | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =cut | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub new { | 
| 47 | 1 |  |  | 1 | 1 | 20 | my ($pkg, %args) = @_; | 
| 48 | 1 |  | 50 |  |  | 35 | $args{client} = Net::MyCommerce::API::Client->new( | 
|  |  |  | 50 |  |  |  |  | 
|  |  |  | 50 |  |  |  |  | 
| 49 |  |  |  |  |  |  | host     => $args{host} || 'http://api.mycommerce.com', | 
| 50 |  |  |  |  |  |  | prefix   => $args{prefix} || '/api/v1', | 
| 51 |  |  |  |  |  |  | timeout  => $args{timeout} || 60, | 
| 52 |  |  |  |  |  |  | sendJSON => 1, | 
| 53 |  |  |  |  |  |  | getJSON  => 1, | 
| 54 |  |  |  |  |  |  | ); | 
| 55 | 1 |  |  |  |  | 15 | $args{token} = Net::MyCommerce::API::Token->new(%args); | 
| 56 | 1 |  |  |  |  | 16 | return bless { %args }, $pkg; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =head2 request (%opts) | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | Generic resource request call. | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | my $resource = Net::MyCommerce::API::Resource->new(); | 
| 64 |  |  |  |  |  |  | my ($error, $result) = $resource->request( path => $path, params => $params ); | 
| 65 |  |  |  |  |  |  | if ($error) { | 
| 66 |  |  |  |  |  |  | print "Error occurred: $error\n"; | 
| 67 |  |  |  |  |  |  | } else { | 
| 68 |  |  |  |  |  |  | my $content = $result->{content}; | 
| 69 |  |  |  |  |  |  | my $status_code = $result->{status_code}; | 
| 70 |  |  |  |  |  |  | my $headers => $result->{headers}; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =cut | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | sub request { | 
| 76 | 1 |  |  | 1 | 1 | 7 | my ($self, %opts) = @_; | 
| 77 | 1 |  |  |  |  | 9 | my ($terror, $token) = $self->{token}->lookup(); | 
| 78 | 1 | 50 |  |  |  | 7 | if ($terror) { | 
| 79 | 1 |  |  |  |  | 10 | $self->{token}->reset(); | 
| 80 | 1 |  |  |  |  | 9 | return ($terror, {}); | 
| 81 |  |  |  |  |  |  | } | 
| 82 | 0 |  |  |  |  |  | $opts{token_id} = $token->{id}; | 
| 83 | 0 |  |  |  |  |  | my ($error, $result) = $self->{client}->request(%opts); | 
| 84 | 0 |  | 0 |  |  |  | $result ||= {}; | 
| 85 | 0 |  | 0 |  |  |  | $result->{content} ||= {}; | 
| 86 | 0 |  | 0 |  |  |  | $result->{status_code} ||= 0; | 
| 87 | 0 | 0 | 0 |  |  |  | if ($result->{content}{error} && $result->{content}{error} eq 'invalid_token') { | 
| 88 | 0 |  |  |  |  |  | $self->{token}->reset(); | 
| 89 | 0 |  |  |  |  |  | $error = 'token no longer valid'; | 
| 90 |  |  |  |  |  |  | } | 
| 91 | 0 |  |  |  |  |  | return ($error, $result); | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | 1; |