File Coverage

blib/lib/MojoX/CloudFlare/Simple.pm
Criterion Covered Total %
statement 17 36 47.2
branch 0 14 0.0
condition 0 5 0.0
subroutine 6 7 85.7
pod 0 1 0.0
total 23 63 36.5


line stmt bran cond sub pod time code
1             package MojoX::CloudFlare::Simple;
2              
3 1     1   52986 use strict;
  1         8  
  1         21  
4 1     1   4 use warnings;
  1         2  
  1         17  
5 1     1   9 use v5.10;
  1         3  
6 1     1   3 use Carp qw/croak/;
  1         2  
  1         38  
7 1     1   388 use Mojo::Base -base;
  1         105200  
  1         5  
8 1     1   665 use Mojo::UserAgent;
  1         184805  
  1         11  
9              
10             our $VERSION = '0.03';
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              
52 0           my $err = $tx->error;
53 0 0         croak "$err->{code} response: $err->{message}" if $err->{code};
54 0           croak "Connection error: $err->{message}";
55             }
56              
57             1;
58             __END__