-
Notifications
You must be signed in to change notification settings - Fork 3
/
P6WAPI.pm6
78 lines (53 loc) · 1.98 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
72
73
74
75
76
77
78
use v6;
use Smack::Response;
use Cofra::Web::Response;
use HTTP::Headers;
use X::Cofra::Web::Error;
unit class Cofra::Web::Response::P6WAPI does Cofra::Web::Response;
has Smack::Response $.response handles <
status headers header Content-Length Content-Type body
redirect finalize to-app
>;
method from-error(Cofra::Web::Response::P6WAPI:U: Exception $error --> Cofra::Web::Response::P6WAPI) {
# TODO .from-error really ought to be pulled up into Cofra::Web::Response.
my $response = do given $error {
when X::Cofra::Web::Error {
my $headers = HTTP::Headers.new;
my @body = '<h1>', .status-message, '</h1>';
$headers.Content-Type = 'text/html; charset=ascii';
$headers.Content-Length = @body.join.chars;
Smack::Response.new(
status => .status,
:$headers, :@body,
);
}
default {
my @body = "Internal Server Error";
my $headers = HTTP::Headers.new;
$headers.Content-Type = 'text/plain; charset=ascii';
$headers.Content-Length = @body.join.chars;
Smack::Response.new(
status => 500,
:$headers, :@body,
);
}
}
Cofra::Web::Response::P6WAPI.new(:$response);
}
=begin pod
=head1 NAME
Cofra::Web::Response::P6WAPI - web responses to P6WAPI servers
=head1 DESCRIPTION
This implements the L<Cofra::Web::Response> interface for P6WAPI servers.
=head1 METHOD
=head2 method response
has Smack::Response $.response handles <
status headers header Content-Length Content-Type body
redirect finalize to-app
>;
Provides the implementation of most the methods in this class by delegating them
to a wrapped L<Smack::Response> object.
=head2 method from-error
method from-error(Cofra::Web::Response::P6WAPI:U: Exception $error --> Cofra::Web::Response::P6WAPI)
This can generate a P6WAPI response from any exception.
=end pod