File Coverage

blib/lib/Webservice/KeyVal/API.pm
Criterion Covered Total %
statement 20 43 46.5
branch 0 10 0.0
condition n/a
subroutine 7 10 70.0
pod 3 3 100.0
total 30 66 45.4


line stmt bran cond sub pod time code
1             package Webservice::KeyVal::API;
2              
3 1     1   247218 use v5.10;
  1         4  
4 1     1   4 use strict;
  1         2  
  1         28  
5 1     1   453 use URI::Escape qw/uri_escape/;
  1         1726  
  1         95  
6              
7             our $VERSION = '0.9.10';
8              
9 1     1   853 use HTTP::Tiny;
  1         51565  
  1         100  
10 1     1   2680 use JSON qw/decode_json/;
  1         16933  
  1         10  
11 1     1   1237 use Util::H2O::More qw/baptise ddd HTTPTiny2h2o h2o/;
  1         18132  
  1         281  
12              
13             use constant {
14 1         553 BASEURL => "https://api.keyval.org",
15 1     1   15 };
  1         4  
16              
17             sub new {
18 0     0 1   my $pkg = shift;
19 0           my $self = baptise { ua => HTTP::Tiny->new }, $pkg;
20 0           return $self;
21             }
22              
23             sub set($$) {
24 0     0 1   my $self = shift;
25              
26 0           my ($key, $val) = @_; # looking for, "key => val"
27              
28 0 0         $key = ($key) ? uri_escape $key : "-";
29 0           $val = uri_escape $val;
30              
31 0           my $URL = sprintf "%s/set/%s/%s", BASEURL, $key, $val; # provides a unique key name
32              
33 0           my $resp = HTTPTiny2h2o $self->ua->get($URL);
34              
35             # checks HTTP::Tiny's HTTP status field - though this API appears to
36             # not implement meaningful statuses (all returned are 200 OK)
37 0 0         if (not $resp->success) {
38 0           die $resp;
39             }
40              
41 0 0         if ($resp->content->status ne "SUCCESS") {
42 0           die sprintf("Key: '%s', Error: '%s'\n", $key, $resp->content->status);
43             }
44              
45 0           return $resp->content;
46             }
47              
48             sub get($) {
49 0     0 1   my ($self, $key) = @_;
50              
51 0           $key = uri_escape $key;
52              
53 0           my $URL = sprintf "%s/get/%s", BASEURL, $key;
54              
55 0           my $resp = HTTPTiny2h2o $self->ua->get($URL);
56              
57             # checks HTTP::Tiny's HTTP status field - though this API appears to
58             # not implement meaningful statuses (all returned are 200 OK)
59 0 0         if (not $resp->success) {
60 0           die $resp;
61             }
62              
63 0 0         if ($resp->content->status ne "SUCCESS") {
64 0           die sprintf("Key: '%s', Error: '%s'\n", $key, $resp->content->status);
65             }
66              
67 0           return $resp->content;
68             }
69              
70             1;
71              
72             __END__