| 1 | package CloudFlare::Client; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Object Orientated Interface to CloudFlare client API | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Or Kavorka will explode | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; use warnings; no indirect 'fatal'; use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Moose; use MooseX::StrictConstructor; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Types::Standard 'Str'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use CloudFlare::Client::Types 'LWPUserAgent'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Kavorka; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use CloudFlare::Client::Exception::Connection; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use CloudFlare::Client::Exception::Upstream; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use LWP::UserAgent 6.02; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # This isn't used directly but we want the dependency | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use LWP::Protocol::https 6.02; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | our $VERSION = 'v0.55.5'; # TRIAL VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # CF credentials | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 5 | has '_user' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | init_arg => 'user',); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 5 | has '_key' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | init_arg => 'apikey',); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 5 | Readonly my $UA_STRING => "CloudFlare::Client/$CloudFlare::Client::VERSION"; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | ▶ | sub _buildUa { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_buildUa: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 4 | Readonly my $ua => LWP::UserAgent::->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | $ua->agent($UA_STRING); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 4 | return $ua;} | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | has '_ua' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | isa => LWPUserAgent, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | builder => '_buildUa',); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # Calls through to the CF API, can throw exceptions under ::Exception:: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | Readonly my $CF_URL => 'https://www.cloudflare.com/api_json.html'; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | method _apiCall ( $act is ro, %args is ro ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
_apiCall: called CC=11 SCAR=24.4
Branch: 7 branches
Condition33%1/3
$#_ == 1 and ref $_[1] eq "HASH"
MC/DC0%0/2
$#_ == 1 and ref $_[1] eq "HASH"
$#_ == 1
ref $_[1] eq "HASH"
Truth table33%1/3
A and B
A= $#_ == 1B= ref $_[1] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # query cloudflare | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | Readonly my $res => $self->_ua->post( $CF_URL, { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | %args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # global args | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # override user specified | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | tkn => $self->_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | email => $self->_user, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | a => $act,}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # Handle connection errors | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | CloudFlare::Client::Exception::Connection::->throw( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | status => $res->status_line, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | message => 'HTTPS request failed',) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | unless $res->is_success; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # Handle errors from CF | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | Readonly my $info => decode_json($res->decoded_content); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | CloudFlare::Client::Exception::Upstream::->throw( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | errorCode => $info->{err_code}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | message => $info->{msg},) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | unless $info->{result} eq 'success'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | return $info->{response};} | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # all API calls are implemented through autoloading, the action is the method | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 5 | ▶ | method AUTOLOAD { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
AUTOLOAD: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 3 | our $AUTOLOAD; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # pull action out of f.q. method name | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | my $act = $AUTOLOAD =~ s/.*:://r; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | return $self->_apiCall( $act, @_ );} | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | 1; # End of CloudFlare::Client | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||