From 59bb04b69dab482bf3f9b1517c48312ba331d345 Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Fri, 13 Nov 2015 14:05:40 +0100 Subject: [PATCH] MT#15883 import comx API client Change-Id: I6bc6784706beb44fb0144dc421f3ce1b0f79816c --- lib/NGCP/Panel/Utils/ComxAPIClient.pm | 202 ++++++++++++++++++++++++++ t/component-comxapiclient.t | 33 +++++ 2 files changed, 235 insertions(+) create mode 100644 lib/NGCP/Panel/Utils/ComxAPIClient.pm create mode 100644 t/component-comxapiclient.t diff --git a/lib/NGCP/Panel/Utils/ComxAPIClient.pm b/lib/NGCP/Panel/Utils/ComxAPIClient.pm new file mode 100644 index 0000000000..dc4a1978fd --- /dev/null +++ b/lib/NGCP/Panel/Utils/ComxAPIClient.pm @@ -0,0 +1,202 @@ +package NGCP::Panel::Utils::ComxAPIClient; + +use warnings; +use strict; + +use Moo; +# use Digest::MD5 qw/md5_hex/; +# use HTTP::Tiny; +# use Storable qw/freeze/; +use Types::Standard qw(Int HashRef); +# with 'Role::REST::Client'; +use LWP::UserAgent; +use JSON qw/decode_json encode_json/; + +has 'ua' => ( is => 'rw', default => sub { + return LWP::UserAgent->new( + ssl_opts => { verify_hostname => 0 }, + timeout => 20, + ); + }); + +has 'host' => (is => 'rw', default => 'https://www.api-cdk.tld:8191'); + +has 'login_status' => (is => 'rw', + #isa => 'HTTP::Response', + ); + +sub mytest { + my $c = NGCP::Panel::Utils::ComxAPIClient->new( + host => 'https://rtcengine.sipwise.com/rtcengine/api', + ); + $c->login('gjungwirth@sipwise', '***', 'rtcengine.sipwise.com:443'); + #p $c->get_sessions; + #p $c->create_session_and_account('npa4V0YkavioQ1GW7Yob', 'sip', 'user1@bar.com', '123456', 'YAqON76yLVtgMgBYeg6v'); + #p $c->create_session_and_account('npa4V0YkavioQ1GW7Yob', 'sip4', 'sip:alice@192.168.51.150', 'alicepass', 'YAqON76yLVtgMgBYeg6v'); + #p $c->get_networks; + $c->create_network('sip', 'sip-connector', {xms => JSON::false}, 'YAqON76yLVtgMgBYeg6v'); + print "done\n"; + return; +} + +# returns appid or 0 +sub login { + my ( $self, $username, $password, $netloc ) = @_; + my $ua = $self->ua; + $netloc //= $self->host =~ s!^https?://(.*:[0-9]*)(/.*$|$)!$1!r; + $ua->credentials($netloc, "ComX", $username, $password); + my $resp = $ua->get($self->host . '/users'); + $self->login_status( $self->_create_response($resp) ); + return; +} + +sub create_session_and_account { + my ($self, $appid, $network, $identifier, $accessToken, $owner) = @_; + my $ua = $self->ua; + my $session_content = encode_json({ + app => $appid, + owner => $owner, + }); + my $session = $self->_create_response( + $ua->post($self->host . '/sessions', 'Content-Type' => 'application/json', Content => $session_content), + ); + my $account_content = encode_json({ + session => $session->{data}{id}, + network => $network, + identifier => $identifier, + accessToken => $accessToken, + owner => $owner, + }); + #p $account_content; + my $account = $self->_create_response( + $ua->post($self->host . '/accounts', 'Content-Type' => 'application/json', Content => $account_content), + ); + $account->{data}{session} = $session->{data}; + return $account; +} + +sub create_network { + my ($self, $tag, $connector, $config, $owner) = @_; + my $ua = $self->ua; + my $network_content = encode_json({ + tag => $tag, + connector => $connector, + config => encode_json($config), + owner => $owner, + }); + my $network = $self->_create_response( + $ua->post($self->host . '/networks', 'Content-Type' => 'application/json', Content => $network_content), + ); + return $network; +} + +sub delete_network { + my ($self, $network_id) = @_; + my $ua = $self->ua; + $network_id //= ""; + my $resp; + $resp = $ua->delete($self->host . "/networks/id/$network_id"); + return $self->_create_response($resp); +} + +sub create_app { + my ($self, $name, $domain, $owner) = @_; + my $ua = $self->ua; + my $app_content = encode_json({ + name => $name, + domain => $domain, + owner => $owner, + }); + my $app = $self->_create_response( + $ua->post($self->host . '/apps', 'Content-Type' => 'application/json', Content => $app_content), + ); + return $app; +} + +sub get_sessions { + my ($self, $max_rows) = @_; + my $sessions = $self->_resolve_collection( '/sessions', $max_rows ); + if ('ARRAY' eq ref $sessions && @{ $sessions }) { + for my $session (@{ $sessions }) { + $session->{accounts} = $self->_resolve_collection( $session->{accounts}{href} ); + } + } + return $sessions; +} + +sub get_session { + my ($self, $session_id) = @_; + my $ua = $self->ua; + + return $self->_create_response( + $ua->get($self->host . "/sessions/id/$session_id") + ); +} + +sub delete_all_sessions { + my ($self) = @_; + my $ua = $self->ua; + my $resp; + for my $session_data ($self->get_sessions) { + my $session_id = $session_data->{id}; + $resp = $ua->delete($self->host . "/sessions/id/$session_id"); + last if $resp->code >= 300; + } + return $resp; +} + +sub get_users { + my ($self, $max_rows) = @_; + my $users = $self->_resolve_collection( '/users', $max_rows ); + return $users; +} + +sub get_networks { + my ($self) = @_; + my $networks = $self->_resolve_collection( '/networks' ); + return $networks; +} + +sub _resolve_collection { + my ($self, $bare_url, $max_rows) = @_; + my $ua = $self->ua; + my $rel_url = $self->_strip_host( $bare_url ); + my $res = $ua->get($self->host . $rel_url); + my @result; + return [] unless $res->code == 200; + my $collection = JSON::decode_json($res->content); + return [] unless $collection; + for my $item (@{ $collection->{items} }) { + last if (defined $max_rows && $max_rows-- <= 0); + my $url = $self->_strip_host( $item->{href} ); + my $item_res = $ua->get($self->host . $url); + my $item_data = decode_json($item_res->content); + push @result, $item_data; + } + return \@result; +} + +sub _strip_host { + my ($self, $url) = @_; + my $url_orig = $self->host; + my $url_noip = $url_orig =~ s!:\d+!!r; + return $url =~ s!$url_orig|$url_noip!!r; +} + +sub _create_response { + my ($self, $res) = @_; + my $data; + if ($res->is_success && $res->content) { + $data = decode_json($res->content); + } + return { + code => $res->code, + data => $data, + response => $res, + }; +} + + +1; + +# vim: set tabstop=4 expandtab: diff --git a/t/component-comxapiclient.t b/t/component-comxapiclient.t new file mode 100644 index 0000000000..249d324327 --- /dev/null +++ b/t/component-comxapiclient.t @@ -0,0 +1,33 @@ +use warnings; +use strict; +use Test::More; + +use NGCP::Panel::Utils::ComxAPIClient; + +my $comx_host = $ENV{COMX_HOST} // 'https://rtcengine.sipwise.com/rtcengine/api'; +my $comx_user = $ENV{COMX_USER} // 'gjungwirth@sipwise'; +my $comx_pass = $ENV{COMX_PASS}; +my $comx_netloc = $comx_host =~ s!^https://([^/:]*)(:[0-9]*)?/.*$!$1.($2||":443")!re; # 'rtcengine.sipwise.com:443' + +my $c = NGCP::Panel::Utils::ComxAPIClient->new( + host => $comx_host, +); +my $login_response = $c->login($comx_user, $comx_pass, $comx_netloc); +ok($c->login_status, "Login done"); +is($c->login_status->{code}, 200, "Login successful"); + + #p $c->get_sessions; +#p $c->create_session_and_account('npa4V0YkavioQ1GW7Yob', 'sip4', 'sip:alice@192.168.51.150', 'alicepass', 'YAqON76yLVtgMgBYeg6v'); +#p $c->get_networks; +my $network = $c->create_network('gjungwirth_test', 'sip-connector', {xms => JSON::false}, 'YAqON76yLVtgMgBYeg6v'); +ok($network, "Create Network"); +is($network->{code}, 201, "Create Network successful"); +ok($network->{data}{id}, "Got a network id"); + +#p $c->create_session_and_account('npa4V0YkavioQ1GW7Yob', 'sip', 'user1@bar.com', '123456', 'YAqON76yLVtgMgBYeg6v'); + +my $tmp_resp = $c->delete_network($network->{data}{id}); +is($tmp_resp->{code}, 200, "Delete Network"); + +ok(1,"stub, done"); +done_testing; \ No newline at end of file