File Coverage

blib/lib/Mojo/CloudCheckr.pm
Criterion Covered Total %
statement 20 24 83.3
branch 3 6 50.0
condition n/a
subroutine 4 5 80.0
pod 1 1 100.0
total 28 36 77.7


line stmt bran cond sub pod time code
1             package Mojo::CloudCheckr;
2 1     1   327011 use Mojo::Base -base;
  1         3  
  1         5  
3              
4             our $VERSION = '0.01';
5              
6 1     1   126 use Mojo::UserAgent;
  1         2  
  1         5  
7              
8             has access_key => sub { die 'missing access_key' };
9             has base_url => 'https://api.cloudcheckr.com/api';
10             has format => 'json';
11             has _ua => sub { Mojo::UserAgent->new };
12              
13             sub get {
14 1 50   1 1 5089 my ($self, $cb) = (shift, ref $_[-1] eq 'CODE' ? pop @_ : ());
15 1         3 my ($controller, $action) = (shift, shift);
16 1 50       5 my $args = @_ % 2 == 0 ? {@_} : shift;
17 1 50       4 if ( $cb ) {
18             $self->_ua->get($self->_url($controller, $action) => form => $args => sub {
19 0     0   0 my ($ua, $tx) = @_;
20 0         0 $tx->req->params->remove('access_key');
21 0         0 $cb->($ua, $tx);
22 0         0 });
23             } else {
24 1         4 my $tx = $self->_ua->get($self->_url($controller, $action) => form => $args);
25 1         10347 $tx->req->params->remove('access_key');
26 1         144 return $tx;
27             }
28             }
29              
30             sub _url {
31 1     1   7 my ($self, $controller, $action) = @_;
32 1         4 my $format = $self->format;
33 1         7 my $url = Mojo::URL->new($self->base_url);
34 1         196 push @{$url->path->parts}, "$controller.$format", $action;
  1         4  
35 1         18 $url->query(access_key => $self->access_key);
36 1         60 return $url;
37             }
38              
39             1;
40              
41             =encoding utf8
42              
43             =head1 NAME
44              
45             Mojo::CloudCheckr - A simple interface to the CloudCheckr API
46              
47             =head1 SYNOPSIS
48              
49             use Mojo::CloudCheckr;
50              
51             my $cc = Mojo::CloudCheckr->new(access_key => '...');
52             say $cc->get(account => 'get_accounts_v2')->result->json('/accounts_and_users/0/account_name');
53            
54             =head1 DESCRIPTION
55              
56             A simple interface to the CloudCheckr API.
57              
58             Currently only a single L method is available, and it offers no data
59             validation or error handling. No built-in support for paging. No support for
60             POST queries (GET only). Pull requests welcome!
61              
62             The API user guide is available at L
63             The API reference guide is available at L
64             The API admin reference guide is available at L
65             The API inventory guide is available at L
66              
67             So what does this module do? It makes building the API URL easier and includes
68             the access key on all requests. It then removes the access key from the request
69             message of the returned transaction. So, not much. But it does offer a little
70             bit of sugar, and, hopefully eventually, some data validation, error handling,
71             built-in support for paging, and POST queries.
72              
73             =head1 ATTRIBUTES
74              
75             L implements the following attributes.
76              
77             =head2 access_key
78              
79             my $access_key = $cc->access_key;
80             $cc = $cc->access_key($key);
81              
82             The access_key for your CloudCheckr API. Learn more at L
83              
84             =head2 base_url
85              
86             my $base_url = $cc->base_url;
87             $cc = $cc->base_url($url);
88              
89             The base URL for the CloudCheckr API, defaults to https://api.cloudcheckr.com/api.
90              
91             =head2 format
92              
93             my $format = $cc->format;
94             $cc = $cc->format($format);
95              
96             The response format from the CloudCheckr API, defaults to json.
97              
98             # Set the format to XML
99             $cc->format('xml');
100              
101             =head1 METHODS
102              
103             L inherits all methods from L and implements the
104             following new ones.
105              
106             =head2 get
107              
108             # Blocking
109             my $tx = $cc->get(controller => 'action', %args);
110             say $tx->result->body;
111            
112             # Non-blocking
113             $cc->get(controller => 'action', %args => sub {
114             my ($ua, $tx) = @_;
115             say $tx->result->body;
116             });
117              
118             All CloudCheckr API calls have a controller (or category) and an action (or
119             task). Each of these is required for L and is defined in the API
120             reference docs. If a callback is provided, it will process the request non-
121             blocking. The access_key parameter will be removed from the request in the
122             returned transaction (so that the arguments that were used to generate the
123             response can be dumped without the need to hide this private key.)
124              
125             =head1 SEE ALSO
126              
127             L
128              
129             =cut