-
Notifications
You must be signed in to change notification settings - Fork 3
/
Controller.pm6
executable file
·70 lines (63 loc) · 2.11 KB
/
Controller.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
use v6;
use PowerNap;
role PowerNap::Controller {
method verb-get(:$request --> PowerNap::Result) {
result-err 501, 'GET not supported for this endpoint.'
}
method verb-post(:$request --> PowerNap::Result) {
result-err 501, 'POST not supported for this endpoint.'
}
method verb-put(:$request --> PowerNap::Result) {
result-err 501, 'PUT not supported for this endpoint.'
}
method verb-patch(:$request --> PowerNap::Result) {
result-err 501, 'PATCH not supported for this endpoint.'
}
method verb-delete(:$request --> PowerNap::Result) {
result-err 501, 'DELETE not supported for this endpoint.'
}
method dispatch-verb(PowerNap::Verb $verb, Map $request --> PowerNap::Result) {
try {
CATCH {
when X::TypeCheck {
warn $_.perl;
return result-err(501, "Type error, this endpoint cannot accept the arguments provided. Please refer to your API documentation.")
}
when X::AdHoc {
if .payload.starts-with('Required named parameter') {
# Hack for handdling missing named parameters in a signiture
return result-err(501, "Missing property error.\n{ .payload }.")
}
else {
warn $_.perl;
return result-err(500, "This endpoint encountered an error when trying to service your request.")
}
}
default {
warn $_.perl;
return result-err(500, "This endpoint encountered an error when trying to service your request.")
}
}
given $verb {
when PowerNap::Verb::GET {
self.verb-get: |$request
}
when PowerNap::Verb::POST {
self.verb-post: |$request
}
when PowerNap::Verb::PUT {
self.verb-put: |$request
}
when PowerNap::Verb::PATCH {
self.verb-patch: |$request
}
when PowerNap::Verb::DELETE {
self.verb-delete: |$request
}
default {
result-err 501, 'Unsupported verb: { $verb.perl }';
}
}
}
}
}