File Coverage

blib/lib/Catmandu/Store/Datahub/API.pm
Criterion Covered Total %
statement 18 92 19.5
branch 0 34 0.0
condition n/a
subroutine 6 15 40.0
pod 0 7 0.0
total 24 148 16.2


line stmt bran cond sub pod time code
1             package Catmandu::Store::Datahub::API;
2              
3 1     1   6 use strict;
  1         2  
  1         25  
4 1     1   4 use warnings;
  1         2  
  1         19  
5              
6 1     1   303 use Catmandu;
  1         79225  
  1         4  
7 1     1   172 use Moo;
  1         2  
  1         13  
8 1     1   626 use JSON;
  1         5459  
  1         4  
9              
10 1     1   103 use LWP::UserAgent;
  1         3  
  1         774  
11              
12             has url => (is => 'ro', required => 1);
13             has client_id => (is => 'ro', required => 1);
14             has client_secret => (is => 'ro', required => 1);
15             has username => (is => 'ro', required => 1);
16             has password => (is => 'ro', required => 1);
17              
18             has client => (is => 'lazy');
19             has access_token => (
20             is => 'lazy',
21             writer => '_set_access_token',
22             builder => '_build_access_token'
23             );
24              
25             sub _build_client {
26 0     0     my $self = shift;
27 0           return LWP::UserAgent->new(keep_alive => 1);
28             }
29              
30             sub _build_access_token {
31 0     0     my $self = shift;
32 0           return $self->generate_token();
33             }
34              
35             sub set_access_token {
36 0     0 0   my $self = shift;
37             # Used to regenerate the token when it becomes invalid
38 0           return $self->_set_access_token($self->generate_token());
39             }
40              
41             sub generate_token {
42 0     0 0   my $self = shift;
43 0           my $oauth = Catmandu::Store::Datahub::OAuth->new(username => $self->username, password => $self->password, client_id => $self->client_id, client_secret => $self->client_secret, url => $self->url);
44 0           return $oauth->token();
45             }
46              
47             sub get {
48 0     0 0   my ($self, $id) = @_;
49              
50 0 0         if (not defined $id) {
51 0           Catmandu::BadArg->throw(
52             'message' => "Record could not be retrieved. Missing 'id'."
53             );
54             }
55              
56 0           my $url = sprintf('%s/api/v1/data/%s', $self->url, $id);
57              
58 0           my $response = $self->client->get($url, Authorization => sprintf('Bearer %s', $self->access_token));
59              
60 0 0         if ($response->is_success) {
    0          
    0          
61 0           return decode_json($response->decoded_content);
62             } elsif ($response->code == 401) {
63 0           my $error = decode_json($response->decoded_content);
64 0 0         if ($error->{'error_description'} eq 'The access token provided has expired.') {
65 0           $self->set_access_token();
66 0           return $self->get($id);
67             }
68             } elsif ($response->code == 404) {
69 0           return {};
70             } else {
71 0           Catmandu::HTTPError->throw({
72             code => $response->code,
73             message => $response->headers->header('message'),
74             url => $response->request->uri,
75             method => $response->request->method,
76             request_headers => [],
77             request_body => $response->request->decoded_content,
78             response_headers => [],
79             response_body => $response->decoded_content,
80             });
81 0           return undef;
82             }
83             }
84              
85             sub add {
86 0     0 0   my ($self, $data) = @_;
87 0           my $url = sprintf('%s/api/v1/data.lidoxml', $self->url);
88              
89 0           my $token = $self->access_token;
90 0           my $response;
91              
92 0           $response = $self->client->post($url, Content_Type => 'application/lido+xml', Authorization => sprintf('Bearer %s', $token), Content => $data);
93              
94 0 0         if ($response->is_success) {
    0          
95 0           return $response->decoded_content;
96             } elsif ($response->code == 401) {
97 0           my $error = decode_json($response->decoded_content);
98 0 0         if ($error->{'error_description'} eq 'The access token provided has expired.') {
99 0           $self->set_access_token();
100 0           return $self->add($data);
101             }
102             } else {
103 0           Catmandu::HTTPError->throw({
104             code => $response->code,
105             message => $response->headers->header('message'),
106             url => $response->request->uri,
107             method => $response->request->method,
108             request_headers => [],
109             request_body => $response->request->decoded_content,
110             response_headers => [],
111             response_body => $response->decoded_content,
112             });
113 0           return undef;
114             }
115             }
116              
117             sub update {
118 0     0 0   my ($self, $id, $data) = @_;
119              
120 0 0         if (not defined $id) {
121 0           Catmandu::BadArg->throw(
122             'message' => "Record could not be updated. Missing 'id'."
123             );
124             }
125              
126 0           my $url = sprintf('%s/api/v1/data/%s', $self->url, $id);
127              
128 0           my $token = $self->access_token;
129 0           my $response;
130              
131 0           $response = $self->client->put($url, Content_Type => 'application/lido+xml', Authorization => sprintf('Bearer %s', $token), Content => $data);
132              
133 0 0         if ($response->is_success) {
    0          
134 0           return $response->decoded_content;
135             } elsif ($response->code == 401) {
136 0           my $error = decode_json($response->decoded_content);
137 0 0         if ($error->{'error_description'} eq 'The access token provided has expired.') {
138 0           $self->set_access_token();
139 0           return $self->update($id, $data);
140             }
141             } else {
142 0           Catmandu::HTTPError->throw({
143             code => $response->code,
144             message => $response->headers->header('message'),
145             url => $response->request->uri,
146             method => $response->request->method,
147             request_headers => [],
148             request_body => $response->request->decoded_content,
149             response_headers => [],
150             response_body => $response->decoded_content,
151             });
152 0           return undef;
153             }
154             }
155              
156             sub delete {
157 0     0 0   my ($self, $id) = @_;
158              
159 0 0         if (not defined $id) {
160 0           Catmandu::BadArg->throw(
161             'message' => "Record could not be deleted. Missing 'id'."
162             );
163             }
164              
165 0           my $url = sprintf('%s/api/v1/data/%s', $self->url, $id);
166              
167 0           my $token = $self->access_token;
168 0           my $response;
169              
170 0           $response = $self->client->delete($url, Authorization => sprintf('Bearer %s', $token));
171              
172 0 0         if ($response->is_success) {
    0          
173 0           return $response->decoded_content;
174             } elsif ($response->code == 401) {
175 0           my $error = decode_json($response->decoded_content);
176 0 0         if ($error->{'error_description'} eq 'The access token provided has expired.') {
177 0           $self->set_access_token();
178 0           return $self->delete($id);
179             }
180             } else {
181 0           Catmandu::HTTPError->throw({
182             code => $response->code,
183             message => $response->headers->header('message'),
184             url => $response->request->uri,
185             method => $response->request->method,
186             request_headers => [],
187             request_body => $response->request->decoded_content,
188             response_headers => [],
189             response_body => $response->decoded_content,
190             });
191 0           return undef;
192             }
193             }
194              
195             sub list {
196 0     0 0   my ($self) = @_;
197 0           my $url = sprintf('%s/api/v1/data', $self->url);
198              
199 0           my $token = $self->access_token;
200 0           my $response = $self->client->get($url, Authorization => sprintf('Bearer %s', $token));
201              
202 0 0         if ($response->is_success) {
203 0           return decode_json($response->decoded_content);
204             } else {
205 0           Catmandu::HTTPError->throw({
206             code => $response->code,
207             message => $response->headers->header('message'),
208             url => $response->request->uri,
209             method => $response->request->method,
210             request_headers => [],
211             request_body => $response->request->decoded_content,
212             response_headers => [],
213             response_body => $response->decoded_content,
214             });
215 0           return undef;
216             }
217             }
218              
219             1;
220              
221             __END__