/
Host.pm
139 lines (115 loc) · 3.43 KB
/
Host.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package WebService::CloudFlare::Host;
use Moose;
use Try::Tiny;
use Data::Dumper;
use LWP;
has 'host_key' => (
is => 'rw',
isa => 'Str',
required => 1,
);
has 'base_api' => (
is => 'ro',
isa => 'Str',
default => 'https://api.cloudflare.com/host-gw.html',
);
has 'user_agent' => (
is => 'ro',
isa => 'Str',
default => 'WebService::CloudFlare::Host/1.0',
);
has 'http_timeout' => (
is => 'ro',
isa => 'Int',
default => 60,
);
has 'ua' => (
is => 'ro',
isa => 'LWP::UserAgent',
lazy_build => 1,
);
sub call {
my ( $self, $class, %args ) = @_;
# Load the request class.
try {
Class::MOP::load_class("WebService::CloudFlare::Host::Request::$class");
} catch {
$self->throw_exception( $_, "Loading request class: $class",
"Class::MOP::load_class",
"WebService::CloudFlare::Host::Request::$class"
);
};
# Create the request object.
my $req = try {
"WebService::CloudFlare::Host::Request::$class"->new( %args )
} catch {
$self->throw_exception( $_, 'Creating API Request',
"WebService::CloudFlare::Host::Request::$class", Dumper \%args
);
};
# Make the actual HTTP request.
my $http_res = try {
$self->_do_call( $req );
} catch {
$self->throw_exception( $_, 'Making HTTP Call To Server API',
'WebService::CloudFlare::Host::do_call', Dumper $req
);
};
# print Dumper $http_res;
#print "------------------------------------\n";
#print $http_res->content;
# Create a response object to send back to the user.
my $res = try {
$self->_create_response( $class, $http_res );
} catch {
$self->throw_exception( $_, 'Creating API Response From HTTP Data',
'WebService::CloudFlare::Host::create_response', Dumper $http_res,
);
};
return $res;
}
sub throw_exception {
my ( $self, $message, $layer, $function, $args ) = @_;
# We installed ::Exception with the package, it's here.
Class::MOP::load_class('WebService::CloudFlare::Host::Exception');
die WebService::CloudFlare::Host::Exception->new(
message => $message,
layer => $layer,
function => $function,
args => $args,
);
}
sub _do_call {
my ( $self, $request ) = @_;
my %arguments = map { my $v = $request->{$request->map->{$_}};
defined($v) ? ($_ => $v) : ()
} keys %{$request->map};
$arguments{host_key} = $self->host_key;
return $self->_http_post( $self->base_api, \%arguments );
}
sub _http_post {
my ( $self, $url, $post_data ) = @_;
my $res = $self->ua->post( $url, $post_data );
return $res;
}
sub _create_response {
my ( $self, $class, $http ) = @_;
try {
Class::MOP::load_class("WebService::CloudFlare::Host::Response::$class");
print "Loaded class";
} catch {
$self->throw_exception( $_, "Loading request class: $class",
"Class::MOP::load_class",
"WebService::CloudFlare::Host::Reponse::$class"
);
};
return "WebService::CloudFlare::Host::Response::$class"->new( $http );
}
sub _build_ua {
my ( $self ) = @_;
return LWP::UserAgent->new(
timeout => $self->http_timeout,
user_agent => $self->user_agent
);
}
1;