lib/Joker.pm
changeset 12 584ceb504d29
parent 11 7326d2e9b3a7
equal deleted inserted replaced
11:7326d2e9b3a7 12:584ceb504d29
     3 use Carp;
     3 use Carp;
     4 use Moose;
     4 use Moose;
     5 use MooseX::SemiAffordanceAccessor;
     5 use MooseX::SemiAffordanceAccessor;
     6 use LWP::UserAgent;
     6 use LWP::UserAgent;
     7 use Joker::Result;
     7 use Joker::Result;
       
     8 use if $ENV{DEBUG} => 'Smart::Comments';
     8 
     9 
     9 has [qw(username password)] => (
    10 has [qw(username password)] => (
    10     isa => 'Str',
    11     isa      => 'Str',
    11     is => 'ro',
    12     is       => 'ro',
    12     required => 1
    13     required => 1
    13 );
    14 );
    14 
    15 
    15 has uri => (
    16 has uri => (
    16     isa => 'Str',
    17     isa     => 'Str',
    17     is => 'ro',
    18     is      => 'ro',
    18     default => 'https://dmapi.joker.com/request/',
    19     default => 'https://dmapi.ote.joker.com/request/',    # default test api
       
    20     initializer => sub {    # append the trailing slash if missing
       
    21         my ($self, $value, $writer) = @_;
       
    22         return if $value =~ m{/$};
       
    23         $writer->("$value/");
       
    24     },
       
    25 );
       
    26 
       
    27 has id_file => (
       
    28     isa     => 'Str',
       
    29     is      => 'ro',
       
    30     default => $ENV{JOKER_ID_FILE} // "$ENV{HOME}/.joker-auth-id",
    19 );
    31 );
    20 
    32 
    21 has ua => (
    33 has ua => (
    22     isa => 'LWP::UserAgent',
    34     isa      => 'LWP::UserAgent',
    23     is => 'ro',
    35     is       => 'ro',
    24     default => sub { LWP::UserAgent->new },
    36     init_arg => undef,
       
    37     default  => sub { LWP::UserAgent->new },
    25 );
    38 );
    26 
    39 
    27 has auth_id => (
    40 has _auth_id => (
    28     isa => 'Str',
    41     isa      => 'Str',
    29     is => 'ro',
    42     is       => 'ro',
    30     lazy => 1,
    43     init_arg => undef,
    31     builder => 'login',
    44     lazy     => 1,
       
    45     builder  => '_build_auth_id',
    32 );
    46 );
       
    47 
       
    48 has last_response => (
       
    49     is       => 'ro',
       
    50     isa      => 'Joker::Response',
       
    51     init_arg => undef,
       
    52     writer   => '_set_last_response',
       
    53 );
       
    54 
       
    55 sub _init_uri {
       
    56     die "@_";
       
    57 }
    33 
    58 
    34 sub request {
    59 sub request {
    35     my ($self, $type) = (shift, shift);
    60     my ($self, $type) = (shift, shift);
    36     my %parm = (
    61     my %parm = (
    37 	'Auth-Sid', $self->auth_id,
    62         'Auth-Sid' => $self->_auth_id,
    38 	@_,
    63         @_,
    39     );
    64     );
    40     my $req = HTTP::Request->new(
    65     my $req = HTTP::Request->new(
    41 	GET => $self->uri . "$type?" .  join '&', map { "$_=$parm{$_}" } keys %parm);
    66         GET => $self->uri . "$type?" . join '&',
       
    67         map { "$_=$parm{$_}" } keys %parm
       
    68     );
    42     my $result = $self->ua->request($req);
    69     my $result = $self->ua->request($req);
       
    70     ### $result
    43     croak $result->status_line if not $result->is_success;
    71     croak $result->status_line if not $result->is_success;
    44     return Joker::Result->new(response => $result->content);
    72     return Joker::Result->new(response => $result->content);
    45 }
    73 }
    46 
    74 
    47 sub login {
    75 sub _login {
    48     my $self = shift;
    76     my $self = shift;
    49     my $req = HTTP::Request->new(
    77     my $req =
    50 	GET => $self->uri . 'login?'
    78       HTTP::Request->new(GET => $self->uri
    51 	    . 'username=' . $self->username . '&'
    79           . 'login?'
    52 	    . 'password=' . $self->password);
    80           . 'username='
       
    81           . $self->username . '&'
       
    82           . 'password='
       
    83           . $self->password);
    53     my $result = $self->ua->request($req);
    84     my $result = $self->ua->request($req);
    54 
    85 
    55     croak $result->status_line if not $result->is_success;
    86     croak $result->status_line if not $result->is_success;
    56     return $1 if $result->content =~ /^Auth-Sid:\s+(\S+)/m;
    87     return $1 if $result->content =~ /^Auth-Sid:\s+(\S+)/m;
    57 
    88 
    58     croak q{Can't get Auth-Sid};
    89     croak q{Can't get Auth-Sid};
    59 }
    90 }
    60 
    91 
       
    92 sub _build_auth_id {
       
    93     my $self = shift;
       
    94     open(my $f, '<', "$ENV{HOME}/.joker.auth-id") or return '';
       
    95     chomp(my $_ = <$f>);
       
    96     return $_ // '';
       
    97 }
       
    98 
    61 __PACKAGE__->meta->make_immutable;
    99 __PACKAGE__->meta->make_immutable;
       
   100 
       
   101 __END__
       
   102 
       
   103 =head1 NAME
       
   104 
       
   105 Joker - simple OO interface to the Joker domain registry
       
   106 
       
   107 =head1 SYNOPSIS
       
   108 
       
   109  use Joker;
       
   110 
       
   111 =head1 DESCRIPTION
       
   112 
       
   113 =head1 CONSTRUCTOR
       
   114 
       
   115 The B<new> method serves as a constructor. It requires a hash with the
       
   116 following attributes:
       
   117 
       
   118 =over
       
   119 
       
   120 =item B<username> = I<username>
       
   121 
       
   122 The username, that is your Joker.com account.
       
   123 
       
   124 =item B<password> = I<password>
       
   125 
       
   126 The password for the account above.
       
   127 
       
   128 =back
       
   129 
       
   130 Optional attributes are:
       
   131 
       
   132 =over
       
   133 
       
   134 =item B<ur> = I<uri of the DMAP server>
       
   135 
       
   136 The URI where the requests are sent to. (default:
       
   137 https://dmapi.ote.joker.com/) For production use remove the "ote" from
       
   138 the URI.
       
   139 
       
   140 =item B<id_file> = I<id file>
       
   141 
       
   142 The temporary store for the joker auth-id. The first login returns a
       
   143 joker auth-id (cookie). This cookie may be reused on further requests
       
   144 until it expires. (default: C<$ENV{JOKER_AUTH_ID_FILE}> //
       
   145 C<$ENV{HOME}/.joker-auth-id>)
       
   146 
       
   147 =back
       
   148 
       
   149 =head1 METHODS
       
   150 
       
   151 =head2 B<request>(I<request>, I<request params>)
       
   152 
       
   153 Send a request to the DMAPI. Returns a L<Joker::Result> object. Please
       
   154 refer to the L<DMAPI specs|https://dmapi.joker.com/docs/DMAPI-ext.txt>.
       
   155 
       
   156 =cut