| 1 | use MooseX::Declare; | ||
| 2 | |||
| 3 | class WWW::Getsy::Client { | ||
| 4 | use Net::OAuth; | ||
| 5 | use MooseX::Types::Moose qw/Str HashRef/; | ||
| 6 | |||
| 7 | has 'tokens' => ( | ||
| 8 | isa => HashRef, | ||
| 9 | is => 'rw', | ||
| 10 | default => undef, | ||
| 11 | ); | ||
| 12 | |||
| 13 | has 'request_token' => ( | ||
| 14 | isa => Str, | ||
| 15 | is => 'rw', | ||
| 16 | default => undef, | ||
| 17 | ); | ||
| 18 | |||
| 19 | has 'authorization_url' => ( | ||
| 20 | isa => Str, | ||
| 21 | is => 'ro', | ||
| 22 | default => "http://www.etsy.com/oauth/signin", | ||
| 23 | ); | ||
| 24 | |||
| 25 | has 'request_token_url' => ( | ||
| 26 | isa => Str, | ||
| 27 | is => 'ro', | ||
| 28 | default => "http://openapi.etsy.com/v2/oauth/request_token", | ||
| 29 | ); | ||
| 30 | |||
| 31 | has 'access_token_url' => ( | ||
| 32 | isa => Str, | ||
| 33 | is => 'ro', | ||
| 34 | default => "http://openapi.etsy.com/v2/oauth/access_token", | ||
| 35 | ); | ||
| 36 | |||
| 37 | method authorized { | ||
| 38 | foreach my $param ( qw(access_token access_token_secret) ) { | ||
| 39 | return 0 unless defined $self->tokens->{$param} && length $self->tokens->{$param}; | ||
| 40 | } | ||
| 41 | return 1; | ||
| 42 | } | ||
| 43 | |||
| 44 | method get_authorization_url(Str :$callback) { | ||
| 45 | # my $self = shift; | ||
| 46 | # my %params = @_; | ||
| 47 | my $url = $self->authorization_url; | ||
| 48 | if (!defined $self->request_token) { | ||
| 49 | $self->request_request_token(%params); | ||
| 50 | } | ||
| 51 | $params{oauth_token} = $self->request_token; | ||
| 52 | $url->query_form(%params); | ||
| 53 | return $url; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub request_request_token { | ||
| 57 | my $self = shift; | ||
| 58 | my %params = @_; | ||
| 59 | my $url = $self->request_token_url; | ||
| 60 | |||
| 61 | $params{callback} = $self->callback unless defined $params{callback}; | ||
| 62 | die "You must pass a callback parameter when using OAuth v1.0a" unless defined $params{callback}; | ||
| 63 | |||
| 64 | my $request_token_response = $self->_make_request( | ||
| 65 | 'Net::OAuth::RequestTokenRequest', | ||
| 66 | $url, 'GET', | ||
| 67 | %params); | ||
| 68 | |||
| 69 | die "GET for $url failed: ".$request_token_response->status_line | ||
| 70 | unless ( $request_token_response->is_success ); | ||
| 71 | |||
| 72 | # Cast response into CGI query for EZ parameter decoding | ||
| 73 | my $request_token_response_query = | ||
| 74 | new CGI( $request_token_response->content ); | ||
| 75 | |||
| 76 | # Split out token and secret parameters from the request token response | ||
| 77 | $self->request_token($request_token_response_query->param('oauth_token')); | ||
| 78 | $self->request_token_secret($request_token_response_query->param('oauth_token_secret')); | ||
| 79 | $self->callback_confirmed($request_token_response_query->param('oauth_callback_confirmed')); | ||
| 80 | |||
| 81 | die "Response does not confirm to OAuth1.0a. oauth_callback_confirmed not received" | ||
| 82 | if $self->oauth_1_0a && !$self->callback_confirmed; | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | method load_tokens(Str $file) { | ||
| 87 | my %tokens = (); | ||
| 88 | return %tokens unless -f $file; | ||
| 89 | |||
| 90 | open(my $fh, $file) || die "Couldn't open $file: $!\n"; | ||
| 91 | while (<$fh>) { | ||
| 92 | chomp; | ||
| 93 | next if /^#/; | ||
| 94 | next if /^\s*$/; | ||
| 95 | next unless /=/; | ||
| 96 | s/(^\s*|\s*$)//g; | ||
| 97 | my ($key, $val) = split /\s*=\s*/, $_, 2; | ||
| 98 | $tokens{$key} = $val; | ||
| 99 | } | ||
| 100 | close($fh); | ||
| 101 | return %tokens; | ||
| 102 | } | ||
| 103 | |||
| 104 | method save_tokens(Str $file) { | ||
| 105 | my %tokens = @_; | ||
| 106 | |||
| 107 | my $max = 0; | ||
| 108 | foreach my $key (keys %tokens) { | ||
| 109 | $max = length($key) if length($key)>$max; | ||
| 110 | } | ||
| 111 | |||
| 112 | open(my $fh, ">$file") || die "Couldn't open $file for writing: $!\n"; | ||
| 113 | foreach my $key (sort keys %tokens) { | ||
| 114 | my $pad = " "x($max-length($key)); | ||
| 115 | print $fh "$key ${pad}= ".$tokens{$key}."\n"; | ||
| 116 | } | ||
| 117 | close($fh); | ||
| 118 | } | ||
| 119 | |||
| 120 | } |