File Coverage

lib/WebService/Braintree/HTTP.pm
Criterion Covered Total %
statement 15 17 88.2
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 21 23 91.3


line stmt bran cond sub pod time code
1             # vim: sw=4 ts=4 ft=perl
2              
3             package WebService::Braintree::HTTP;
4             $WebService::Braintree::HTTP::VERSION = '0.94';
5 20     20   302 use 5.010_001;
  20         55  
6 20     20   93 use strictures 1;
  20         102  
  20         624  
7              
8 20     20   8222 use Data::Dumper;
  20         91794  
  20         1157  
9 20     20   4729 use HTTP::Request;
  20         156900  
  20         601  
10 20     20   7846 use LWP::UserAgent;
  20         294065  
  20         721  
11              
12 20     20   5367 use WebService::Braintree::Xml qw(hash_to_xml xml_to_hash);
  0            
  0            
13              
14             use Moose;
15             use Carp qw(confess);
16             use constant CLIENT_VERSION => $WebService::Braintree::VERSION || 'development';
17              
18             has 'config' => (is => 'ro', default => sub { WebService::Braintree->configuration });
19              
20             sub post {
21             my ($self, $path, $params) = @_;
22             $self->make_request($path, $params, 'POST');
23             }
24              
25             sub put {
26             my ($self, $path, $params) = @_;
27             $self->make_request($path, $params, 'PUT');
28             }
29              
30             sub get {
31             my ($self, $path, $params) = @_;
32             $self->make_request($path, $params, 'GET');
33             }
34              
35             sub delete {
36             my ($self, $path, $params) = @_;
37             $self->make_request($path, undef, 'DELETE');
38             }
39              
40             sub make_request {
41             my ($self, $path, $params, $verb) = @_;
42             my $request = HTTP::Request->new($verb => $self->config->base_merchant_url . $path);
43             $request->headers->authorization_basic($self->config->public_key, $self->config->private_key);
44              
45             if ($params) {
46             $request->content(hash_to_xml($params));
47             $request->content_type("text/xml; charset=utf-8");
48             }
49              
50             $request->header("X-ApiVersion" => $self->config->api_version);
51             $request->header("environment" => $self->config->environment);
52             $request->header("User-Agent" => "Braintree Perl Module " . CLIENT_VERSION );
53              
54             my $agent = LWP::UserAgent->new;
55              
56             warn Dumper $request if $ENV{WEBSERVICE_BRAINTREE_DEBUG};
57             my $response = $agent->request($request);
58             warn Dumper $response->content if $ENV{WEBSERVICE_BRAINTREE_DEBUG};
59              
60             $self->check_response_code($response->code);
61              
62             if ($response->header('Content-Length') > 1) {
63             return xml_to_hash($response->content);
64             } else {
65             return {http_status => $response->code};
66             }
67             }
68              
69             sub check_response_code {
70             my ($self, $code) = @_;
71             confess "NotFoundError" if $code eq '404';
72             confess "AuthenticationError" if $code eq '401';
73             confess "AuthorizationError" if $code eq '403';
74             confess "ServerError" if $code eq '500';
75             confess "DownForMaintenance" if $code eq '503';
76             }
77              
78             __PACKAGE__->meta->make_immutable;
79              
80             1;
81             __END__