File Coverage

blib/lib/Catmandu/CA/API/Login.pm
Criterion Covered Total %
statement 24 43 55.8
branch 0 6 0.0
condition n/a
subroutine 8 11 72.7
pod 0 1 0.0
total 32 61 52.4


line stmt bran cond sub pod time code
1             package Catmandu::CA::API::Login;
2              
3             our $VERSION = '0.05';
4              
5 1     1   4 use strict;
  1         2  
  1         24  
6 1     1   17 use warnings;
  1         2  
  1         27  
7              
8 1     1   3 use Moo;
  1         1  
  1         7  
9 1     1   230 use Catmandu::Sane;
  1         1  
  1         7  
10              
11 1     1   176 use Data::Dumper qw(Dumper);
  1         1  
  1         56  
12              
13 1     1   4 use LWP::UserAgent;
  1         1  
  1         13  
14 1     1   410 use HTTP::Request::Common;
  1         1538  
  1         49  
15 1     1   4 use JSON;
  1         2  
  1         4  
16              
17             has username => (is => 'ro', required => 1);
18             has password => (is => 'ro', required => 1);
19             has url => (is => 'ro', required => 1);
20              
21             has port => (is => 'lazy');
22             has ua => (is => 'lazy');
23              
24             sub _build_port {
25 0     0     my $self = shift;
26             # If the port is included
27 0 0         if ($self->url =~ /^https?:\/\/[^\/:]:([0-9]+)\//) {
28 0           return $1;
29             }
30             # If it is https
31 0 0         if ($self->url =~ /^https/) {
32 0           return '443';
33             } else {
34             # Anything else
35 0           return '80';
36             }
37             }
38              
39             sub _build_ua {
40 0     0     my $self = shift;
41 0           my $ua = LWP::UserAgent->new(
42             agent => sprintf('catmandu-ca/%s', $VERSION)
43             );
44 0           return $ua;
45             }
46              
47             sub token {
48 0     0 0   my ($self) = @_;
49 0           my $url = sprintf('%s/service.php/auth/login', $self->url);
50 0           my $request = GET $url;
51 0           $request->authorization_basic($self->username, $self->password);
52 0           my $response = $self->ua->request($request);
53 0 0         if ($response->is_success) {
54 0           my $content = decode_json($response->decoded_content);
55 0           return $content->{'authToken'};
56             } else {
57 0           Catmandu::HTTPError->throw({
58             code => $response->code,
59             message => $response->status_line,
60             url => $response->request->uri,
61             method => $response->request->method,
62             request_headers => [],
63             request_body => $response->request->decoded_content,
64             response_headers => [],
65             response_body => $response->decoded_content,
66             });
67 0           return undef;
68             }
69             }
70              
71             1;
72             __END__