lib/Joker.pm
changeset 13 4e431e2de91a
parent 12 584ceb504d29
equal deleted inserted replaced
12:584ceb504d29 13:4e431e2de91a
     1 package Joker;
       
     2 
       
     3 use Carp;
       
     4 use Moose;
       
     5 use MooseX::SemiAffordanceAccessor;
       
     6 use LWP::UserAgent;
       
     7 use Joker::Result;
       
     8 use if $ENV{DEBUG} => 'Smart::Comments';
       
     9 
       
    10 has [qw(username password)] => (
       
    11     isa      => 'Str',
       
    12     is       => 'ro',
       
    13     required => 1
       
    14 );
       
    15 
       
    16 has uri => (
       
    17     isa     => 'Str',
       
    18     is      => 'ro',
       
    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",
       
    31 );
       
    32 
       
    33 has ua => (
       
    34     isa      => 'LWP::UserAgent',
       
    35     is       => 'ro',
       
    36     init_arg => undef,
       
    37     default  => sub { LWP::UserAgent->new },
       
    38 );
       
    39 
       
    40 has _auth_id => (
       
    41     isa      => 'Str',
       
    42     is       => 'ro',
       
    43     init_arg => undef,
       
    44     lazy     => 1,
       
    45     builder  => '_build_auth_id',
       
    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 }
       
    58 
       
    59 sub request {
       
    60     my ($self, $type) = (shift, shift);
       
    61     my %parm = (
       
    62         'Auth-Sid' => $self->_auth_id,
       
    63         @_,
       
    64     );
       
    65     my $req = HTTP::Request->new(
       
    66         GET => $self->uri . "$type?" . join '&',
       
    67         map { "$_=$parm{$_}" } keys %parm
       
    68     );
       
    69     my $result = $self->ua->request($req);
       
    70     ### $result
       
    71     croak $result->status_line if not $result->is_success;
       
    72     return Joker::Result->new(response => $result->content);
       
    73 }
       
    74 
       
    75 sub _login {
       
    76     my $self = shift;
       
    77     my $req =
       
    78       HTTP::Request->new(GET => $self->uri
       
    79           . 'login?'
       
    80           . 'username='
       
    81           . $self->username . '&'
       
    82           . 'password='
       
    83           . $self->password);
       
    84     my $result = $self->ua->request($req);
       
    85 
       
    86     croak $result->status_line if not $result->is_success;
       
    87     return $1 if $result->content =~ /^Auth-Sid:\s+(\S+)/m;
       
    88 
       
    89     croak q{Can't get Auth-Sid};
       
    90 }
       
    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 
       
    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