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

(-)a/C4/Installer/PerlDependencies.pm (+10 lines)
Lines 762-767 our $PERL_DEPS = { Link Here
762
        'required' => '0',
762
        'required' => '0',
763
        'min_ver'  => '0.614',
763
        'min_ver'  => '0.614',
764
    },
764
    },
765
    'Mojolicious' => {
766
        'usage'    => 'REST API',
767
        'required' => '0',
768
        'min_ver'  => '5.54',
769
    },
770
    'Swagger2' => {
771
        'usage'    => 'REST API',
772
        'required' => '0',
773
        'min_ver'  => '0.28',
774
    },
765
};
775
};
766
776
767
1;
777
1;
(-)a/Koha/Object.pm (+12 lines)
Lines 207-212 sub id { Link Here
207
    return $id;
207
    return $id;
208
}
208
}
209
209
210
=head3 $object->unblessed();
211
212
Returns an unblessed representation of object.
213
214
=cut
215
216
sub unblessed {
217
    my ($self) = @_;
218
219
    return { $self->_result->get_columns };
220
}
221
210
=head3 $object->_result();
222
=head3 $object->_result();
211
223
212
Returns the internal DBIC Row object
224
Returns the internal DBIC Row object
(-)a/Koha/Objects.pm (+12 lines)
Lines 181-186 sub as_list { Link Here
181
    return wantarray ? @objects : \@objects;
181
    return wantarray ? @objects : \@objects;
182
}
182
}
183
183
184
=head3 Koha::Objects->unblessed
185
186
Returns an unblessed representation of objects.
187
188
=cut
189
190
sub unblessed {
191
    my ($self) = @_;
192
193
    return [ map { $_->unblessed } $self->as_list ];
194
}
195
184
=head3 Koha::Objects->_wrap
196
=head3 Koha::Objects->_wrap
185
197
186
wraps the DBIC object in a corresponding Koha object
198
wraps the DBIC object in a corresponding Koha object
(-)a/Koha/REST/V1.pm (+28 lines)
Line 0 Link Here
1
package Koha::REST::V1;
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
    # Force charset=utf8 in Content-Type header for JSON responses
20
    $self->types->type(json => 'application/json; charset=utf8');
21
22
    $self->plugin(Swagger2 => {
23
        route => $route,
24
        url => $self->home->rel_file("api/v1/swagger.json"),
25
    });
26
}
27
28
1;
(-)a/Koha/REST/V1/Borrowers.pm (+29 lines)
Line 0 Link Here
1
package Koha::REST::V1::Borrowers;
2
3
use Modern::Perl;
4
5
use Mojo::Base 'Mojolicious::Controller';
6
7
use Koha::Borrowers;
8
9
sub list_borrowers {
10
    my ($c, $args, $cb) = @_;
11
12
    my $borrowers = Koha::Borrowers->search;
13
14
    $c->$cb($borrowers->unblessed, 200);
15
}
16
17
sub get_borrower {
18
    my ($c, $args, $cb) = @_;
19
20
    my $borrower = Koha::Borrowers->find($args->{borrowernumber});
21
22
    if ($borrower) {
23
        return $c->$cb($borrower->unblessed, 200);
24
    }
25
26
    $c->$cb({error => "Borrower not found"}, 404);
27
}
28
29
1;
(-)a/api/v1/app.pl (+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::V1');
(-)a/api/v1/swagger.json (+108 lines)
Line 0 Link Here
1
{
2
  "swagger": "2.0",
3
  "info": {
4
    "title": "Koha REST API",
5
    "version": "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": "/api/v1",
16
  "paths": {
17
    "/borrowers": {
18
      "get": {
19
        "x-mojo-controller": "Koha::REST::V1::Borrowers",
20
        "operationId": "listBorrowers",
21
        "tags": ["borrowers"],
22
        "produces": [
23
          "application/json"
24
        ],
25
        "responses": {
26
          "200": {
27
            "description": "A list of borrowers",
28
            "schema": {
29
              "type": "array",
30
              "items": {
31
                "$ref": "#/definitions/borrower"
32
              }
33
            }
34
          }
35
        }
36
      }
37
    },
38
    "/borrowers/{borrowernumber}": {
39
      "get": {
40
        "x-mojo-controller": "Koha::REST::V1::Borrowers",
41
        "operationId": "getBorrower",
42
        "tags": ["borrowers"],
43
        "parameters": [
44
          {
45
            "$ref": "#/parameters/borrowernumberPathParam"
46
          }
47
        ],
48
        "produces": [
49
          "application/json"
50
        ],
51
        "responses": {
52
          "200": {
53
            "description": "A borrower",
54
            "schema": {
55
              "$ref": "#/definitions/borrower"
56
            }
57
          },
58
          "404": {
59
            "description": "Borrower not found",
60
            "schema": {
61
              "$ref": "#/definitions/error"
62
            }
63
          }
64
        }
65
      }
66
    }
67
  },
68
  "definitions": {
69
    "borrower": {
70
      "type": "object",
71
      "properties": {
72
        "borrowernumber": {
73
          "$ref": "#/definitions/borrowernumber"
74
        },
75
        "cardnumber": {
76
          "description": "library assigned ID number for borrowers"
77
        },
78
        "surname": {
79
          "description": "borrower's last name"
80
        },
81
        "firstname": {
82
          "description": "borrower's first name"
83
        }
84
      }
85
    },
86
    "borrowernumber": {
87
      "description": "Borrower internal identifier"
88
    },
89
    "error": {
90
      "type": "object",
91
      "properties": {
92
        "error": {
93
          "description": "Error message",
94
          "type": "string"
95
        }
96
      }
97
    }
98
  },
99
  "parameters": {
100
    "borrowernumberPathParam": {
101
      "name": "borrowernumber",
102
      "in": "path",
103
      "description": "Internal borrower identifier",
104
      "required": "true",
105
      "type": "integer"
106
    }
107
  }
108
}
(-)a/etc/koha-httpd.conf (+56 lines)
Lines 112-117 Link Here
112
     RewriteRule ^/bib/([^\/]*)/?$ /cgi-bin/koha/opac-detail\.pl?bib=$1 [PT]
112
     RewriteRule ^/bib/([^\/]*)/?$ /cgi-bin/koha/opac-detail\.pl?bib=$1 [PT]
113
     RewriteRule ^/isbn/([^\/]*)/?$ /search?q=isbn:$1 [PT]
113
     RewriteRule ^/isbn/([^\/]*)/?$ /search?q=isbn:$1 [PT]
114
     RewriteRule ^/issn/([^\/]*)/?$ /search?q=issn:$1 [PT]
114
     RewriteRule ^/issn/([^\/]*)/?$ /search?q=issn:$1 [PT]
115
116
     # REST API configuration
117
     Alias "/api" "__OPAC_CGI_DIR__/api"
118
     <Directory __OPAC_CGI_DIR__/api>
119
       Options +ExecCGI +FollowSymlinks
120
       AddHandler cgi-script .pl
121
122
       RewriteEngine On
123
       RewriteBase /api/
124
       RewriteCond %{REQUEST_FILENAME} !-f
125
       RewriteCond %{REQUEST_FILENAME} !-d
126
       RewriteCond %{DOCUMENT_ROOT}/../api/$1/app.pl -f
127
       RewriteRule ^(.*?)/.* $1/app.pl/api/$0 [L]
128
     </Directory>
115
   </IfModule>
129
   </IfModule>
116
</VirtualHost>
130
</VirtualHost>
117
131
Lines 214-218 Link Here
214
     RewriteRule ^/bib/([^\/]*)/?$ /cgi-bin/koha/detail\.pl?bib=$1 [PT]
228
     RewriteRule ^/bib/([^\/]*)/?$ /cgi-bin/koha/detail\.pl?bib=$1 [PT]
215
     RewriteRule ^/isbn/([^\/]*)/?$ /search?q=isbn:$1 [PT]
229
     RewriteRule ^/isbn/([^\/]*)/?$ /search?q=isbn:$1 [PT]
216
     RewriteRule ^/issn/([^\/]*)/?$ /search?q=issn:$1 [PT]
230
     RewriteRule ^/issn/([^\/]*)/?$ /search?q=issn:$1 [PT]
231
232
233
     # REST API configuration
234
     Alias "/api" "__INTRANET_CGI_DIR__/api"
235
     <Directory __INTRANET_CGI_DIR__/api>
236
       Options +ExecCGI +FollowSymlinks
237
       AddHandler cgi-script .pl
238
239
       RewriteEngine On
240
       RewriteBase /api/
241
       RewriteCond %{REQUEST_FILENAME} !-f
242
       RewriteCond %{REQUEST_FILENAME} !-d
243
       RewriteCond %{DOCUMENT_ROOT}/../api/$1/app.pl -f
244
       RewriteRule ^(.*?)/.* $1/app.pl/api/$0 [L]
245
     </Directory>
217
   </IfModule>
246
   </IfModule>
218
</VirtualHost>
247
</VirtualHost>
248
249
# Uncomment this VirtualHost to enable API access through
250
# api.__WEBSERVER_HOST__:__WEBSERVER_PORT__
251
#<VirtualHost __WEBSERVER_IP__:__WEBSERVER_PORT__>
252
#  ServerAdmin __WEBMASTER_EMAIL__
253
#  DocumentRoot __INTRANET_CGI_DIR__/api
254
#  ServerName api.__WEBSERVER_HOST__:__WEBSERVER_PORT__
255
#  SetEnv KOHA_CONF "__KOHA_CONF_DIR__/koha-conf.xml"
256
#  SetEnv PERL5LIB "__PERL_MODULE_DIR__"
257
#  ErrorLog __LOG_DIR__/koha-api-error_log
258
#
259
#  <IfModule mod_rewrite.c>
260
#    <Directory __INTRANET_CGI_DIR__/api>
261
#      Options +ExecCGI +FollowSymlinks
262
#      AddHandler cgi-script .pl
263
#
264
#      RewriteEngine on
265
#
266
#      RewriteRule ^api/(.*) $1 [L]
267
#
268
#      RewriteCond %{REQUEST_FILENAME} !-f
269
#      RewriteCond %{REQUEST_FILENAME} !-d
270
#      RewriteCond %{DOCUMENT_ROOT}/$1/app.pl -f
271
#      RewriteRule ^(.*?)/.* $1/app.pl/api/$0 [L]
272
#    </Directory>
273
#  </IfModule>
274
#</VirtualHost>
(-)a/t/db_dependent/api/v1/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::V1');
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('/api/v1/borrowers')
30
  ->status_is(200);
31
32
$t->get_ok("/api/v1/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