perl物件導向入門之處理json資料和rest api in perl

來源:互聯網
上載者:User

標籤:baseurl   cts   style   windows   arch   case   分享   建構函式   agent   

需求:有些已經實現好的Restful API,通過調用Restful API,取出Restful API的傳回值中部分key的值。
1)Rest中,每個對象都是1個URL;
這裡需要瞭解perl發送request,以及怎麼處理response.
http://www.redmine.org/projects/redmine/wiki/Rest_api_with_perl
http://search.cpan.org/~ether/libwww-perl-6.15/lib/LWP/UserAgent.pm

2)Restful API的傳回值是json格式的,也就是python中的嵌套字典
這裡需要學習json的知識
http://search.cpan.org/~makamaka/JSON-2.90/lib/JSON.pm

3)產物作為類提供
需要瞭解物件導向,除此以外還需要瞭解perl的雜湊,數組,迴圈等基礎知識。
http://www.runoob.com/perl/perl-tutorial.html

一、安裝perl IDE
    有很多,菜鳥教程也有推薦。這裡安裝的是Padre
    windows PC上裝perl IDE:http://padre.perlide.org/   

二、練習
    就是按照菜鳥教程練習0.5天
    
三、就是根據需求寫指令碼
每個URL返回的資料結構如下:

建構函式和一些共通的變數

our $web_base_url          = "";our %web_base_url          = ();$web_base_url{testBase}  = "this is for URL";our $ENODEB =  CONSTANT VALUEour $CABINET = CONSTANT VALUEour $TELNET = CONSTANT VALUEour $SWITCH = CONSTANT VALUEour $MDU = CONSTANT VALUEour $SYNCHRONIZATION = CONSTANT VALUEour $SOURCE = CONSTANT VALUE############################################################################### CONSTRUCTOR# new ( <testplanName> )# This is the cunstructor for this class##############################################################################sub new {    my $class = shift;    my (%params) = @_;    my $self = {    _baseUrl => $web_base_url{testBase},    _tpname => $params{tpname}    };    $self->{URL} = $self->{_baseUrl}.=$self->{_tpname};    bless ($self, $class);        $self->_setCisAttributes();    $self->{class} = $class;    return $self;}

 

1.發送get請求,獲得responseContent

sub _getResponseContent{    my( $self,$URL) = @_;    my $ua = LWP::UserAgent->new(             protocols_allowed => [ ‘http‘, ‘https‘ ],             timeout           => 30,             ssl_opts => { verify_hostname => 0 }             );    my $retval = {};    my $json = JSON->new->utf8;                        my $response = $ua->get($URL);    if ($response->is_error){        print "Failed to get  data by $self->{URL}\n";        die $response->message;    }           my $content = $response->content;    $retval = $json->decode($content);    return $retval;}


2.分解每個大的item值

sub _setCisAttributes{    my($self) = @_;    $self->{testPlan} = $self->_getResponseContent($self->{URL})->{items};    foreach ( @{ $self->{testPlan} }){        die "Not find key cfItem_type in element $->{‘cfItem_type‘} $self->{URL}" unless $_->{‘cfItem_type‘};        if ($_->{‘cfItem_type‘} =~ /$ENODEB/){            $self->{testRbsHash} = $_;        }        if ($_->{‘cfItem_type‘} =~ /$CABINET/){            $self->{testCabinetHash} = $_;        }        if ($_->{‘cfItem_type‘} =~ /$TELNET/ and $_->{‘cfItem_type‘} =~ /$SWITCH/){            $self->{testSwitchHash} = $_;        }        if ($_->{‘cfItem_type‘} =~ /$MDU/ and $_->{params}->{Master}->{value} =~ /true/){            $self->{testMduHash} = $_;            my $test = scalar($_->{params}->{Master}->{value});        }        if ($_->{‘cfItem_type‘} =~ /$SYNCHRONIZATION/ and $_->{‘cfItem_type‘} =~ /$SOURCE/){                             if (exists($_->{relation_list}->[0]->{params_ci_1}->{Value}->{0}->{‘Primary sync ref‘})             and $_->{relation_list}->[0]->{params_ci_1}->{Value}->{0}->{‘Primary sync ref‘}->{value} =~ /true/){                $self->{testPNTPHash} = $_;             }            else                              {                $self->{testSNTPHash} = $_;              }        }            }    return $self;}


3.取值的共通方法

sub _getCiAttributes{    my ( $self, $ci, $attribs ) = @_;    #print "$ci\n";    #print "$attribs\n";    my $ref = "";        switch($ci){        case "$ENODEB" {            die "Not find ENODEB attributes\n" unless $self->{testRbsHash};                            $ref = $self->{testRbsHash};                                }        case "$CABINET" {            die "Not find Cabinet attributes\n" unless $self->{testCabinetHash};            $ref = $self->{testCabinetHash};        }        case "$TELNET" {                        die "Not find telnet switch attributes\n" unless $self->{testSwitchHash};            $ref = $self->{testSwitchHash};        }        case "$MDU" {                    die "Not find master Du attributes\n" unless  $self->{testMduHash};            $ref = $self->{testMduHash};        }    }    foreach ( @{ $attribs } ) {        $ref = $ref->[ 0 ] if ( ref( $ref ) eq ‘ARRAY‘ );                $ref = $ref->{ $_ };     }    return $ref;}


4.供包外調用的方法,只舉一例

sub getSwitchPort{    #my($funcName) = shift;    my($self) = @_;    return $self->_getCiAttributes("$TELNET",[‘relation_list‘,‘params_ci_1‘,‘Port‘]);}

perl物件導向入門之處理json資料和rest api in perl

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.