| 1 | package Net::Plurk::OAuth; | ||
| 2 | 0 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | use Net::OAuth; | ||
| 4 | use AnyEvent::HTTP; | ||
| 5 | use Digest::MD5 'md5_hex'; | ||
| 6 | $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A; | ||
| 7 | |||
| 8 | use namespace::autoclean; | ||
| 9 | has consumer_key => ( isa => 'Str', is => 'ro', required => 1); | ||
| 10 | has consumer_secret => ( isa => 'Str', is => 'ro', required => 1); | ||
| 11 | has access_token => ( isa => 'Str', is => 'rw', default => ''); | ||
| 12 | has access_token_secret => ( isa => 'Str', is => 'rw', default => ''); | ||
| 13 | has _errormsg => ( isa => 'Maybe[Str]', is => 'rw'); | ||
| 14 | has _errorcode => ( isa => 'Int', is => 'rw', default => 0); | ||
| 15 | has base_url => ( isa => 'Str', is => 'ro', default => 'http://www.plurk.com'); | ||
| 16 | has request_url => ( isa => 'Str', is => 'rw'); | ||
| 17 | has request_token_path => ( isa => 'Str', | ||
| 18 | is => 'ro', default => '/OAuth/request_token'); | ||
| 19 | has authorization_path => ( isa => 'Str', | ||
| 20 | is => 'ro', default => '/OAuth/authoriz'); | ||
| 21 | has access_token_path => ( isa => 'Str', | ||
| 22 | is => 'ro', default => '/OAuth/access_token'); | ||
| 23 | has request_method => ( isa => 'Str', is => 'rw', default => 'POST'); | ||
| 24 | has signature_method => ( isa => 'Str', is => 'ro', default => 'HMAC-SHA1'); | ||
| 25 | has _request => (isa => 'Net::OAuth::Request', is => 'rw'); | ||
| 26 | has json_parser => (isa => 'JSON::Any', is => 'ro', default => sub {JSON::Any->new()}); | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Net::Plurk::OAuth | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | Access Plurk OAuth API via Net::OAuth | ||
| 35 | |||
| 36 | =head2 _make_request | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub _make_request { | ||
| 41 | my ($self, $request_url, %args) = @_; | ||
| 42 | $args{message_type} //= 'protected resource'; | ||
| 43 | $self->_request( | ||
| 44 | Net::OAuth->request(delete $args{message_type})->new( | ||
| 45 | consumer_key => $self->consumer_key, | ||
| 46 | consumer_secret => $self->consumer_secret, | ||
| 47 | token => $self->access_token, | ||
| 48 | token_secret => $self->access_token_secret, | ||
| 49 | request_url => $request_url, | ||
| 50 | request_method => $self->request_method, | ||
| 51 | signature_method => $self->signature_method, | ||
| 52 | timestamp => time, | ||
| 53 | nonce => md5_hex(time() * rand()), | ||
| 54 | callback => (delete $args{callback}), | ||
| 55 | extra_params => { %args }, | ||
| 56 | ) | ||
| 57 | ); | ||
| 58 | $self->_request->sign; | ||
| 59 | } | ||
| 60 | |||
| 61 | =head2 authorize | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub authorize { | ||
| 66 | my ($self, %args) = @_; | ||
| 67 | $self->access_token($args{access_token}); | ||
| 68 | $self->access_token_secret($args{access_token_secret}); | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 request | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub request { | ||
| 76 | my ($self, $path, %args) = @_; | ||
| 77 | my ($header, $data); | ||
| 78 | my $request_url = $self->base_url.$path; | ||
| 79 | $self->_make_request($request_url, %args); | ||
| 80 | |||
| 81 | my $w = AE::cv; | ||
| 82 | http_post ($self->_request->to_url, | ||
| 83 | sub { | ||
| 84 | ($data, $header) = @_; | ||
| 85 | $self->_errormsg(undef); # clear errormsg | ||
| 86 | $self->_errorcode(0); # clear errorcode | ||
| 87 | if ($header->{Status} ne '200') { | ||
| 88 | $self->_errormsg($header->{Reason}); | ||
| 89 | $self->_errorcode($header->{Status}); | ||
| 90 | } | ||
| 91 | $data = $self->json_parser->from_json($data); | ||
| 92 | $w->send; | ||
| 93 | } | ||
| 94 | ); | ||
| 95 | $w->recv; | ||
| 96 | return wantarray ? ($data, $header) : $data; | ||
| 97 | } | ||
| 98 | |||
| 99 | =head2 get_request_token | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub get_request_token { | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | no Moose; | ||
| 108 | 1; | ||