File Coverage

blib/lib/Catmandu/Store/REST/API.pm
Criterion Covered Total %
statement 12 49 24.4
branch 0 12 0.0
condition n/a
subroutine 4 10 40.0
pod 0 5 0.0
total 16 76 21.0


line stmt bran cond sub pod time code
1             package Catmandu::Store::REST::API;
2              
3 1     1   8 use Catmandu::Sane;
  1         3  
  1         8  
4 1     1   138 use Moo;
  1         2  
  1         4  
5 1     1   255 use JSON;
  1         2  
  1         5  
6 1     1   587 use LWP::UserAgent;
  1         31465  
  1         615  
7              
8             has base_url => (is => 'ro', required => 1);
9             has query_string => (is => 'ro');
10              
11             has client => (is => 'lazy');
12              
13             sub _build_client {
14 0     0     my $self = shift;
15 0           return LWP::UserAgent->new();
16             }
17              
18             sub mk_url {
19 0     0 0   my ($self, $id) = @_;
20 0 0         if (defined($id)) {
21 0           return sprintf('%s/%s%s', $self->base_url, $id, $self->query_string);
22             } else {
23 0           return sprintf('%s%s', $self->base_url, $self->query_string);
24             }
25             }
26              
27             sub get {
28 0     0 0   my ($self, $id) = @_;
29 0           my $url = $self->mk_url($id);
30              
31 0           my $response = $self->client->get($url);
32              
33 0 0         if ($response->is_success) {
    0          
34 0           return decode_json($response->decoded_content);
35             } elsif ($response->code == 404) {
36 0           return {};
37             } else {
38 0           Catmandu::HTTPError->throw({
39             code => $response->code,
40             message => $response->status_line,
41             url => $response->request->url,
42             method => $response->request->method,
43             request_headers => [],
44             request_body => $response->request->decoded_content,
45             response_headers => [],
46             response_body => $response->decoded_content,
47             });
48 0           return {};
49             }
50             }
51              
52             sub post {
53 0     0 0   my ($self, $data) = @_;
54 0           my $url = $self->mk_url();
55 0           my $json_data = encode_json($data);
56            
57 0           my $response = $self->client->post($url, Content_Type => 'application/json', Content => $json_data);
58              
59 0 0         if ($response->is_success) {
60 0           return decode_json($response->decoded_content);
61             } else {
62 0           Catmandu::HTTPError->throw({
63             code => $response->code,
64             message => $response->status_line,
65             url => $response->request->url,
66             method => $response->request->method,
67             request_headers => [],
68             request_body => $response->request->decoded_content,
69             response_headers => [],
70             response_body => $response->decoded_content,
71             });
72 0           return {};
73             }
74             }
75              
76             sub put {
77 0     0 0   my ($self, $id, $data) = @_;
78 0           my $url = $self->mk_url($id);
79 0           my $json_data = encode_json($data);
80            
81 0           my $response = $self->client->put($url, Content_Type => 'application/json', Content => $json_data);
82              
83 0 0         if ($response->is_success) {
84 0           return decode_json($response->decoded_content);
85             } else {
86 0           Catmandu::HTTPError->throw({
87             code => $response->code,
88             message => $response->status_line,
89             url => $response->request->url,
90             method => $response->request->method,
91             request_headers => [],
92             request_body => $response->request->decoded_content,
93             response_headers => [],
94             response_body => $response->decoded_content,
95             });
96 0           return {};
97             }
98             }
99              
100             sub delete {
101 0     0 0   my ($self, $id) = @_;
102 0           my $url = $self->mk_url($id);
103              
104 0           my $response = $self->client->delete($url);
105            
106 0 0         if ($response->is_success) {
107 0           return decode_json($response->decoded_content);
108             } else {
109 0           Catmandu::HTTPError->throw({
110             code => $response->code,
111             message => $response->status_line,
112             url => $response->request->url,
113             method => $response->request->method,
114             request_headers => [],
115             request_body => $response->request->decoded_content,
116             response_headers => [],
117             response_body => $response->decoded_content,
118             });
119 0           return {};
120             }
121             }
122              
123             1;