Skip to content

feat: add patch method #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/JSON/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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) = @_;
Expand Down Expand Up @@ -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<path> to the API endpoint as the first parameter. The B<put()> and
Perform an HTTP action (GET|POST|PATCH|PUT|DELETE) against the given API. All methods
take the B<path> to the API endpoint as the first parameter. The B<patch()>, B<put()> and
B<post()> methods also accept a second B<data> 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<apphdr> parameter in the last position, which
is a hashref. The referenced hash contains header names and values that will be
Expand All @@ -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<path>, with the provided B<data>. Like
B<get>, this will append path to the end of the B<base_url>.

$api->patch('/objects/', $obj);

=head2 put

Performs an HTTP PUT on the given B<path>, with the provided B<data>. Like
Expand Down
16 changes: 15 additions & 1 deletion t/requests.t
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down