Perl Object-oriented processing JSON data and Rest API in Perl

Source: Internet
Author: User
Tags install perl redmine

Requirements : Some of the RESTful APIs have been implemented to remove the value of some key from the return value of the RESTful API by invoking the RESTful API.
1) in rest, each object is a 1 URL;
Here you need to know how Perl sends the request and how to handle 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) The return value of the Restful API is in JSON format, which is a nested dictionary in Python
We need to learn the JSON knowledge.
http://search.cpan.org/~makamaka/JSON-2.90/lib/JSON.pm

3) product as a class to provide
You need to understand the basics of Perl, such as hashing, arrays, loops, and so on, in addition to understanding object-oriented.
Http://www.runoob.com/perl/perl-tutorial.html

First, install the Perl IDE
There are many, novice tutorials are also recommended. Padre is installed here.
Install Perl ide:http://padre.perlide.org/on Windows PC

Second, practice
is to follow the rookie tutorial for 0.5 days.

Write the script according to the requirement.
The data structures returned by each URL are as follows:

Constructors and some common variables

 Our $web _base_url=""; Our %web_base_url= ();$web _base_url{Testbase} =" This was forURL"; Our $ENODEB=CONSTANT VALUE Our $CABINET=CONSTANT VALUE Our $TELNET=CONSTANT VALUE Our $SWITCH=CONSTANT VALUE Our $MDU=CONSTANT VALUE Our $SYNCHRONIZATION=CONSTANT VALUE Our $SOURCE=CONSTANT VALUE############################################################################### CONSTRUCTOR# New (< testplanname>) # The cunstructor for this class############################################################ ##################SubNew {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. send a GET request to get responsecontent

Sub_getresponsecontent{my($self,$URL) =@_; my $ua= lwp::useragent->New (protocols_allowed= ['http','HTTPS'],Timeout= -,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. Break down each large item value

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. Common methods of value-taking

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. The method for the outward use of the package, to give only one example

Subgetswitchport{#my ($funcName) = shift;    my($self) =@_; return $self->_getciattributes ("$TELNET",['relation_list','params_ci_1','Port']);}

Perl Object-oriented processing JSON data and Rest API in Perl

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.