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

(-)a/C4/Installer/PerlDependencies.pm (+9 lines)
Lines 766-771 our $PERL_DEPS = { Link Here
766
        'usage'    => 'Tests',
766
        'usage'    => 'Tests',
767
        'required' => '0',
767
        'required' => '0',
768
        'min_ver'  => '0.0.3',
768
        'min_ver'  => '0.0.3',
769
    'Mojolicious' => {
770
        'usage'    => 'REST API',
771
        'required' => '0',
772
        'min_ver'  => '5.54',
773
    },
774
    'Swagger2' => {
775
        'usage'    => 'REST API',
776
        'required' => '0',
777
        'min_ver'  => '0.28',
769
    },
778
    },
770
};
779
};
771
780
(-)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 310-315 sub as_list { Link Here
310
    return wantarray ? @objects : \@objects;
310
    return wantarray ? @objects : \@objects;
311
}
311
}
312
312
313
=head3 Koha::Objects->unblessed
314
315
Returns an unblessed representation of objects.
316
317
=cut
318
319
sub unblessed {
320
    my ($self) = @_;
321
322
    return [ map { $_->unblessed } $self->as_list ];
323
}
324
313
=head3 Koha::Objects->_wrap
325
=head3 Koha::Objects->_wrap
314
326
315
wraps the DBIC object in a corresponding Koha object
327
wraps the DBIC object in a corresponding Koha object
(-)a/Koha/REST/V1.pm (+25 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
    $self->plugin(Swagger2 => {
20
        route => $route,
21
        url => $self->home->rel_file("api/v1/swagger.json"),
22
    });
23
}
24
25
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/script.cgi (+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": "/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 (+19 lines)
Lines 216-218 Link Here
216
     RewriteRule ^/issn/([^\/]*)/?$ /search?q=issn:$1 [PT]
216
     RewriteRule ^/issn/([^\/]*)/?$ /search?q=issn:$1 [PT]
217
   </IfModule>
217
   </IfModule>
218
</VirtualHost>
218
</VirtualHost>
219
220
<VirtualHost __WEBSERVER_IP__:__WEBSERVER_PORT__>
221
   ServerAdmin __WEBMASTER_EMAIL__
222
   DocumentRoot __INTRANET_CGI_DIR__/api
223
   ServerName api.__WEBSERVER_HOST__:__WEBSERVER_PORT__
224
   SetEnv KOHA_CONF "__KOHA_CONF_DIR__/koha-conf.xml"
225
   SetEnv PERL5LIB "__PERL_MODULE_DIR__"
226
227
  <Directory __INTRANET_CGI_DIR__/api>
228
    Options +ExecCGI +FollowSymlinks
229
    AddHandler cgi-script .cgi
230
231
    RewriteEngine on
232
    RewriteCond %{REQUEST_FILENAME} !-f
233
    RewriteCond %{REQUEST_FILENAME} !-d
234
    RewriteCond %{DOCUMENT_ROOT}/$1/script.cgi -f
235
    RewriteRule ^(.*?)/.* $1/script.cgi/$0 [L]
236
  </Directory>
237
</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('/v1/borrowers')
30
  ->status_is(200);
31
32
$t->get_ok("/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