| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::Braintree::HTTP; | 
| 2 |  |  |  |  |  |  | $WebService::Braintree::HTTP::VERSION = '0.93'; | 
| 3 | 1 |  |  | 1 |  | 410 | use HTTP::Request; | 
|  | 1 |  |  |  |  | 15803 |  | 
|  | 1 |  |  |  |  | 45 |  | 
| 4 | 1 |  |  | 1 |  | 641 | use LWP::UserAgent; | 
|  | 1 |  |  |  |  | 22911 |  | 
|  | 1 |  |  |  |  | 50 |  | 
| 5 | 1 |  |  | 1 |  | 505 | use WebService::Braintree::Xml; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | use Moose; | 
| 7 |  |  |  |  |  |  | use Carp qw(confess); | 
| 8 |  |  |  |  |  |  | use constant CLIENT_VERSION => $WebService::Braintree::VERSION || 'development'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | has 'config' => (is => 'ro', default => sub { WebService::Braintree->configuration }); | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub post { | 
| 13 |  |  |  |  |  |  | my ($self, $path, $params) = @_; | 
| 14 |  |  |  |  |  |  | $self -> make_request($path, $params, 'POST'); | 
| 15 |  |  |  |  |  |  | } | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub put { | 
| 18 |  |  |  |  |  |  | my ($self, $path, $params) = @_; | 
| 19 |  |  |  |  |  |  | $self -> make_request($path, $params, 'PUT'); | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub get { | 
| 23 |  |  |  |  |  |  | my ($self, $path, $params) = @_; | 
| 24 |  |  |  |  |  |  | $self -> make_request($path, $params, 'GET'); | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub delete { | 
| 28 |  |  |  |  |  |  | my ($self, $path, $params) = @_; | 
| 29 |  |  |  |  |  |  | $self -> make_request($path, undef, 'DELETE'); | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub make_request { | 
| 33 |  |  |  |  |  |  | my ($self, $path, $params, $verb) = @_; | 
| 34 |  |  |  |  |  |  | my $request = HTTP::Request->new($verb => $self->config->base_merchant_url . $path); | 
| 35 |  |  |  |  |  |  | $request->headers->authorization_basic($self->config->public_key, $self->config->private_key); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | if ($params) { | 
| 38 |  |  |  |  |  |  | $request->content(hash_to_xml($params)); | 
| 39 |  |  |  |  |  |  | $request->content_type("text/xml; charset=utf-8"); | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | $request->header("X-ApiVersion" => $self->config->api_version); | 
| 43 |  |  |  |  |  |  | $request->header("environment" => $self->config->environment); | 
| 44 |  |  |  |  |  |  | $request->header("User-Agent" => "Braintree Perl Module " . CLIENT_VERSION ); | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | my $agent = LWP::UserAgent->new; | 
| 47 |  |  |  |  |  |  | my $response = $agent->request($request); | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | $self->check_response_code($response->code); | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | if ($response->header('Content-Length') > 1) { | 
| 52 |  |  |  |  |  |  | return xml_to_hash($response->content); | 
| 53 |  |  |  |  |  |  | } else { | 
| 54 |  |  |  |  |  |  | return {http_status => $response->code}; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub check_response_code { | 
| 59 |  |  |  |  |  |  | my ($self, $code) = @_; | 
| 60 |  |  |  |  |  |  | confess "NotFoundError"       if $code eq '404'; | 
| 61 |  |  |  |  |  |  | confess "AuthenticationError" if $code eq '401'; | 
| 62 |  |  |  |  |  |  | confess "AuthorizationError"  if $code eq '403'; | 
| 63 |  |  |  |  |  |  | confess "ServerError"         if $code eq '500'; | 
| 64 |  |  |  |  |  |  | confess "DownForMaintenance"  if $code eq '503'; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 68 |  |  |  |  |  |  | 1; | 
| 69 |  |  |  |  |  |  |  |