From dbfd6207290d1eee53fddec4c7c3b4aac0b2d47a Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Wed, 8 Apr 2015 18:48:36 +0100 Subject: Bug 1051056: The REST API needs to be versioned so that new changes can be made that do not break compatibility r=dylan,a=glob --- extensions/Example/API/1_0/Resource/Example.pm | 59 ++++++++++++++++++++++++++ extensions/Example/Config.pm | 8 ++++ extensions/Example/Extension.pm | 27 +++++++----- 3 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 extensions/Example/API/1_0/Resource/Example.pm (limited to 'extensions/Example') diff --git a/extensions/Example/API/1_0/Resource/Example.pm b/extensions/Example/API/1_0/Resource/Example.pm new file mode 100644 index 000000000..c4c5f9a32 --- /dev/null +++ b/extensions/Example/API/1_0/Resource/Example.pm @@ -0,0 +1,59 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +package Bugzilla::API::1_0::Resource::Example; + +use 5.10.1; +use strict; +use warnings; +use parent qw(Bugzilla::API::1_0::Resource); +use Bugzilla::Error; + +############# +# Constants # +############# + +use constant READ_ONLY => qw( + hello + throw_an_error +); + +use constant PUBLIC_METHODS => qw( + hello + throw_an_error +); + +sub REST_RESOURCES { + my $rest_resources = [ + qr{^/hello$}, { + GET => { + method => 'hello' + } + }, + qr{^/throw_an_error$}, { + GET => { + method => 'throw_an_error' + } + } + ]; + return $rest_resources; +} + +########### +# Methods # +########### + +# This can be called as Example.hello() from the WebService. +sub hello { + return { + message => 'Hello!' + }; +} + +sub throw_an_error { ThrowUserError('example_my_error') } + +1; diff --git a/extensions/Example/Config.pm b/extensions/Example/Config.pm index e7782ef6c..16708e3f8 100644 --- a/extensions/Example/Config.pm +++ b/extensions/Example/Config.pm @@ -29,4 +29,12 @@ use constant OPTIONAL_MODULES => [ }, ]; +# The map determines which verion of +# the Core API an extension's API modules +# were written to work with. +use constant API_VERSION_MAP => { + '1_0' => '1_0', + '2_0' => '1_0' +}; + __PACKAGE__->NAME; diff --git a/extensions/Example/Extension.pm b/extensions/Example/Extension.pm index a866e85e6..1649957fd 100644 --- a/extensions/Example/Extension.pm +++ b/extensions/Example/Extension.pm @@ -1058,29 +1058,34 @@ sub webservice_rest_resources { my $resources = $args->{'resources'}; # Add a new resource that allows for /rest/example/hello # to call Example.hello - $resources->{'Bugzilla::Extension::Example::WebService'} = [ - qr{^/example/hello$}, { - GET => { - method => 'hello', - } - } - ]; + #$resources->{'Bugzilla::Extension::Example::WebService'} = [ + # qr{^/example/hello$}, { + # GET => { + # method => 'hello', + # } + # } + #]; } -sub webservice_rest_response { +sub webservice_rest_result { my ($self, $args) = @_; - my $rpc = $args->{'rpc'}; - my $result = $args->{'result'}; - my $response = $args->{'response'}; + my $result = $args->{'result'}; # Convert a list of bug hashes to a single bug hash if only one is # being returned. if (ref $$result eq 'HASH' && exists $$result->{'bugs'} + && ref $$result->{'bugs'} eq 'ARRAY' && scalar @{ $$result->{'bugs'} } == 1) { $$result = $$result->{'bugs'}->[0]; } } +sub webservice_rest_response { + my ($self, $args) = @_; + my $response = $args->{'response'}; + $response->header('X-Example-Header', 'This is an example header'); +} + # This must be the last line of your extension. __PACKAGE__->NAME; -- cgit v1.2.3-24-g4f1b