POST, GET, and item GET work +adapt to new roleless api controllers Change-Id: Iab9c3ac3f63bd1da51c3723ed49ab4dadfd490ccchanges/68/4268/12
parent
22c989e514
commit
de6d94748e
@ -0,0 +1,195 @@
|
||||
package NGCP::Panel::Controller::API::RtcSessions;
|
||||
use NGCP::Panel::Utils::Generic qw(:all);
|
||||
|
||||
use boolean qw(true);
|
||||
use Data::HAL qw();
|
||||
use Data::HAL::Link qw();
|
||||
use HTTP::Headers qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
|
||||
require Catalyst::ActionRole::ACL;
|
||||
require Catalyst::ActionRole::CheckTrailingSlash;
|
||||
require Catalyst::ActionRole::HTTPMethods;
|
||||
require Catalyst::ActionRole::RequireSSL;
|
||||
|
||||
|
||||
sub api_description {
|
||||
return 'Show a collection of RTC sessions, belonging to a specific subscriber.';
|
||||
}
|
||||
|
||||
sub allowed_methods{
|
||||
return [qw/GET POST OPTIONS HEAD/];
|
||||
}
|
||||
|
||||
sub query_params {
|
||||
return [];
|
||||
}
|
||||
|
||||
use base qw/Catalyst::Controller NGCP::Panel::Role::API::RtcSessions/;
|
||||
|
||||
sub resource_name{
|
||||
return 'rtcsessions';
|
||||
}
|
||||
sub dispatch_path{
|
||||
return '/api/rtcsessions/';
|
||||
}
|
||||
sub relation{
|
||||
return 'http://purl.org/sipwise/ngcp-api/#rel-rtcsessions';
|
||||
}
|
||||
|
||||
__PACKAGE__->config(
|
||||
action => {
|
||||
map { $_ => {
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => [qw/admin reseller/],
|
||||
Args => 0,
|
||||
Does => [qw(ACL CheckTrailingSlash RequireSSL)],
|
||||
Method => $_,
|
||||
Path => __PACKAGE__->dispatch_path,
|
||||
} } @{ __PACKAGE__->allowed_methods },
|
||||
},
|
||||
action_roles => [qw(HTTPMethods)],
|
||||
);
|
||||
|
||||
sub auto :Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->set_body($c);
|
||||
$self->log_request($c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub GET :Allow {
|
||||
my ($self, $c) = @_;
|
||||
my $page = $c->request->params->{page} // 1;
|
||||
my $rows = $c->request->params->{rows} // 10;
|
||||
{
|
||||
my $subscribers = $self->item_rs($c);
|
||||
(my $total_count, $subscribers) = $self->paginate_order_collection($c, $subscribers);
|
||||
my (@embedded, @links);
|
||||
for my $subscriber ($subscribers->all) {
|
||||
my $hal = $self->hal_from_item($c, $subscriber);
|
||||
next unless $hal;
|
||||
push @embedded, $hal;
|
||||
push @links, Data::HAL::Link->new(
|
||||
relation => 'ngcp:'.$self->resource_name,
|
||||
href => sprintf('%s%d', $self->dispatch_path, $subscriber->id),
|
||||
);
|
||||
}
|
||||
push @links,
|
||||
Data::HAL::Link->new(
|
||||
relation => 'curies',
|
||||
href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}',
|
||||
name => 'ngcp',
|
||||
templated => true,
|
||||
),
|
||||
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
|
||||
Data::HAL::Link->new(relation => 'self', href => sprintf('%s?page=%s&rows=%s', $self->dispatch_path, $page, $rows));
|
||||
if(($total_count / $rows) > $page ) {
|
||||
push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('%s?page=%d&rows=%d', $self->dispatch_path, $page + 1, $rows));
|
||||
}
|
||||
if($page > 1) {
|
||||
push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page - 1, $rows));
|
||||
}
|
||||
|
||||
my $hal = Data::HAL->new(
|
||||
embedded => [@embedded],
|
||||
links => [@links],
|
||||
);
|
||||
$hal->resource({
|
||||
total_count => $total_count,
|
||||
});
|
||||
my $response = HTTP::Response->new(HTTP_OK, undef,
|
||||
HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json);
|
||||
$c->response->headers($response->headers);
|
||||
$c->response->body($response->content);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
sub HEAD :Allow {
|
||||
my ($self, $c) = @_;
|
||||
$c->forward(qw(GET));
|
||||
$c->response->body(q());
|
||||
return;
|
||||
}
|
||||
|
||||
sub OPTIONS :Allow {
|
||||
my ($self, $c) = @_;
|
||||
my $allowed_methods = $self->allowed_methods_filtered($c);
|
||||
$c->response->headers(HTTP::Headers->new(
|
||||
Allow => join(', ', @{ $allowed_methods }),
|
||||
Accept_Post => 'application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-'.$self->resource_name,
|
||||
));
|
||||
$c->response->content_type('application/json');
|
||||
$c->response->body(JSON::to_json({ methods => $allowed_methods })."\n");
|
||||
return;
|
||||
}
|
||||
|
||||
sub POST :Allow {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
my $guard = $c->model('DB')->txn_scope_guard;
|
||||
{
|
||||
my $schema = $c->model('DB');
|
||||
my $resource = $self->get_valid_post_data(
|
||||
c => $c,
|
||||
media_type => 'application/json',
|
||||
);
|
||||
last unless $resource;
|
||||
|
||||
if($c->user->roles eq "admin") {
|
||||
} elsif($c->user->roles eq "reseller") {
|
||||
$resource->{reseller_id} = $c->user->reseller_id; # TODO: ?
|
||||
} else {
|
||||
$resource->{subscriber_id} = $c->user->id;
|
||||
}
|
||||
|
||||
my $subscriber_item = $c->model('DB')->resultset('voip_subscribers')->search_rs({
|
||||
id => $resource->{subscriber_id},
|
||||
})->first;
|
||||
|
||||
unless ($subscriber_item) {
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Subscriber invalid or not found.");
|
||||
last;
|
||||
}
|
||||
|
||||
# my $form = $self->get_form();
|
||||
# $resource->{reseller_id} //= undef;
|
||||
# last unless $self->validate_form(
|
||||
# c => $c,
|
||||
# resource => $resource,
|
||||
# form => $form,
|
||||
# );
|
||||
|
||||
my $session_item = NGCP::Panel::Utils::Rtc::create_rtc_session(
|
||||
config => $c->config,
|
||||
subscriber_item => $subscriber_item,
|
||||
resource => $resource,
|
||||
err_code => sub {
|
||||
my ($err) = @_;
|
||||
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, $err);
|
||||
});
|
||||
last unless $session_item;
|
||||
|
||||
$guard->commit;
|
||||
|
||||
$c->response->status(HTTP_CREATED);
|
||||
$c->response->header(Location => sprintf('%s%d', $self->dispatch_path, $session_item->id));
|
||||
$c->response->body(q());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
sub end : Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->log_response($c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
no Moose;
|
||||
1;
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
||||
@ -0,0 +1,122 @@
|
||||
package NGCP::Panel::Controller::API::RtcSessionsItem;
|
||||
use NGCP::Panel::Utils::Generic qw(:all);
|
||||
|
||||
use Data::HAL qw();
|
||||
use Data::HAL::Link qw();
|
||||
use HTTP::Headers qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
|
||||
require Catalyst::ActionRole::ACL;
|
||||
require Catalyst::ActionRole::HTTPMethods;
|
||||
require Catalyst::ActionRole::RequireSSL;
|
||||
|
||||
use base qw/Catalyst::Controller NGCP::Panel::Role::API::RtcSessions/;
|
||||
|
||||
sub resource_name{
|
||||
return 'rtcsessions';
|
||||
}
|
||||
sub dispatch_path{
|
||||
return '/api/rtcsessions/';
|
||||
}
|
||||
sub relation{
|
||||
return 'http://purl.org/sipwise/ngcp-api/#rel-rtcsessions';
|
||||
}
|
||||
|
||||
sub allowed_methods{
|
||||
return [qw/GET OPTIONS HEAD/];
|
||||
}
|
||||
|
||||
sub journal_query_params {
|
||||
my($self,$query_params) = @_;
|
||||
return $self->get_journal_query_params($query_params);
|
||||
}
|
||||
|
||||
sub get_journal_methods{
|
||||
return [qw/handle_item_base_journal handle_journals_get handle_journalsitem_get handle_journals_options handle_journalsitem_options handle_journals_head handle_journalsitem_head/];
|
||||
}
|
||||
|
||||
__PACKAGE__->config(
|
||||
action => {
|
||||
(map { $_ => {
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => [qw/admin reseller/],
|
||||
Args => 1,
|
||||
Does => [qw(ACL RequireSSL)],
|
||||
Method => $_,
|
||||
Path => __PACKAGE__->dispatch_path,
|
||||
} } @{ __PACKAGE__->allowed_methods }),
|
||||
@{ __PACKAGE__->get_journal_action_config(__PACKAGE__->resource_name,{
|
||||
ACLDetachTo => '/api/root/invalid_user',
|
||||
AllowedRole => [qw/admin reseller/],
|
||||
Does => [qw(ACL RequireSSL)],
|
||||
}) },
|
||||
},
|
||||
action_roles => [qw(HTTPMethods)],
|
||||
);
|
||||
|
||||
sub auto :Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->set_body($c);
|
||||
$self->log_request($c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub GET :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
{
|
||||
last unless $self->valid_id($c, $id);
|
||||
my $item = $self->item_by_id($c, $id);
|
||||
last unless $self->resource_exists($c, rtc_session => $item);
|
||||
|
||||
my $hal = $self->hal_from_item($c, $item);
|
||||
|
||||
unless ($hal) {
|
||||
$c->log->error("Session not found. It may have expired.");
|
||||
$self->error($c, HTTP_NOT_FOUND, "Session not found. It may have expired.");
|
||||
last;
|
||||
}
|
||||
|
||||
my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new(
|
||||
(map { # XXX Data::HAL must be able to generate links with multiple relations
|
||||
s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|r
|
||||
=~ s/rel=self/rel="item self"/r;
|
||||
} $hal->http_headers),
|
||||
), $hal->as_json);
|
||||
$c->response->headers($response->headers);
|
||||
$c->response->body($response->content);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
sub HEAD :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
$c->forward(qw(GET));
|
||||
$c->response->body(q());
|
||||
return;
|
||||
}
|
||||
|
||||
sub OPTIONS :Allow {
|
||||
my ($self, $c, $id) = @_;
|
||||
my $allowed_methods = $self->allowed_methods_filtered($c);
|
||||
$c->response->headers(HTTP::Headers->new(
|
||||
Allow => join(', ', @{ $allowed_methods }),
|
||||
Accept_Patch => 'application/json-patch+json',
|
||||
));
|
||||
$c->response->content_type('application/json');
|
||||
$c->response->body(JSON::to_json({ methods => $allowed_methods })."\n");
|
||||
return;
|
||||
}
|
||||
|
||||
sub end : Private {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
$self->log_response($c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
no Moose;
|
||||
1;
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
||||
@ -0,0 +1,91 @@
|
||||
package NGCP::Panel::Role::API::RtcSessions;
|
||||
use NGCP::Panel::Utils::Generic qw(:all);
|
||||
|
||||
use base 'NGCP::Panel::Role::API';
|
||||
|
||||
use boolean qw(true);
|
||||
use TryCatch;
|
||||
use Data::HAL qw();
|
||||
use Data::HAL::Link qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
use JSON::Types;
|
||||
|
||||
use NGCP::Panel::Utils::Subscriber;
|
||||
use NGCP::Panel::Utils::Rtc;
|
||||
|
||||
sub get_form {
|
||||
my ($self) = @_;
|
||||
|
||||
#return NGCP::Panel::Form::Rtc::NetworksAdmin->new;
|
||||
return;
|
||||
}
|
||||
|
||||
sub hal_from_item {
|
||||
my ($self, $c, $item) = @_;
|
||||
|
||||
my $resource = {
|
||||
subscriber_id => $item->subscriber->voip_subscriber->id, # this may be confusing but we store the provisioning-subscriber-id but show the billing one
|
||||
rtc_network_tag => $item->rtc_network_tag,
|
||||
};
|
||||
|
||||
my $rtc_session = NGCP::Panel::Utils::Rtc::get_rtc_session(
|
||||
config => $c->config,
|
||||
item => $item,
|
||||
err_code => sub {
|
||||
$c->log->warn(shift); return;
|
||||
});
|
||||
if ($rtc_session) {
|
||||
$resource->{rtc_browser_token} = $rtc_session->{data}{token};
|
||||
} else {
|
||||
# here either delete our DB entry, or recreate it accordingly
|
||||
$item->delete;
|
||||
return;
|
||||
}
|
||||
|
||||
my $hal = Data::HAL->new(
|
||||
links => [
|
||||
Data::HAL::Link->new(
|
||||
relation => 'curies',
|
||||
href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}',
|
||||
name => 'ngcp',
|
||||
templated => true,
|
||||
),
|
||||
Data::HAL::Link->new(relation => 'collection', href => sprintf("/api/%s/", $self->resource_name)),
|
||||
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
|
||||
Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $item->id)),
|
||||
Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $item->subscriber->voip_subscriber->id)),
|
||||
$self->get_journal_relation_link($item->id),
|
||||
],
|
||||
relation => 'ngcp:'.$self->resource_name,
|
||||
);
|
||||
|
||||
$hal->resource($resource);
|
||||
return $hal;
|
||||
}
|
||||
|
||||
sub item_rs {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
my $item_rs;
|
||||
$item_rs = $c->model('DB')->resultset('rtc_session');
|
||||
if($c->user->roles eq "admin") {
|
||||
} elsif($c->user->roles eq "reseller") {
|
||||
$item_rs = $item_rs->search({
|
||||
'contact.reseller_id' => $c->user->reseller_id,
|
||||
},{
|
||||
join => {subscriber => { voip_subscriber => { contract => 'contact' }}},
|
||||
});
|
||||
}
|
||||
|
||||
return $item_rs;
|
||||
}
|
||||
|
||||
sub item_by_id {
|
||||
my ($self, $c, $id) = @_;
|
||||
|
||||
my $item_rs = $self->item_rs($c);
|
||||
return $item_rs->find($id);
|
||||
}
|
||||
|
||||
1;
|
||||
# vim: set tabstop=4 expandtab:
|
||||
Loading…
Reference in new issue