diff --git a/lib/JSON/API.pm b/lib/JSON/API.pm index 1b2d164..6c89160 100644 --- a/lib/JSON/API.pm +++ b/lib/JSON/API.pm @@ -174,6 +174,12 @@ sub get $self->_http_req("GET", $path, undef, $apphdr); } +sub patch +{ + my ($self, $path, $data, $apphdr) = @_; + $self->_http_req("PATCH", $path, $data, $apphdr); +} + sub put { my ($self, $path, $data, $apphdr) = @_; @@ -307,12 +313,12 @@ being decoded. =back -=head2 get|post|put|del +=head2 get|post|patch|put|del -Perform an HTTP action (GET|POST|PUT|DELETE) against the given API. All methods -take the B to the API endpoint as the first parameter. The B and +Perform an HTTP action (GET|POST|PATCH|PUT|DELETE) against the given API. All methods +take the B to the API endpoint as the first parameter. The B, B and B methods also accept a second B parameter, which should be a reference -to be serialized into JSON for POST/PUTing to the endpoint. +to be serialized into JSON for POST/PATCH/PUTing to the endpoint. All methods also accept an optional B parameter in the last position, which is a hashref. The referenced hash contains header names and values that will be @@ -337,6 +343,13 @@ this will be turned into querystring parameters, with URI encoded values. # Automatically add + encode querystring params my $obj = $api->get('/objects/1', { param => 'value' }); +=head2 patch + +Performs an HTTP PATCH on the given B, with the provided B. Like +B, this will append path to the end of the B. + + $api->patch('/objects/', $obj); + =head2 put Performs an HTTP PUT on the given B, with the provided B. Like diff --git a/t/requests.t b/t/requests.t index 83d426f..3b691ab 100644 --- a/t/requests.t +++ b/t/requests.t @@ -18,6 +18,12 @@ sub call_api my ($code, $response) = $api->put($path, $data); is($code, $expected_code, "List context HTTP Code for $message should return $expected_code"); is_deeply($response, $expected, "List context response for $message"); + } elsif ($METHOD eq "PATCH") { + my $scalar = $api->patch($path, $data); + is_deeply($scalar, $expected, "Scalar context for $message"); + my ($code, $response) = $api->post($path, $data); + is($code, $expected_code, "List context HTTP Code for $message should return $expected_code"); + is_deeply($response, $expected, "List context response for $message"); } elsif ($METHOD eq "POST") { my $scalar = $api->post($path, $data); is_deeply($scalar, $expected, "Scalar context for $message"); @@ -31,7 +37,7 @@ sub call_api is($code, $expected_code, "List context HTTP Code: $message should return $expected_code"); is_deeply($response, $expected, "List context response for $message"); } else { - fail("Invalid METHOD provided. Must be one of GET PUT POST DELETE"); + fail("Invalid METHOD provided. Must be one of GET PUT PATCH POST DELETE"); } } @@ -128,6 +134,10 @@ call_api($api, "POST", '/get_invalid_json', undef, {}, 200, "post('/get_invalid_json') returns {}"); +call_api($api, "PATCH", '/get_invalid_json', undef, + {}, 200, + "post('/get_invalid_json') returns {}"); + call_api($api, "PUT",'/put_valid_json', {name => 'foo', value => 'bar'}, { code => 'success' }, 200, "put('/put_valid_json') returns with decoded content"); @@ -136,6 +146,10 @@ call_api($api, "POST", '/post_valid_json', {name => 'foo', value => 'bar'}, { code => 'success' }, 200, "post('/post_valid_json') returns with decoded content"); +call_api($api, "PATCH", '/post_valid_json', {name => 'foo', value => 'bar'}, + { code => 'success' }, 200, + "post('/post_valid_json') returns with decoded content"); + call_api($api, "DELETE", '/del_valid_json', undef, {}, 200, "del('/del_valid_json') returns without content");