/
P6WAPI.pm6
71 lines (50 loc) · 1.71 KB
/
P6WAPI.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use v6;
unit role Cofra::Web::P6WAPI;
use Cofra::Web::Response;
use Cofra::Web::Request::P6WAPI;
use Cofra::Web::Response::P6WAPI;
use X::Cofra::Web::Error;
method app { ... }
method log-error { ... }
method p6wapi-request-response-dispatch(%env --> List) {
try {
my $req = Cofra::Web::Request::P6WAPI.new(:%env, :$.app);
# Just in case someone has some middleware that needs these
%env<cofra.app> = $.app;
%env<cofra.web> = self;
%env<cofra.request> = $req;
my $res = self.request-response-dispatch($req);
CATCH {
when X::Cofra::Web::Error {
$res = Cofra::Web::Response::P6WAPI.from-error($_);
return $res.finalize;
}
default {
self.log-error($_);
$res = Cofra::Web::Response::P6WAPI.from-error(
X::Cofra::Web::Error.new(self, $req)
);
return $res.finalize;
}
}
$res.finalize;
}
}
=begin pod
=head1 NAME
Cofra::Web::P6WAPI - not yet documented
=head1 DESCRIPTION
This provides the tooling to wrap the dispatch methods of L<Cofra::Web> and
adapts those methods to work with a P6WAPI server.
=head1 METHODS
=head2 method p6wapi-request-response-dispatch
method p6wapi-request-response-disaptch(%env --> List)
This is a partial implementation of a P6WAPI request-response method. The
difference is that this is a method requiring an invocant and it does not return
asynchronously. This code will turn it into a proper P6WAPI request-response method:
sub (%env --> Promise) {
start {
$web.p6wapi-request-response-dispatch(%env);
}
}
=end pod