« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/SocialMedia/Facebook.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/SocialMedia/Line.pm »
| 1 | package Dwarf::Module::SocialMedia::Amazon; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent 'Dwarf::Module'; | ||
| 4 | use Dwarf::HTTP::Async; | ||
| 5 | use DateTime; | ||
| 6 | use DateTime::Format::HTTP; | ||
| 7 | use HTTP::Request::Common (); | ||
| 8 | use JSON; | ||
| 9 | use LWP::UserAgent; | ||
| 10 | use Data::Dumper; | ||
| 11 | |||
| 12 | use Dwarf::Accessor qw/ | ||
| 13 | ua urls | ||
| 14 | key secret | ||
| 15 | access_token | ||
| 16 | user_id name profile_image | ||
| 17 | on_error | ||
| 18 | /; | ||
| 19 | |||
| 20 | sub init { | ||
| 21 | my $self = shift; | ||
| 22 | |||
| 23 | $self->{ua} ||= LWP::UserAgent->new; | ||
| 24 | |||
| 25 | $self->{urls} ||= { | ||
| 26 | api => 'https://api.amazon.com', | ||
| 27 | authorization => 'https://www.amazon.com/ap/oa', | ||
| 28 | access_token => 'https://api.amazon.com/auth/o2/token', | ||
| 29 | }; | ||
| 30 | |||
| 31 | $self->{on_error} ||= sub { die @_ }; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub authorized { | ||
| 35 | my ($self, $will_die) = @_; | ||
| 36 | $will_die ||= 1; | ||
| 37 | my $authorized = defined $self->access_token; | ||
| 38 | if ($will_die && !$authorized) { | ||
| 39 | $self->on_error("Unauthorized"); | ||
| 40 | } | ||
| 41 | return $authorized; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub show_user { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | die 'access token must be specified.' unless defined $self->access_token; | ||
| 48 | |||
| 49 | return $self->call('user/profile', 'GET'); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub get_authorization_url { | ||
| 53 | my ($self, %params) = @_; | ||
| 54 | |||
| 55 | die 'key must be specified.' unless defined $self->key; | ||
| 56 | die 'secret must be specified.' unless defined $self->secret; | ||
| 57 | die "redirect_uri must be specified." unless defined $params{redirect_uri}; | ||
| 58 | |||
| 59 | $params{response_type} = "code"; | ||
| 60 | $params{client_id} ||= $self->key; | ||
| 61 | # [todo] | ||
| 62 | # add state parameters. | ||
| 63 | # https://images-na.ssl-images-amazon.com/images/G/01/lwa/dev/docs/website-developer-guide._TTH_.pdf | ||
| 64 | |||
| 65 | my $uri = URI->new($self->urls->{authorization}); | ||
| 66 | $uri->query_form(%params); | ||
| 67 | return $uri; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub request_access_token { | ||
| 71 | my ($self, %params) = @_; | ||
| 72 | |||
| 73 | die 'key must be specified.' unless defined $self->key; | ||
| 74 | die 'secret must be specified.' unless defined $self->secret; | ||
| 75 | die "redirect_uri must be specified." unless defined $params{redirect_uri}; | ||
| 76 | die "code must be specified." unless defined $params{code}; | ||
| 77 | |||
| 78 | $params{grant_type} = "authorization_code"; | ||
| 79 | $params{client_id} ||= $self->key; | ||
| 80 | $params{client_secret} ||= $self->secret; | ||
| 81 | |||
| 82 | # [memo] the following code doesn't work! | ||
| 83 | # my $uri = URI->new($self->urls->{access_token}); | ||
| 84 | # $uri->query_form(%params); | ||
| 85 | # | ||
| 86 | # my $res = $self->ua->post($uri); | ||
| 87 | |||
| 88 | my $res = $self->ua->post($self->urls->{access_token}, \%params); | ||
| 89 | |||
| 90 | if ($res->code !~ /^2/) { | ||
| 91 | $self->on_error->('Amazon OAuth Error: Could not get access token.'); | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | my $decoded = decode_json $res->{_content}; | ||
| 96 | my $access_token = $decoded->{access_token}; | ||
| 97 | |||
| 98 | $self->access_token($access_token); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _make_request { | ||
| 102 | my ($self, $command, $method, $params) = @_; | ||
| 103 | |||
| 104 | $method = uc $method; | ||
| 105 | |||
| 106 | my $uri = URI->new($self->urls->{api} . '/' . $command); | ||
| 107 | $uri->query_form(%{ $params }) if $method =~ /^(GET|DELETE)$/; | ||
| 108 | |||
| 109 | my %data = %{ $params }; | ||
| 110 | |||
| 111 | if ($method eq 'MULTIPART_POST') { | ||
| 112 | $method = 'POST'; | ||
| 113 | my $source = $params->{source}; | ||
| 114 | delete $params->{source}; | ||
| 115 | $uri->query_form(%{ $params }); | ||
| 116 | %data = ( | ||
| 117 | Content_Type => 'multipart/form-data', | ||
| 118 | Content => [ source => $source ], | ||
| 119 | ); | ||
| 120 | } elsif ($method eq 'POST') { | ||
| 121 | %data = (Content => $params) | ||
| 122 | } | ||
| 123 | |||
| 124 | no strict 'refs'; | ||
| 125 | my $req = &{"HTTP::Request::Common::$method"}($uri, %data); | ||
| 126 | $req->header("Content-Length", 0) if $method eq 'DELETE'; | ||
| 127 | $req->header(Authorization => "Bearer " . $self->access_token); | ||
| 128 | |||
| 129 | return $req; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub call { | ||
| 133 | my ($self, $command, $method, $params) = @_; | ||
| 134 | $self->authorized; | ||
| 135 | my $req = $self->_make_request($command, $method, $params); | ||
| 136 | my $res = $self->ua->request($req); | ||
| 137 | return $self->validate($res); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub validate { | ||
| 141 | my ($self, $res) = @_; | ||
| 142 | my $content = eval { decode_json($res->decoded_content) }; | ||
| 143 | if ($@) { | ||
| 144 | warn "Couldn't decode JSON: $@"; | ||
| 145 | $content = $res->decoded_content; | ||
| 146 | } | ||
| 147 | |||
| 148 | # [todo] | ||
| 149 | # check error status codes | ||
| 150 | |||
| 151 | return $content; | ||
| 152 | } | ||
| 153 | |||
| 154 | 1; |