MT#3997 API: move req/res log functions to Role.

agranig/rest
Andreas Granig 12 years ago
parent 81bd123070
commit beb93987eb

@ -37,11 +37,11 @@ __PACKAGE__->config(
action_roles => [qw(HTTPMethods)],
);
sub auto :Allow {
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$c->log->debug("++++++++++++++++ request body: " . $c->stash->{body});
$self->log_request($c);
}
sub GET :Allow {
@ -234,16 +234,8 @@ sub valid_id : Private {
sub end : Private {
my ($self, $c) = @_;
$c->forward(qw(Controller::Root render));
$c->response->content_type('')
if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way
if (@{ $c->error }) {
my $msg = join ', ', @{ $c->error };
$c->log->error($msg);
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error");
$c->clear_errors;
}
$c->log->debug("++++++++++++++++ response body: " . $c->response->body // '');
$self->log_response($c);
}
# vim: set tabstop=4 expandtab:

@ -41,10 +41,11 @@ __PACKAGE__->config(
action_roles => [qw(HTTPMethods)],
);
sub auto :Allow {
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$c->log->debug("++++++++++++++++ request body: " . $c->stash->{body});
$self->log_request($c);
}
sub GET :Allow {
@ -404,17 +405,8 @@ sub hal_from_contact : Private {
sub end : Private {
my ($self, $c) = @_;
$c->forward(qw(Controller::Root render));
$c->response->content_type('')
if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way
if (@{ $c->error }) {
my $msg = join ', ', @{ $c->error };
$c->log->error($msg);
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error");
$c->clear_errors;
}
$c->log->debug("++++++++++++++++ response body: " . ($c->response->body // ''));
$self->log_response($c);
}
# vim: set tabstop=4 expandtab:

@ -41,14 +41,13 @@ __PACKAGE__->config(
action_roles => [qw(HTTPMethods)],
);
sub auto :Allow {
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$c->log->debug("++++++++++++++++ request body: " . $c->stash->{body});
$self->log_request($c);
}
sub GET :Allow :Args(0) {
my ($self, $c) = @_;
my $page = $c->request->params->{page} // 1;
@ -223,17 +222,8 @@ sub valid_id : Private {
sub end : Private {
my ($self, $c) = @_;
$c->forward(qw(Controller::Root render));
$c->response->content_type('')
if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way
if (@{ $c->error }) {
my $msg = join ', ', @{ $c->error };
$c->log->error($msg);
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error");
$c->clear_errors;
}
$c->log->debug("++++++++++++++++ response body: " . ($c->response->body // ''));
}
$self->log_response($c);
}
# vim: set tabstop=4 expandtab:

@ -42,11 +42,11 @@ __PACKAGE__->config(
action_roles => [qw(HTTPMethods)],
);
sub auto :Allow {
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$c->log->debug("++++++++++++++++ request body: " . $c->stash->{body});
$self->log_request($c);
}
sub GET :Allow {
@ -355,16 +355,8 @@ sub hal_from_contract :Private {
sub end : Private {
my ($self, $c) = @_;
$c->forward(qw(Controller::Root render));
$c->response->content_type('')
if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way
if (@{ $c->error }) {
my $msg = join ', ', @{ $c->error };
$c->log->error($msg);
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error");
$c->clear_errors;
}
$c->log->debug("++++++++++++++++ response body: " . ($c->response->body // ''));
$self->log_response($c);
}
# vim: set tabstop=4 expandtab:

@ -30,11 +30,11 @@ __PACKAGE__->config(
action_roles => [qw(HTTPMethods)],
);
sub auto :Allow {
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$c->log->debug("++++++++++++++++ request body: " . $c->stash->{body});
$self->log_request($c);
}
sub GET :Allow {
@ -65,15 +65,10 @@ sub OPTIONS :Allow {
return;
}
sub end :Private {
sub end : Private {
my ($self, $c) = @_;
$c->forward(qw(Controller::Root render));
$c->response->content_type('')
if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way
if (@{ $c->error }) {
my $msg = join ', ', @{ $c->error };
$c->log->error($msg);
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error");
$c->clear_errors;
}
$self->log_response($c);
}
# vim: set tabstop=4 expandtab:

@ -35,6 +35,13 @@ __PACKAGE__->config(
action_roles => [qw(HTTPMethods)],
);
sub auto :Private {
my ($self, $c) = @_;
$self->set_body($c);
$self->log_request($c);
}
sub GET : Allow {
my ($self, $c) = @_;
my $response = $self->cached($c);

@ -278,5 +278,43 @@ sub set_body {
$c->stash->{body} = $c->request->body ? (do { local $/; $c->request->body->getline }) : '';
}
sub log_request {
my ($self, $c) = @_;
my $params = join(', ', map { "'".$_."'='".($c->request->query_params->{$_} // '')."'" }
keys %{ $c->request->query_params }
);
my $user;
if($c->user->roles eq "api_admin" || $c->user->roles eq "api_reseller") {
$user = $c->user->login;
} else {
$user = $c->user->username . '@' . $c->user->domain;
}
$c->log->info("API function '".$c->request->path."' called by '" . $user .
"' ('" . $c->user->roles . "') from host '".$c->request->address."' with params " .
(length $params ? $params : "''") .
" and body '" . $c->stash->{body} . "'");
}
sub log_response {
my ($self, $c) = @_;
# TODO: should be put a UUID to stash in log_request and use it here to correlate
# req/res lines?
$c->forward(qw(Controller::Root render));
$c->response->content_type('')
if $c->response->content_type =~ qr'text/html'; # stupid RenderView getting in the way
if (@{ $c->error }) {
my $msg = join ', ', @{ $c->error };
$c->log->error($msg);
$self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error");
$c->clear_errors;
}
$c->log->info("API function '".$c->request->path."' generated response with code '" .
$c->response->code . "' and body '" .
$c->response->body . "'");
}
1;
# vim: set tabstop=4 expandtab:

@ -23,6 +23,11 @@ $ua->ssl_opts(
# OPTIONS tests
{
# test some uri params
$req = HTTP::Request->new('OPTIONS', $uri.'/api/?foo=bar&bla');
$res = $ua->request($req);
ok($res->code == 200, "check options request with uri params");
$req = HTTP::Request->new('OPTIONS', $uri.'/api/');
$res = $ua->request($req);
ok($res->code == 200, "check options request");

Loading…
Cancel
Save