Browse Source
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 All API stuff is in /api/v1 (except Perl modules) So we have: /api/v1/script.cgi CGI script /api/v1/swagger.json Swagger specification Change both OPAC and Intranet VirtualHosts to access the API, so we have: http://OPAC/api/v1/swagger.json Swagger specification http://OPAC/api/v1/{path} API endpoint http://INTRANET/api/v1/swagger.json Swagger specification http://INTRANET/api/v1/{path} API endpoint Add a (disabled) virtual host in Apache configuration api.HOSTNAME, so we have: http://api.HOSTNAME/api/v1/swagger.json Swagger specification http://api.HOSTNAME/api/v1/{path} API endpoint Add 'unblessed' subroutines to both Koha::Objects and Koha::Object to be able to pass it to Mojolicious Test plan: 1/ Install Perl modules Mojolicious and Swagger2 2/ perl Makefile.PL 3/ make && make install 4/ Change etc/koha-httpd.conf and copy it to the right place if needed 5/ Reload Apache 6/ Check that http://(OPAC|INTRANET)/api/v1/borrowers and http://(OPAC|INTRANET)/api/v1/borrowers/{borrowernumber} works Optionally, you could verify that http://(OPAC|INTRANET)/vX/borrowers (where X is an integer greater than 1) returns a 404 error Signed-off-by: Alex Arnaud <alex.arnaud@biblibre.com> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>3.22.x
Julian Maurice
9 years ago
committed by
Tomas Cohen Arazi
9 changed files with 298 additions and 0 deletions
@ -0,0 +1,28 @@ |
|||
package Koha::REST::V1; |
|||
|
|||
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; |
|||
} |
|||
); |
|||
|
|||
# Force charset=utf8 in Content-Type header for JSON responses |
|||
$self->types->type(json => 'application/json; charset=utf8'); |
|||
|
|||
$self->plugin(Swagger2 => { |
|||
route => $route, |
|||
url => $self->home->rel_file("api/v1/swagger.json"), |
|||
}); |
|||
} |
|||
|
|||
1; |
@ -0,0 +1,29 @@ |
|||
package Koha::REST::V1::Borrowers; |
|||
|
|||
use Modern::Perl; |
|||
|
|||
use Mojo::Base 'Mojolicious::Controller'; |
|||
|
|||
use Koha::Borrowers; |
|||
|
|||
sub list_borrowers { |
|||
my ($c, $args, $cb) = @_; |
|||
|
|||
my $borrowers = Koha::Borrowers->search; |
|||
|
|||
$c->$cb($borrowers->unblessed, 200); |
|||
} |
|||
|
|||
sub get_borrower { |
|||
my ($c, $args, $cb) = @_; |
|||
|
|||
my $borrower = Koha::Borrowers->find($args->{borrowernumber}); |
|||
|
|||
if ($borrower) { |
|||
return $c->$cb($borrower->unblessed, 200); |
|||
} |
|||
|
|||
$c->$cb({error => "Borrower not found"}, 404); |
|||
} |
|||
|
|||
1; |
@ -0,0 +1,6 @@ |
|||
#!/usr/bin/env perl |
|||
|
|||
use Modern::Perl; |
|||
|
|||
require Mojolicious::Commands; |
|||
Mojolicious::Commands->start_app('Koha::REST::V1'); |
@ -0,0 +1,108 @@ |
|||
{ |
|||
"swagger": "2.0", |
|||
"info": { |
|||
"title": "Koha REST API", |
|||
"version": "1", |
|||
"license": { |
|||
"name": "GPL v3", |
|||
"url": "http://www.gnu.org/licenses/gpl.txt" |
|||
}, |
|||
"contact": { |
|||
"name": "Koha Team", |
|||
"url": "http://koha-community.org/" |
|||
} |
|||
}, |
|||
"basePath": "/api/v1", |
|||
"paths": { |
|||
"/borrowers": { |
|||
"get": { |
|||
"x-mojo-controller": "Koha::REST::V1::Borrowers", |
|||
"operationId": "listBorrowers", |
|||
"tags": ["borrowers"], |
|||
"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::V1::Borrowers", |
|||
"operationId": "getBorrower", |
|||
"tags": ["borrowers"], |
|||
"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": { |
|||
"$ref": "#/definitions/borrowernumber" |
|||
}, |
|||
"cardnumber": { |
|||
"description": "library assigned ID number for borrowers" |
|||
}, |
|||
"surname": { |
|||
"description": "borrower's last name" |
|||
}, |
|||
"firstname": { |
|||
"description": "borrower's first name" |
|||
} |
|||
} |
|||
}, |
|||
"borrowernumber": { |
|||
"description": "Borrower internal identifier" |
|||
}, |
|||
"error": { |
|||
"type": "object", |
|||
"properties": { |
|||
"error": { |
|||
"description": "Error message", |
|||
"type": "string" |
|||
} |
|||
} |
|||
} |
|||
}, |
|||
"parameters": { |
|||
"borrowernumberPathParam": { |
|||
"name": "borrowernumber", |
|||
"in": "path", |
|||
"description": "Internal borrower identifier", |
|||
"required": "true", |
|||
"type": "integer" |
|||
} |
|||
} |
|||
} |
@ -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::V1'); |
|||
|
|||
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('/api/v1/borrowers') |
|||
->status_is(200); |
|||
|
|||
$t->get_ok("/api/v1/borrowers/$borrowernumber") |
|||
->status_is(200) |
|||
->json_is('/borrowernumber' => $borrowernumber) |
|||
->json_is('/surname' => "Test Surname"); |
|||
|
|||
$dbh->rollback; |
Loading…
Reference in new issue