| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Google::Contacts::Server; | 
| 2 |  |  |  |  |  |  | { | 
| 3 |  |  |  |  |  |  | $WWW::Google::Contacts::Server::VERSION = '0.39'; | 
| 4 |  |  |  |  |  |  | } | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 17 |  |  | 17 |  | 99 | use Moose; | 
|  | 17 |  |  |  |  | 37 |  | 
|  | 17 |  |  |  |  | 178 |  | 
| 7 | 17 |  |  | 17 |  | 129557 | use LWP::UserAgent; | 
|  | 17 |  |  |  |  | 838273 |  | 
|  | 17 |  |  |  |  | 645 |  | 
| 8 | 17 |  |  | 17 |  | 14269 | use Net::Google::AuthSub; | 
|  | 17 |  |  |  |  | 125391 |  | 
|  | 17 |  |  |  |  | 644 |  | 
| 9 | 17 |  |  | 17 |  | 129 | use Carp qw( croak ); | 
|  | 17 |  |  |  |  | 39 |  | 
|  | 17 |  |  |  |  | 10486 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | has ua => ( | 
| 12 |  |  |  |  |  |  | is      => 'ro', | 
| 13 |  |  |  |  |  |  | default => sub { LWP::UserAgent->new }, | 
| 14 |  |  |  |  |  |  | ); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | has authsub => ( | 
| 17 |  |  |  |  |  |  | is         => 'ro', | 
| 18 |  |  |  |  |  |  | lazy_build => 1, | 
| 19 |  |  |  |  |  |  | ); | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | has username => ( | 
| 22 |  |  |  |  |  |  | isa      => 'Str', | 
| 23 |  |  |  |  |  |  | is       => 'ro', | 
| 24 |  |  |  |  |  |  | required => 1, | 
| 25 |  |  |  |  |  |  | ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | has password => ( | 
| 28 |  |  |  |  |  |  | isa      => 'Str', | 
| 29 |  |  |  |  |  |  | is       => 'ro', | 
| 30 |  |  |  |  |  |  | required => 1, | 
| 31 |  |  |  |  |  |  | ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | has protocol => ( | 
| 34 |  |  |  |  |  |  | isa     => 'Str', | 
| 35 |  |  |  |  |  |  | is      => 'ro', | 
| 36 |  |  |  |  |  |  | default => 'http', | 
| 37 |  |  |  |  |  |  | ); | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | has gdata_version => ( | 
| 40 |  |  |  |  |  |  | isa     => 'Str', | 
| 41 |  |  |  |  |  |  | is      => 'ro', | 
| 42 |  |  |  |  |  |  | default => '3.0', | 
| 43 |  |  |  |  |  |  | ); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub _build_authsub { | 
| 46 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 |  |  |  |  |  | my $auth = Net::Google::AuthSub->new( service => 'cp' ); | 
| 49 | 0 |  |  |  |  |  | my $res = $auth->login( $self->username, $self->password ); | 
| 50 | 0 | 0 | 0 |  |  |  | unless ( $res and $res->is_success ) { | 
| 51 | 0 | 0 |  |  |  |  | if ($res) { | 
| 52 | 0 |  |  |  |  |  | warn $res->{_response}->content; | 
| 53 |  |  |  |  |  |  | } | 
| 54 | 0 |  |  |  |  |  | croak "Authentication failed"; | 
| 55 |  |  |  |  |  |  | } | 
| 56 | 0 |  |  |  |  |  | return $auth; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub authenticate { | 
| 60 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 61 | 0 | 0 |  |  |  |  | return 1 if ( $self->authsub ); | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | foreach my $method (qw(get post put delete)) { | 
| 65 |  |  |  |  |  |  | around $method => sub { | 
| 66 |  |  |  |  |  |  | my $orig = shift; | 
| 67 |  |  |  |  |  |  | my $self = shift; | 
| 68 |  |  |  |  |  |  | my $id   = shift; | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | # ENSURE we use right protocol.. Even though we've specified https, | 
| 71 |  |  |  |  |  |  | # references in the data from google points to http:// | 
| 72 |  |  |  |  |  |  | my $proto = $self->protocol; | 
| 73 |  |  |  |  |  |  | $id =~ s{^\w+://}{$proto://}; | 
| 74 |  |  |  |  |  |  | return $self->$orig( $id, @_ ); | 
| 75 |  |  |  |  |  |  | }; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub get { | 
| 79 |  |  |  |  |  |  | my ( $self, $id ) = @_; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | my %headers = $self->authsub->auth_params; | 
| 82 |  |  |  |  |  |  | $headers{'GData-Version'} = $self->gdata_version; | 
| 83 |  |  |  |  |  |  | my $res = $self->ua->get( $id, %headers ); | 
| 84 |  |  |  |  |  |  | unless ( $res->is_success ) { | 
| 85 |  |  |  |  |  |  | croak "GET failed: " . $res->status_line; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  | return $res; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub post { | 
| 91 |  |  |  |  |  |  | my ( $self, $id, $etag, $content_type, $content ) = @_; | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | my %headers = $self->authsub->auth_params; | 
| 94 |  |  |  |  |  |  | $headers{'Content-Type'}  = $content_type; | 
| 95 |  |  |  |  |  |  | $headers{'GData-Version'} = $self->gdata_version; | 
| 96 |  |  |  |  |  |  | my $res = $self->ua->post( $id, %headers, Content => $content ); | 
| 97 |  |  |  |  |  |  | unless ( $res->is_success ) { | 
| 98 | 17 |  |  | 17 |  | 14469 | use Data::Dumper; | 
|  | 17 |  |  |  |  | 104418 |  | 
|  | 17 |  |  |  |  | 3403 |  | 
| 99 |  |  |  |  |  |  | print Dumper $res; | 
| 100 |  |  |  |  |  |  | croak "POST failed: " . $res->status_line; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  | return $res; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub put { | 
| 106 |  |  |  |  |  |  | my ( $self, $id, $etag, $content_type, $content ) = @_; | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | my %headers = $self->authsub->auth_params; | 
| 109 |  |  |  |  |  |  | $headers{'Content-Type'}           = $content_type; | 
| 110 |  |  |  |  |  |  | $headers{'GData-Version'}          = $self->gdata_version; | 
| 111 |  |  |  |  |  |  | $headers{'If-Match'}               = $etag; | 
| 112 |  |  |  |  |  |  | $headers{'X-HTTP-Method-Override'} = 'PUT'; | 
| 113 |  |  |  |  |  |  | my $res = $self->ua->post( $id, %headers, Content => $content ); | 
| 114 |  |  |  |  |  |  | unless ( $res->is_success ) { | 
| 115 | 17 |  |  | 17 |  | 180 | use Data::Dumper; | 
|  | 17 |  |  |  |  | 34 |  | 
|  | 17 |  |  |  |  | 3428 |  | 
| 116 |  |  |  |  |  |  | print Dumper $res; | 
| 117 |  |  |  |  |  |  | croak "PUT failed: " . $res->status_line; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  | return $res; | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | sub delete { | 
| 123 |  |  |  |  |  |  | my ( $self, $id, $etag ) = @_; | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | my %headers = $self->authsub->auth_params; | 
| 126 |  |  |  |  |  |  | $headers{'If-Match'}               = $etag; | 
| 127 |  |  |  |  |  |  | $headers{'X-HTTP-Method-Override'} = 'DELETE'; | 
| 128 |  |  |  |  |  |  | $headers{'GData-Version'}          = $self->gdata_version; | 
| 129 |  |  |  |  |  |  | my $res = $self->ua->post( $id, %headers ); | 
| 130 |  |  |  |  |  |  | unless ( $res->is_success ) { | 
| 131 |  |  |  |  |  |  | croak "DELETE failed: " . $res->status_line; | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  | return $res; | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 17 |  |  | 17 |  | 110 | no Moose; | 
|  | 17 |  |  |  |  | 36 |  | 
|  | 17 |  |  |  |  | 353 |  | 
| 137 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 138 |  |  |  |  |  |  | 1; | 
| 139 |  |  |  |  |  |  | __END__ |