View | Details | Raw Unified | Return to bug 13799
Collapse All | Expand All

(-)a/C4/Installer/PerlDependencies.pm (+10 lines)
Lines 742-747 our $PERL_DEPS = { Link Here
742
        'required' => '0',
742
        'required' => '0',
743
        'min_ver'  => '5.61',
743
        'min_ver'  => '5.61',
744
    },
744
    },
745
    'Mojolicious' => {
746
        'usage'    => 'REST API',
747
        'required' => '0',
748
        'min_ver'  => '6.0',
749
    },
750
    'Swagger2' => {
751
        'usage'    => 'REST API',
752
        'required' => '0',
753
        'min_ver'  => '0.28',
754
    },
745
};
755
};
746
756
747
1;
757
1;
(-)a/Koha/REST.pm (+25 lines)
Line 0 Link Here
1
package Koha::REST;
2
3
use Modern::Perl;
4
use Mojo::Base 'Mojolicious';
5
6
sub startup {
7
    my $self = shift;
8
9
    my $route = $self->routes->under->to(
10
        cb => sub {
11
            my $c = shift;
12
            my $user = $c->param('user');
13
            # Do the authentication stuff here...
14
            $c->stash('user', $user);
15
            return 1;
16
        }
17
    );
18
19
    $self->plugin(Swagger2 => {
20
        route => $route,
21
        url => $self->home->rel_file("rest.json"),
22
    });
23
}
24
25
1;
(-)a/Koha/REST/Borrowers.pm (+29 lines)
Line 0 Link Here
1
package Koha::REST::Borrowers;
2
3
use Modern::Perl;
4
5
use Mojo::Base 'Mojolicious::Controller';
6
7
use C4::Members;
8
9
sub list_borrowers {
10
    my ($c, $args, $cb) = @_;
11
12
    my $borrowers = C4::Members::Search;
13
14
    $c->$cb($borrowers, 200);
15
}
16
17
sub get_borrower {
18
    my ($c, $args, $cb) = @_;
19
20
    my $borrower = C4::Members::GetMemberDetails($args->{borrowernumber});
21
22
    if ($borrower) {
23
        return $c->$cb($borrower, 200);
24
    }
25
26
    $c->$cb({error => "Borrower not found"}, 404);
27
}
28
29
1;
(-)a/rest (+6 lines)
Line 0 Link Here
1
#!/usr/bin/env perl
2
3
use Modern::Perl;
4
5
require Mojolicious::Commands;
6
Mojolicious::Commands->start_app('Koha::REST');
(-)a/rest.json (+103 lines)
Line 0 Link Here
1
{
2
  "swagger": "2.0",
3
  "info": {
4
    "title": "Koha REST API",
5
    "version": "0.1",
6
    "license": {
7
      "name": "GPL v3",
8
      "url": "http://www.gnu.org/licenses/gpl.txt"
9
    },
10
    "contact": {
11
      "name": "Koha Team",
12
      "url": "http://koha-community.org/"
13
    }
14
  },
15
  "basePath": "/",
16
  "paths": {
17
    "/borrowers": {
18
      "get": {
19
        "x-mojo-controller": "Koha::REST::Borrowers",
20
        "operationId": "listBorrowers",
21
        "produces": [
22
          "application/json"
23
        ],
24
        "responses": {
25
          "200": {
26
            "description": "A list of borrowers",
27
            "schema": {
28
              "type": "array",
29
              "items": {
30
                "$ref": "#/definitions/borrower"
31
              }
32
            }
33
          }
34
        }
35
      }
36
    },
37
    "/borrowers/{borrowernumber}": {
38
      "get": {
39
        "x-mojo-controller": "Koha::REST::Borrowers",
40
        "operationId": "getBorrower",
41
        "parameters": [
42
          {
43
            "$ref": "#/parameters/borrowernumberPathParam"
44
          }
45
        ],
46
        "produces": [
47
          "application/json"
48
        ],
49
        "responses": {
50
          "200": {
51
            "description": "A borrower",
52
            "schema": {
53
              "$ref": "#/definitions/borrower"
54
            }
55
          },
56
          "404": {
57
            "description": "Borrower not found",
58
            "schema": {
59
              "$ref": "#/definitions/error"
60
            }
61
          }
62
        }
63
      }
64
    }
65
  },
66
  "definitions": {
67
    "borrower": {
68
      "type": "object",
69
      "properties": {
70
        "borrowernumber": {
71
          "description": "Borrower internal identifier"
72
        },
73
        "cardnumber": {
74
          "description": "library assigned ID number for borrowers"
75
        },
76
        "surname": {
77
          "description": "borrower's last name"
78
        },
79
        "firstname": {
80
          "description": "borrower's first name"
81
        }
82
      }
83
    },
84
    "error": {
85
      "type": "object",
86
      "properties": {
87
        "error": {
88
          "description": "Error message",
89
          "type": "string"
90
        }
91
      }
92
    }
93
  },
94
  "parameters": {
95
    "borrowernumberPathParam": {
96
      "name": "borrowernumber",
97
      "in": "path",
98
      "description": "Internal borrower identifier",
99
      "required": "true",
100
      "type": "integer"
101
    }
102
  }
103
}
(-)a/t/db_dependent/rest/borrowers.t (-1 / +37 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 6;
6
use Test::Mojo;
7
8
use C4::Context;
9
10
use Koha::Database;
11
use Koha::Borrower;
12
13
my $dbh = C4::Context->dbh;
14
$dbh->{AutoCommit} = 0;
15
$dbh->{RaiseError} = 1;
16
17
my $t = Test::Mojo->new('Koha::REST');
18
19
my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
20
my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
21
22
my $borrower = Koha::Borrower->new;
23
$borrower->categorycode( $categorycode );
24
$borrower->branchcode( $branchcode );
25
$borrower->surname("Test Surname");
26
$borrower->store;
27
my $borrowernumber = $borrower->borrowernumber;
28
29
$t->get_ok('/borrowers')
30
  ->status_is(200);
31
32
$t->get_ok("/borrowers/$borrowernumber")
33
  ->status_is(200)
34
  ->json_is('/borrowernumber' => $borrowernumber)
35
  ->json_is('/surname' => "Test Surname");
36
37
$dbh->rollback;

Return to bug 13799