File Coverage

blib/lib/MojoX/CloudFlare/Simple.pm
Criterion Covered Total %
statement 17 37 45.9
branch 0 16 0.0
condition 0 7 0.0
subroutine 6 7 85.7
pod 0 1 0.0
total 23 68 33.8


line stmt bran cond sub pod time code
1             package MojoX::CloudFlare::Simple;
2              
3 1     1   68001 use strict;
  1         13  
  1         31  
4 1     1   6 use warnings;
  1         2  
  1         24  
5 1     1   15 use v5.10;
  1         3  
6 1     1   6 use Carp qw/croak/;
  1         2  
  1         52  
7 1     1   479 use Mojo::Base -base;
  1         191987  
  1         9  
8 1     1   942 use Mojo::UserAgent;
  1         247444  
  1         12  
9              
10             our $VERSION = '0.04';
11              
12             has 'ua' => sub {
13             my $ua = Mojo::UserAgent->new;
14             $ua->transactor->name("MojoX-CloudFlare-Simple $VERSION");
15             return $ua;
16             };
17              
18             has 'url_prefix' => sub { 'https://api.cloudflare.com/client/v4' };
19             has 'email';
20             has 'key';
21             has 'user_service_key';
22              
23             sub request {
24 0     0 0   my ($self, $method, $url, $params) = @_;
25              
26 0 0 0       croak 'email and key are all required' unless $self->email and $self->key;
27              
28 0           my $header = {
29             'X-Auth-Email' => $self->email,
30             'X-Auth-Key' => $self->key,
31             'Content-Type' => 'application/json',
32             };
33 0 0         $header->{'X-Auth-User-Service-Key'} = $self->user_service_key if $self->user_service_key;
34              
35 0 0         $url = '/' . $url unless $url =~ m{^/};
36 0           $url = $self->url_prefix . $url;
37              
38 0           my @extra;
39 0 0         if ($method eq 'GET') {
    0          
40 0           my $uri = Mojo::URL->new($url);
41 0           $uri->query($params);
42 0           $url = $uri->to_string();
43 0           } elsif (grep { $method eq $_ } ('POST', 'PUT', 'PATCH', 'DELETE')) {
44 0           @extra = (json => $params);
45             }
46              
47 0           my $tx = $self->ua->build_tx($method => $url => $header => @extra );
48 0           $tx = $self->ua->start($tx);
49              
50 0 0 0       return $tx->res->json if ($tx->res->headers->content_type || '') =~ /json/;
51 0 0 0       return $tx->res->body if ($tx->res->headers->content_type || '') =~ /application\/octet-stream/;
52              
53 0           my $err = $tx->error;
54 0 0         croak "$err->{code} response: $err->{message}" if $err->{code};
55 0           croak "Connection error: $err->{message}";
56             }
57              
58             1;
59             __END__