File Coverage

blib/lib/Catmandu/CA/API/Request.pm
Criterion Covered Total %
statement 21 49 42.8
branch 0 8 0.0
condition n/a
subroutine 7 12 58.3
pod 0 3 0.0
total 28 72 38.8


line stmt bran cond sub pod time code
1             package Catmandu::CA::API::Request;
2              
3             our $VERSION = '0.04';
4              
5 1     1   4 use strict;
  1         1  
  1         23  
6 1     1   3 use warnings;
  1         1  
  1         18  
7              
8 1     1   4 use Moo;
  1         0  
  1         4  
9 1     1   182 use Catmandu::Sane;
  1         3  
  1         4  
10              
11 1     1   726 use LWP::UserAgent;
  1         30309  
  1         31  
12 1     1   7 use JSON;
  1         1  
  1         10  
13              
14 1     1   553 use Catmandu::CA::API::Login;
  1         2  
  1         516  
15              
16             has url => (is => 'ro', required => 1);
17             has url_query => (is => 'ro', required => 1);
18              
19             has username => (is => 'ro', required => 1);
20             has password => (is => 'ro', required => 1);
21              
22             has lang => (is => 'ro', default => 'nl_NL');
23              
24             has token => (is => 'lazy');
25             has ua => (is => 'lazy');
26              
27             sub _build_token {
28 0     0     my $self = shift;
29 0           my $login = Catmandu::CA::API::Login->new(username => $self->username, password => $self->password, url => $self->url);
30 0           return $login->token();
31             }
32              
33             sub _build_ua {
34 0     0     my $self = shift;
35 0           my $ua = LWP::UserAgent->new(
36             agent => sprintf('catmandu-ca/%s', $VERSION)
37             );
38 0           return $ua;
39             }
40              
41             sub get {
42 0     0 0   my ($self, $query) = @_;
43 0           my $url = sprintf('%s/%s?source=%s&authToken=%s&lang=%s',
44             $self->url,
45             $self->url_query,
46             $query,
47             $self->token,
48             $self->lang
49             );
50 0           my $response = $self->ua->get($url);
51 0 0         if ($response->is_success) {
    0          
52 0           return decode_json($response->decoded_content);
53             } elsif ($response->code == 404) {
54 0           return {};
55             } else {
56 0           Catmandu::HTTPError->throw({
57             code => $response->code,
58             message => $response->status_line,
59             url => $response->request->uri,
60             method => $response->request->method,
61             request_headers => [],
62             request_body => $response->request->decoded_content,
63             response_headers => [],
64             response_body => $response->decoded_content,
65             });
66 0           return {};
67             }
68             }
69              
70             sub put {
71 0     0 0   my ($self, $data) = @_;
72 0           my $url = sprintf('%s/%s&authToken=%s', $self->url, $self->url_query, $self->token);
73 0           my $response = $self->ua->put($url, Content => $data, Content_type => 'application/json');
74              
75 0 0         if (!$response->is_success) {
76 0           Catmandu::HTTPError->throw({
77             code => $response->code,
78             message => $response->status_line,
79             url => $response->request->uri,
80             method => $response->request->method,
81             request_headers => [],
82             request_body => $response->request->decoded_content,
83             response_headers => [],
84             response_body => $response->decoded_content,
85             });
86 0           return 0;
87             }
88              
89 0           return 1;
90             }
91              
92             sub delete {
93 0     0 0   my $self = shift;
94 0           my $url = sprintf('%s/%s&authToken=%s', $self->url, $self->url_query, $self->token);
95 0           my $response = $self->ua->delete($url);
96              
97 0 0         if (!$response->is_success) {
98 0           Catmandu::HTTPError->throw({
99             code => $response->code,
100             message => $response->status_line,
101             url => $response->request->uri,
102             method => $response->request->method,
103             request_headers => [],
104             request_body => $response->request->decoded_content,
105             response_headers => [],
106             response_body => $response->decoded_content,
107             });
108 0           return 0;
109             }
110              
111 0           return 1;
112             }
113              
114             1;
115             __END__