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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
| #!/usr/bin/env perl
# 言語: perl
# バージョン: 5.36以上
# 依存: Moo, JSON, Time::HiRes(cpanmでインストール)
use v5.36;
package ResponseRole {
use Moo::Role;
requires 'render';
}
package SuccessResponse {
use Moo;
use JSON qw(encode_json);
with 'ResponseRole';
has data => (is => 'ro', required => 1);
sub render($self) {
my $body = encode_json({
success => JSON::true,
message => 'リクエストが正常に処理されました',
data => $self->data,
});
return "HTTP/1.1 200 OK\nContent-Type: application/json\n\n$body";
}
}
package ErrorResponse {
use Moo;
use JSON qw(encode_json);
with 'ResponseRole';
has status => (is => 'ro', required => 1);
has error_code => (is => 'ro', required => 1);
has message => (is => 'ro', required => 1);
sub render($self) {
my $body = encode_json({
success => JSON::false,
error => $self->message,
code => $self->error_code,
});
return sprintf(
"HTTP/1.1 %s\nContent-Type: application/json\n\n%s",
$self->status, $body,
);
}
}
package RateLimitResponse {
use Moo;
use JSON qw(encode_json);
with 'ResponseRole';
has retry_after => (is => 'ro', default => sub { 60 });
sub render($self) {
my $body = encode_json({
success => JSON::false,
error => 'リクエスト数が上限を超えました',
code => 'RATE_LIMIT_EXCEEDED',
retry_after => $self->retry_after,
});
return sprintf(
"HTTP/1.1 429 Too Many Requests\nContent-Type: application/json\nRetry-After: %d\n\n%s",
$self->retry_after, $body,
);
}
}
package ServerErrorResponse {
use Moo;
use JSON qw(encode_json);
with 'ResponseRole';
has error_id => (is => 'ro', required => 1);
sub render($self) {
my $body = encode_json({
success => JSON::false,
error => 'サーバー内部エラーが発生しました',
code => 'INTERNAL_ERROR',
error_id => $self->error_id,
});
return "HTTP/1.1 500 Internal Server Error\nContent-Type: application/json\n\n$body";
}
}
package Scenario {
use Moo;
use Time::HiRes qw(gettimeofday tv_interval);
sub create_response($self) {
die "create_response must be implemented by subclass";
}
sub scenario_name($self) {
my $class = ref($self);
$class =~ s/Scenario$//;
return $class;
}
sub log_request($self) {
my $name = $self->scenario_name;
my $timestamp = localtime();
say STDERR "[$timestamp] Processing: $name";
}
sub log_complete($self, $elapsed) {
my $name = $self->scenario_name;
my $timestamp = localtime();
say STDERR "[$timestamp] Completed: $name (${elapsed}ms)";
}
sub execute($self) {
my $start = [gettimeofday];
$self->log_request;
my $response = $self->create_response;
my $elapsed = int(tv_interval($start) * 1000);
$self->log_complete($elapsed);
return $response->render;
}
}
package SuccessScenario {
use Moo;
extends 'Scenario';
sub create_response($self) {
return SuccessResponse->new(
data => { id => 1, name => 'サンプルアイテム' },
);
}
}
package NotFoundScenario {
use Moo;
extends 'Scenario';
sub create_response($self) {
return ErrorResponse->new(
status => '404 Not Found',
error_code => 'NOT_FOUND',
message => 'リソースが見つかりません',
);
}
}
package RateLimitScenario {
use Moo;
extends 'Scenario';
has retry_after => (is => 'ro', default => sub { 60 });
sub create_response($self) {
return RateLimitResponse->new(
retry_after => $self->retry_after,
);
}
}
package ServerErrorScenario {
use Moo;
extends 'Scenario';
sub create_response($self) {
my $error_id = sprintf("ERR-%06d", int(rand(1000000)));
return ServerErrorResponse->new(error_id => $error_id);
}
}
for my $scenario_class (qw(
SuccessScenario
NotFoundScenario
RateLimitScenario
ServerErrorScenario
)) {
say "=== $scenario_class ===";
my $scenario = $scenario_class->new;
say $scenario->execute;
say "";
}
|