File Coverage

blib/lib/Catmandu/Store/Resolver/API.pm
Criterion Covered Total %
statement 12 79 15.1
branch 0 14 0.0
condition n/a
subroutine 4 12 33.3
pod 0 6 0.0
total 16 111 14.4


line stmt bran cond sub pod time code
1             package Catmandu::Store::Resolver::API;
2              
3 1     1   4 use Moo;
  1         1  
  1         4  
4 1     1   162 use JSON;
  1         1  
  1         8  
5 1     1   627 use LWP::UserAgent;
  1         28175  
  1         24  
6              
7 1     1   5 use Catmandu::Sane;
  1         1  
  1         6  
8              
9             has url => (is => 'ro', required => 1);
10             has username => (is => 'ro', required => 1);
11             has password => (is => 'ro', required => 1);
12              
13             has ua => (is => 'lazy');
14             has cookie_jar => (is => 'lazy');
15              
16             sub _build_ua {
17 0     0     my $self = shift;
18 0           my $ua = LWP::UserAgent->new();
19 0           $ua->cookie_jar({});
20 0           return $ua;
21             }
22              
23             sub _build_cookie_jar {
24 0     0     my $self = shift;
25 0           return $self->login();
26             }
27              
28             sub login {
29 0     0 0   my $self = shift;
30 0           my $auth_url = '%s/resolver/api/login';
31 0           my $req_url = sprintf($auth_url, $self->url);
32 0           my $form = {
33             'username' => $self->username,
34             'password' => $self->password
35             };
36 0           my $response = $self->ua->post($req_url, Content => $form);
37 0 0         if ($response->is_success) {
38 0           return $self->ua->cookie_jar;
39             } else {
40 0           Catmandu::HTTPError->throw({
41             code => $response->code,
42             message => $response->status_line,
43             url => $response->request->uri,
44             method => $response->request->method,
45             request_headers => [],
46             request_body => $response->request->decoded_content,
47             response_headers => [],
48             response_body => $response->decoded_content
49             });
50 0           return undef;
51             }
52             }
53              
54             sub logout {
55 0     0 0   my $self = shift;
56 0           my $url = '%s/resolver/api/logout';
57 0           my $req_url = sprintf($url, $self->url);
58 0           $self->ua->cookie_jar($self->cookie_jar);
59              
60 0           my $response = $self->ua->get($req_url);
61 0 0         if ($response->is_success) {
62 0           return 1;
63             } else {
64 0           Catmandu::HTTPError->throw({
65             code => $response->code,
66             message => $response->status_line,
67             url => $response->request->uri,
68             method => $response->request->method,
69             request_headers => [],
70             request_body => $response->request->decoded_content,
71             response_headers => [],
72             response_body => $response->decoded_content
73             });
74 0           return undef;
75             }
76             }
77              
78             sub get {
79 0     0 0   my ($self, $id) = @_;
80 0           my $url = '%s/resolver/api/entity/original/%s';
81 0           my $req_url = sprintf($url, $self->url, $id);
82              
83 0           $self->ua->cookie_jar($self->cookie_jar);
84              
85 0           my $response = $self->ua->get($req_url);
86              
87 0 0         if ($response->is_success) {
88             # New API
89 0           return decode_json($response->decoded_content);
90             } else {
91             # Try with the old API
92             # Please note: this doesn't work with the original object number
93             # so it could say 'does not exist' because the original object number
94             # is not equal to the PID
95 0           $url = '%s/resolver/api/entity/%s';
96 0           $req_url = sprintf($url, $self->url, $id);
97 0           $response = $self->ua->get($req_url);
98 0 0         if ($response->is_success) {
99 0           return decode_json($response->decoded_content);
100             } else {
101             # Give up
102 0           Catmandu::HTTPError->throw({
103             code => $response->code,
104             message => $response->status_line,
105             url => $response->request->uri,
106             method => $response->request->method,
107             request_headers => [],
108             request_body => $response->request->decoded_content,
109             response_headers => [],
110             response_body => $response->decoded_content
111             });
112 0           return undef;
113             }
114             }
115             }
116              
117             sub post {
118 0     0 0   my ($self, $data) = @_;
119 0           my $json_data = encode_json($data);
120 0           my $url = '%s/resolver/api/entity';
121 0           my $req_url = sprintf($url, $self->url);
122              
123 0           $self->ua->cookie_jar($self->login());
124              
125 0           my $response = $self->ua->post($req_url, Content_Type => 'application/json', Content => $json_data);
126              
127 0 0         if ($response->is_success) {
128 0           return decode_json($response->decoded_content);
129             } else {
130 0           Catmandu::HTTPError->throw({
131             code => $response->code,
132             message => $response->status_line,
133             url => $response->request->uri,
134             method => $response->request->method,
135             request_headers => [],
136             request_body => $response->request->decoded_content,
137             response_headers => [],
138             response_body => $response->decoded_content
139             });
140 0           return undef;
141             }
142              
143             }
144              
145             sub put {
146 0     0 0   my ($self, $id, $data) = @_;
147 0           my $json_data = encode_json($data);
148 0           my $url = '%s/resolver/api/entity/%s';
149 0           my $req_url = sprintf($url, $self->url, $id);
150              
151 0           $self->ua->cookie_jar($self->login());
152              
153 0           my $response = $self->ua->put($req_url, Content_Type => 'application/json', Content => $json_data);
154              
155 0 0         if ($response->is_success) {
156 0           return decode_json($response->decoded_content);
157             } else {
158 0           Catmandu::HTTPError->throw({
159             code => $response->code,
160             message => $response->status_line,
161             url => $response->request->uri,
162             method => $response->request->method,
163             request_headers => [],
164             request_body => $response->request->decoded_content,
165             response_headers => [],
166             response_body => $response->decoded_content
167             });
168 0           return undef;
169             }
170             }
171              
172             sub delete {
173 0     0 0   my ($self, $id) = @_;
174 0           my $url = '%s/resolver/api/entity/%s';
175 0           my $req_url = sprintf($url, $self->url, $id);
176              
177 0           $self->ua->cookie_jar($self->login());
178              
179 0           my $response = $self->ua->delete($req_url);
180              
181 0 0         if ($response->is_success) {
182 0           return $response->decoded_content;
183             } else {
184 0           Catmandu::HTTPError->throw({
185             code => $response->code,
186             message => $response->status_line,
187             url => $response->request->uri,
188             method => $response->request->method,
189             request_headers => [],
190             request_body => $response->request->decoded_content,
191             response_headers => [],
192             response_body => $response->decoded_content
193             });
194 0           return undef;
195             }
196             }
197              
198             1;