From 46c8878373cd58875c06c58af80c86eb3c5e4d9e Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 4 Mar 2015 16:46:33 +0100 Subject: [PATCH] Bug 13799: REST API proof of concept with Mojolicious/Swagger2 1/ Install Mojolicious Perl module 2/ Launch server with: $ morbo ./rest Actual routes are: /borrowers Return a list of all borrowers in Koha /borrowers/{borrowernumber} Return the borrower identified by {borrowernumber} (eg. /borrowers/1) There is a test file you can run with: $ prove t/db_dependent/rest/borrowers.t --- C4/Installer/PerlDependencies.pm | 10 ++++ Koha/REST.pm | 25 +++++++++ Koha/REST/Borrowers.pm | 29 +++++++++++ rest | 6 +++ rest.json | 103 ++++++++++++++++++++++++++++++++++++++ t/db_dependent/rest/borrowers.t | 37 ++++++++++++++ 6 files changed, 210 insertions(+) create mode 100644 Koha/REST.pm create mode 100644 Koha/REST/Borrowers.pm create mode 100644 rest create mode 100644 rest.json create mode 100644 t/db_dependent/rest/borrowers.t diff --git a/C4/Installer/PerlDependencies.pm b/C4/Installer/PerlDependencies.pm index 4b2067e..eda36dd 100644 --- a/C4/Installer/PerlDependencies.pm +++ b/C4/Installer/PerlDependencies.pm @@ -732,6 +732,16 @@ our $PERL_DEPS = { 'required' => '0', 'min_ver' => '0.31', }, + 'Mojolicious' => { + 'usage' => 'REST API', + 'required' => '0', + 'min_ver' => '6.0', + }, + 'Swagger2' => { + 'usage' => 'REST API', + 'required' => '0', + 'min_ver' => '0.28', + }, }; 1; diff --git a/Koha/REST.pm b/Koha/REST.pm new file mode 100644 index 0000000..b657b20 --- /dev/null +++ b/Koha/REST.pm @@ -0,0 +1,25 @@ +package Koha::REST; + +use Modern::Perl; +use Mojo::Base 'Mojolicious'; + +sub startup { + my $self = shift; + + my $route = $self->routes->under->to( + cb => sub { + my $c = shift; + my $user = $c->param('user'); + # Do the authentication stuff here... + $c->stash('user', $user); + return 1; + } + ); + + $self->plugin(Swagger2 => { + route => $route, + url => $self->home->rel_file("rest.json"), + }); +} + +1; diff --git a/Koha/REST/Borrowers.pm b/Koha/REST/Borrowers.pm new file mode 100644 index 0000000..438cbbc --- /dev/null +++ b/Koha/REST/Borrowers.pm @@ -0,0 +1,29 @@ +package Koha::REST::Borrowers; + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use C4::Members; + +sub list_borrowers { + my ($c, $args, $cb) = @_; + + my $borrowers = C4::Members::Search; + + $c->$cb($borrowers, 200); +} + +sub get_borrower { + my ($c, $args, $cb) = @_; + + my $borrower = C4::Members::GetMemberDetails($args->{borrowernumber}); + + if ($borrower) { + return $c->$cb($borrower, 200); + } + + $c->$cb({error => "Borrower not found"}, 404); +} + +1; diff --git a/rest b/rest new file mode 100644 index 0000000..520733f --- /dev/null +++ b/rest @@ -0,0 +1,6 @@ +#!/usr/bin/env perl + +use Modern::Perl; + +require Mojolicious::Commands; +Mojolicious::Commands->start_app('Koha::REST'); diff --git a/rest.json b/rest.json new file mode 100644 index 0000000..849a518 --- /dev/null +++ b/rest.json @@ -0,0 +1,103 @@ +{ + "swagger": "2.0", + "info": { + "title": "Koha REST API", + "version": "0.1", + "license": { + "name": "GPL v3", + "url": "http://www.gnu.org/licenses/gpl.txt" + }, + "contact": { + "name": "Koha Team", + "url": "http://koha-community.org/" + } + }, + "basePath": "/", + "paths": { + "/borrowers": { + "get": { + "x-mojo-controller": "Koha::REST::Borrowers", + "operationId": "listBorrowers", + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A list of borrowers", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/borrower" + } + } + } + } + } + }, + "/borrowers/{borrowernumber}": { + "get": { + "x-mojo-controller": "Koha::REST::Borrowers", + "operationId": "getBorrower", + "parameters": [ + { + "$ref": "#/parameters/borrowernumberPathParam" + } + ], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A borrower", + "schema": { + "$ref": "#/definitions/borrower" + } + }, + "404": { + "description": "Borrower not found", + "schema": { + "$ref": "#/definitions/error" + } + } + } + } + } + }, + "definitions": { + "borrower": { + "type": "object", + "properties": { + "borrowernumber": { + "description": "Borrower internal identifier" + }, + "cardnumber": { + "description": "library assigned ID number for borrowers" + }, + "surname": { + "description": "borrower's last name" + }, + "firstname": { + "description": "borrower's first name" + } + } + }, + "error": { + "type": "object", + "properties": { + "error": { + "description": "Error message", + "type": "string" + } + } + } + }, + "parameters": { + "borrowernumberPathParam": { + "name": "borrowernumber", + "in": "path", + "description": "Internal borrower identifier", + "required": "true", + "type": "integer" + } + } +} diff --git a/t/db_dependent/rest/borrowers.t b/t/db_dependent/rest/borrowers.t new file mode 100644 index 0000000..73d274d --- /dev/null +++ b/t/db_dependent/rest/borrowers.t @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use Modern::Perl; + +use Test::More tests => 6; +use Test::Mojo; + +use C4::Context; + +use Koha::Database; +use Koha::Borrower; + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +my $t = Test::Mojo->new('Koha::REST'); + +my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode(); +my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode(); + +my $borrower = Koha::Borrower->new; +$borrower->categorycode( $categorycode ); +$borrower->branchcode( $branchcode ); +$borrower->surname("Test Surname"); +$borrower->store; +my $borrowernumber = $borrower->borrowernumber; + +$t->get_ok('/borrowers') + ->status_is(200); + +$t->get_ok("/borrowers/$borrowernumber") + ->status_is(200) + ->json_is('/borrowernumber' => $borrowernumber) + ->json_is('/surname' => "Test Surname"); + +$dbh->rollback; -- 1.7.10.4