[Bps-public-commit] rt-extension-rest2 branch, master, updated. 40d50270b6577177317067a59fe3464cc166329c
Shawn Moore
shawn at bestpractical.com
Tue Dec 13 14:48:15 EST 2016
The branch, master has been updated
via 40d50270b6577177317067a59fe3464cc166329c (commit)
via cd3a717aac7300ac31788ad814ce4ddcb6e4771e (commit)
via 2a5b9dbb104ab540fb1b580bfcbbb0e54302933d (commit)
from c8581fb576e41372f0d4e6a4ec8827ed8c142f6b (commit)
Summary of changes:
README | 8 ++---
lib/RT/Extension/REST2.pm | 8 ++---
lib/RT/Extension/REST2/Resource/Record.pm | 11 ++----
lib/RT/Extension/REST2/Resource/Record/Writable.pm | 29 ++++++++++------
.../REST2/Resource/Role/RequestBodyIsJSON.pm | 2 +-
t/lib/RT/Extension/REST2/Test.pm.in | 8 -----
t/tickets.t | 39 ++++++++++++++++++++++
7 files changed, 69 insertions(+), 36 deletions(-)
- Log -----------------------------------------------------------------
commit 2a5b9dbb104ab540fb1b580bfcbbb0e54302933d
Author: Shawn M Moore <shawn at bestpractical.com>
Date: Tue Dec 13 19:32:35 2016 +0000
Revert "Replace PUT with PATCH"
This reverts commit c8581fb576e41372f0d4e6a4ec8827ed8c142f6b.
Web::Machine doesn't support PATCH yet, and it seems like no other
implementation in another language does either.
https://github.com/webmachine/webmachine-ruby/issues/109
https://github.com/for-GET/http-decision-diagram/issues/35
diff --git a/README b/README
index 2ba9d30..e12bb05 100644
--- a/README
+++ b/README
@@ -22,17 +22,17 @@ USAGE
Currently provided endpoints under /REST/2.0/ are:
GET /ticket/:id
- PATCH /ticket/:id <JSON body>
+ PUT /ticket/:id <JSON body>
DELETE /ticket/:id
Sets ticket status to "deleted".
GET /queue/:id
- PATCH /queue/:id <JSON body>
+ PUT /queue/:id <JSON body>
DELETE /queue/:id
Disables the queue.
GET /user/:id
- PATCH /user/:id <JSON body>
+ PUT /user/:id <JSON body>
DELETE /user/:id
Disables the user.
@@ -41,7 +41,7 @@ USAGE
When a GET request is made, each endpoint returns a JSON representation
of the specified resource, or a 404 if not found.
- When a PATCH request is made, the request body should be a modified copy
+ When a PUT request is made, the request body should be a modified copy
(or partial copy) of the JSON representation of the specified resource,
and the record will be updated.
diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index 0a7090c..a3d6591 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -50,17 +50,17 @@ Add this line:
Currently provided endpoints under C</REST/2.0/> are:
GET /ticket/:id
- PATCH /ticket/:id <JSON body>
+ PUT /ticket/:id <JSON body>
DELETE /ticket/:id
Sets ticket status to "deleted".
GET /queue/:id
- PATCH /queue/:id <JSON body>
+ PUT /queue/:id <JSON body>
DELETE /queue/:id
Disables the queue.
GET /user/:id
- PATCH /user/:id <JSON body>
+ PUT /user/:id <JSON body>
DELETE /user/:id
Disables the user.
@@ -69,7 +69,7 @@ For queues and users, C<:id> may be the numeric id or the unique name.
When a GET request is made, each endpoint returns a JSON representation of the
specified resource, or a 404 if not found.
-When a PATCH request is made, the request body should be a modified copy (or
+When a PUT request is made, the request body should be a modified copy (or
partial copy) of the JSON representation of the specified resource, and the
record will be updated.
diff --git a/lib/RT/Extension/REST2/Resource/Record.pm b/lib/RT/Extension/REST2/Resource/Record.pm
index 9a7e34c..99d7935 100644
--- a/lib/RT/Extension/REST2/Resource/Record.pm
+++ b/lib/RT/Extension/REST2/Resource/Record.pm
@@ -68,17 +68,12 @@ sub last_modified {
return create_date($updated);
}
-sub known_methods {
- my $self = shift;
- return [@{$self->SUPER::known_methods(@_)}, 'PATCH'];
-}
-
sub allowed_methods {
my $self = shift;
my @ok;
- push @ok, 'GET', 'HEAD' if $self->DOES("RT::Extension::REST2::Resource::Record::Readable");
- push @ok, 'DELETE' if $self->DOES("RT::Extension::REST2::Resource::Record::Deletable");
- push @ok, 'PATCH', 'POST' if $self->DOES("RT::Extension::REST2::Resource::Record::Writable");
+ push @ok, 'GET', 'HEAD' if $self->DOES("RT::Extension::REST2::Resource::Record::Readable");
+ push @ok, 'DELETE' if $self->DOES("RT::Extension::REST2::Resource::Record::Deletable");
+ push @ok, 'PUT', 'POST' if $self->DOES("RT::Extension::REST2::Resource::Record::Writable");
return \@ok;
}
diff --git a/lib/RT/Extension/REST2/Resource/Record/Writable.pm b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
index 048af88..65b5d57 100644
--- a/lib/RT/Extension/REST2/Resource/Record/Writable.pm
+++ b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
@@ -30,9 +30,9 @@ sub from_json {
);
my $method = $self->request->method;
- return $method eq 'PATCH' ? $self->update_resource($data) :
- $method eq 'POST' ? $self->create_resource($data) :
- \501 ;
+ return $method eq 'PUT' ? $self->update_resource($data) :
+ $method eq 'POST' ? $self->create_resource($data) :
+ \501 ;
}
sub update_resource {
@@ -69,7 +69,7 @@ sub create_resource {
if ($self->resource_exists) {
return error_as_json(
$self->response,
- \409, "Resource already exists; use PATCH to update");
+ \409, "Resource already exists; use PUT to update");
}
my ($ok, $msg) = $self->create_record($data);
diff --git a/lib/RT/Extension/REST2/Resource/Role/RequestBodyIsJSON.pm b/lib/RT/Extension/REST2/Resource/Role/RequestBodyIsJSON.pm
index dbe38ec..2d571cc 100644
--- a/lib/RT/Extension/REST2/Resource/Role/RequestBodyIsJSON.pm
+++ b/lib/RT/Extension/REST2/Resource/Role/RequestBodyIsJSON.pm
@@ -24,7 +24,7 @@ role {
return $malformed if $malformed;
my $request = $self->request;
- return 0 unless $request->method =~ /^(PUT|POST|PATCH)$/;
+ return 0 unless $request->method =~ /^(PUT|POST)$/;
my $json = eval {
JSON::from_json($request->content)
diff --git a/t/lib/RT/Extension/REST2/Test.pm.in b/t/lib/RT/Extension/REST2/Test.pm.in
index be15105..771e3c9 100644
--- a/t/lib/RT/Extension/REST2/Test.pm.in
+++ b/t/lib/RT/Extension/REST2/Test.pm.in
@@ -66,14 +66,6 @@ sub mech { RT::Extension::REST2::Test::Mechanize->new }
return $json->decode($res->content);
}
-
- # modeled off of LWP::UserAgent::put
- sub patch {
- require HTTP::Request::Common;
- my($self, @parameters) = @_;
- my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
- return $self->request( HTTP::Request::Common::_simple_req( 'PATCH', @parameters ), @suff );
- }
}
1;
commit cd3a717aac7300ac31788ad814ce4ddcb6e4771e
Author: Shawn M Moore <shawn at bestpractical.com>
Date: Tue Dec 13 19:46:00 2016 +0000
Factor an update_record out of update_resource
diff --git a/lib/RT/Extension/REST2/Resource/Record/Writable.pm b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
index 65b5d57..3c14152 100644
--- a/lib/RT/Extension/REST2/Resource/Record/Writable.pm
+++ b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
@@ -35,16 +35,10 @@ sub from_json {
\501 ;
}
-sub update_resource {
+sub update_record {
my $self = shift;
my $data = shift;
- if (not $self->resource_exists) {
- return error_as_json(
- $self->response,
- \404, "Resource does not exist; use POST to create");
- }
-
# XXX TODO: ->Update doesn't handle roles
my @results = $self->record->Update(
ARGSRef => $data,
@@ -56,6 +50,19 @@ sub update_resource {
return;
}
+sub update_resource {
+ my $self = shift;
+ my $data = shift;
+
+ if (not $self->resource_exists) {
+ return error_as_json(
+ $self->response,
+ \404, "Resource does not exist; use POST to create");
+ }
+
+ return $self->update_record($data);
+}
+
sub create_record {
my $self = shift;
my $data = shift;
commit 40d50270b6577177317067a59fe3464cc166329c
Author: Shawn M Moore <shawn at bestpractical.com>
Date: Tue Dec 13 19:48:08 2016 +0000
First cut of ticket update tests
diff --git a/t/tickets.t b/t/tickets.t
index 8ced0c7..bce0484 100644
--- a/t/tickets.t
+++ b/t/tickets.t
@@ -136,4 +136,43 @@ my ($ticket_url, $ticket_id);
like($ticket->{_url}, qr{$rest_base_path/ticket/1$});
}
+# Ticket Update
+{
+ my $payload = $json->encode({
+ Subject => 'Ticket update using REST',
+ Priority => 42,
+ });
+
+ # Rights Test - No ModifyTicket
+ my $res = $mech->put($ticket_url,
+ 'Content' => $payload,
+ 'Content-Type' => 'application/json; charset=utf-8',
+ 'Authorization' => $auth,
+ );
+ TODO: {
+ local $TODO = "RT ->Update isn't introspectable";
+ is($res->code, 403);
+ };
+ is_deeply($mech->json_response, ['Ticket 1: Permission Denied', 'Ticket 1: Permission Denied']);
+
+ $user->PrincipalObj->GrantRight( Right => 'ModifyTicket' );
+
+ $res = $mech->put($ticket_url,
+ 'Content' => $payload,
+ 'Content-Type' => 'application/json; charset=utf-8',
+ 'Authorization' => $auth,
+ );
+ is($res->code, 200);
+ is_deeply($mech->json_response, ["Ticket 1: Priority changed from (no value) to '42'", "Ticket 1: Subject changed from 'Ticket creation using REST' to 'Ticket update using REST'"]);
+
+ $res = $mech->get($ticket_url,
+ 'Authorization' => $auth,
+ );
+ is($res->code, 200);
+
+ my $content = $mech->json_response;
+ is($content->{Subject}, 'Ticket update using REST');
+ is($content->{Priority}, 42);
+}
+
done_testing;
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list