API-ParallelsWPB

 view release on metacpan or  search on metacpan

LICENSE  view on Meta::CPAN


2. You may apply bug fixes, portability fixes and other modifications derived
from the Public Domain or from the Copyright Holder. A Package modified in such
a way shall still be considered the Standard Version.

3. You may otherwise modify your copy of this Package in any way, provided that
you insert a prominent notice in each changed file stating how and when you
changed that file, and provided that you do at least ONE of the following:

  a) place your modifications in the Public Domain or otherwise make them
     Freely Available, such as by posting said modifications to Usenet or an
     equivalent medium, or placing the modifications on a major archive site
     such as ftp.uu.net, or by allowing the Copyright Holder to include your
     modifications in the Standard Version of the Package.

  b) use the modified Package only within your corporation or organization.

  c) rename any non-standard executables so the names do not conflict with
     standard executables, which must also be provided, and provide a separate
     manual page for each non-standard executable that clearly documents how it
     differs from the Standard Version.

README  view on Meta::CPAN


    debug
        Debug flag, requests will be loogged to stderr. Optional parameter.

    timeout
        Connection timeout. Optional parameter.

  f_request($self, $url_array_ref, $data)
    "Free" request. Now for internal usage only.

    $data: req_type : HTTP request type: get, post, put, delete. GET by
    default. post_data: data for POST request. Must be hashref.

SEE ALSO
    Parallels Presence Builder Guide
    <http://download1.parallels.com/WPB/Doc/11.5/en-US/online/presence-build
    er-standalone-installation-administration-guide>

    API::ParallelsWPB::Response

    API::ParallelsWPB::Requests

lib/API/ParallelsWPB.pm  view on Meta::CPAN


    confess "$url_array is not array!" unless ( ref $url_array eq 'ARRAY' );

    $data->{req_type} ||= 'GET';
    $data->{req_type} = uc $data->{req_type};

    #compile URL
    my $url = 'https://' . $self->{server} . '/api/' . $self->{api_version} . '/';
    $url .= join( '/', @{ $url_array }) . '/';

    my $post_data;

    if ( $data->{req_type} eq 'POST' || $data->{req_type} eq 'PUT' ) {
        $data->{post_data} ||= {};
        unless ( ref $data->{post_data} eq 'HASH' || ref $data->{post_data} eq 'ARRAY' ) {
            confess "parameter post_data must be hashref or arrayref!"
        }
        $post_data = $self->_json->encode($data->{post_data});
    }
    $post_data ||= '{}';

    my $response = $self->_send_request($data, $url, $post_data);
    return $response;
}

sub _send_request {
    my ( $self, $data, $url, $post_data ) = @_;

    my $ua = LWP::UserAgent->new();
    my $req = HTTP::Request->new( $data->{req_type} => $url );

    if ( $data->{req_type} eq 'POST' || $data->{req_type} eq 'PUT' ) {
        $req->header( 'content-type' => 'application/json' );
        $req->content( $post_data );
    }

    $req->authorization_basic( $self->{username}, $self->{password} );
    $ua->ssl_opts( verify_hostname => 0 );
    $ua->timeout( $self->{timeout} );

    warn $req->as_string if ( $self->{debug} );

    my $res = $ua->request( $req );
    warn $res->as_string if ( $self->{debug} );

lib/API/ParallelsWPB.pm  view on Meta::CPAN


Connection timeout. Optional parameter.

=back

=head2 B<f_request($self, $url_array_ref, $data)>

"Free" request. Now for internal usage only.

$data:
    req_type : HTTP request type: get, post, put, delete. GET by default.
    post_data: data for POST request. Must be hashref.

=head1 SEE ALSO

L<Parallels Presence Builder Guide|http://download1.parallels.com/WPB/Doc/11.5/en-US/online/presence-builder-standalone-installation-administration-guide>

L<API::ParallelsWPB::Response>

L<API::ParallelsWPB::Requests>

=head1 AUTHORS

lib/API/ParallelsWPB/Requests.pm  view on Meta::CPAN



sub create_site {
    my ( $self, %param ) = @_;

    $param{state}                ||= DEFAULT_CREATE_SITE_STATE;
    $param{publicationSettings}  ||= {};
    $param{ownerInfo}            ||= {};
    $param{isPromoFooterVisible} ||= '';

    my $post_array = [ {
        state                => $param{state},
        publicationSettings  => $param{publicationSettings},
        ownerInfo            => $param{ownerInfo},
        isPromoFooterVisible => $param{isPromoFooterVisible}
    } ];

    my $res = $self->f_request(
        ['sites'],
        {
            req_type  => 'post',
            post_data => $post_array,
        }
    );

    my $uuid = $res->response;
    if ( $uuid ) {
        $self->{uuid} = $uuid;
    }
    else {
        carp "parameter uuid not found";
    }

lib/API/ParallelsWPB/Requests.pm  view on Meta::CPAN

    my ( $self, %param ) = @_;

    $param{localeCode}      ||= DEFAULT_LOCALE_CODE;
    $param{sessionLifeTime} ||= DEFAULT_SESSIONLIFETIME;

    my $uuid = $self->_get_uuid( %param );

    return $self->f_request(
        [ 'sites', $uuid, 'token' ],
        {
            req_type  => 'post',
            post_data => [
                {
                    localeCode => $param{localeCode},
                    sessionLifeTime => $param{sessionLifeTime},
                } ],
        }
    );
}


sub deploy {
    my ( $self, %param ) = @_;

    $param{localeCode}   ||= $self->DEFAULT_LOCALE_CODE;
    $param{templateCode} ||= $self->DEFAULT_TEMPLATE_CODE;
    my $siteuuid = $self->_get_uuid( %param );

    my @post_data = map { $param{$_} } qw/templateCode localeCode title/;

    return $self->f_request(
        [ 'sites', $siteuuid, 'deploy' ],
        {
            req_type  => 'post',
            post_data => \@post_data
        }
    );
}



sub get_site_info {
    my ( $self, %param ) = @_;

    my $uuid = $self->_get_uuid( %param );

lib/API/ParallelsWPB/Requests.pm  view on Meta::CPAN



sub change_site_properties {
    my ( $self, %param ) = @_;

    my $uuid = $self->_get_uuid( %param );
    return $self->f_request(
        [ 'sites', $uuid ],
        {
            req_type  => 'put',
            post_data => [\%param]
        }
    );
}



sub publish {
    my ( $self, %param ) = @_;

    my $uuid = $self->_get_uuid( %param );
    return $self->f_request(
        [ 'sites', $uuid, 'publish' ],
        {
            req_type  => 'post',
        }
    );
}



sub delete_site {
    my ( $self, %param ) = @_;

    my $uuid = $self->_get_uuid( %param );

lib/API/ParallelsWPB/Requests.pm  view on Meta::CPAN


sub set_site_custom_variable {
    my ( $self, %param ) = @_;

    my $uuid = $self->_get_uuid( %param );

    delete $param{uuid} if ( exists $param{uuid} );
    return $self->f_request( [ 'sites', $uuid, 'custom-properties' ],
        {
            req_type  => 'put',
            post_data => [ \%param ],
        }
    );
}


sub get_sites_custom_variables {
    my ( $self ) = @_;

    return $self->f_request( [qw/ system custom-properties /],
        { req_type => 'get' } );
}


sub set_sites_custom_variables {
    my ( $self, %param ) = @_;

    return $self->f_request( [ qw/ system custom-properties / ],
        {
            req_type  => 'put',
            post_data => [ \%param ],
        }
    );
}


sub set_custom_trial_messages {
    my ( $self, @param ) = @_;

    return $self->f_request( [ qw/ system trial-mode messages / ],
        {
            req_type  => 'put',
            post_data => [ \@param ]
        }
    );
}


sub get_custom_trial_messages {
    my ( $self ) = @_;

    return $self->f_request( [qw/ system trial-mode messages /],
        { req_type => 'get' } );

lib/API/ParallelsWPB/Requests.pm  view on Meta::CPAN



sub change_promo_footer {
    my ( $self, %param ) = @_;

    confess "Required parameter message!" unless ( $param{message} );

    return $self->f_request( [ qw/ system promo-footer / ],
        {
           req_type  => 'put',
           post_data => [ $param{message} ],
        }
    );
}


sub set_site_promo_footer_visible {
    my ( $self, %param ) = @_;

    my $uuid = $self->_get_uuid( %param );

    return $self->f_request( [ 'sites', $uuid ], {
            req_type  => 'put',
            post_data => [ { isPromoFooterVisible => 'true' } ],
        }
    );
}


sub set_site_promo_footer_invisible {
    my ( $self, %param ) = @_;

    my $uuid = $self->_get_uuid( %param );

    return $self->f_request( [ 'sites', $uuid ], {
            req_type  => 'put',
            post_data => [ { isPromoFooterVisible => 'false' } ],
        }
    );
}



sub set_limits {
    my ( $self, %param ) = @_;

    my $uuid = $self->_get_uuid( %param );

    return $self->f_request( [ 'sites', $uuid, 'limits' ], {
            req_type  => 'put',
            post_data => [ \%param ],
        }
    );
}


sub configure_buy_and_publish_dialog {
    my ( $self, $params ) = @_;

    return $self->f_request(['system', 'trial-mode', 'messages'], {req_type => 'put', post_data => [ $params ]});

}

sub _get_uuid {
    my ( $self, %param ) = @_;

    my $uuid = $param{uuid} ? $param{uuid} : $self->{uuid};
    confess "Required parameter uuid!" unless ( $uuid );

    return $uuid;

t/03_f_request.t  view on Meta::CPAN

use API::ParallelsWPB;
use API::ParallelsWPB::Response;
use utf8;

my %transfered_params = ();

{
    no warnings 'redefine';

    *API::ParallelsWPB::_send_request = sub {
        my ( $self, $data, $url, $post_data ) = @_;
        %transfered_params = (
            self      => $self,
            data      => $data,
            url       => $url,
            post_data => $post_data
        );
    };
}

my $client = API::ParallelsWPB->new(
    username => 'test',
    password => 'passw0rd',
    server   => '127.0.0.1'
);

t/03_f_request.t  view on Meta::CPAN


};

subtest 'Test POST request' => sub {

    plan tests => 3;

    $client->f_request(
        ['sites'],
        {
            req_type  => 'post',
            post_data => [ { state => 'trial' } ]
        }
    );

    is(
        $transfered_params{url},
        'https://127.0.0.1/api/5.3/sites/',
        'Url for post is ok'
    );

    is( $transfered_params{post_data},
        qq/[{"state":"trial"}]/, 'POST data is ok' );

    is_deeply(
        $transfered_params{data},
        { req_type => 'POST', post_data => [ { state => 'trial' } ] },
        'Request type is POST'
    );
};

subtest 'Test POST request with uuid' => sub {

    plan tests => 4;

    $client->f_request(
        [ 'sites', '123', 'token' ],
        {
            req_type  => 'post',
            post_data => [
                {
                    localeCode      => 'de_DE',
                    sessionLifeTime => 1000
                }
            ],
        }
    );

    is(
        $transfered_params{url},
        'https://127.0.0.1/api/5.3/sites/123/token/',
        'Url for post with uuid is ok'
    );

    like( $transfered_params{post_data},
        qr/"sessionLifeTime":1000/, 'sessionLifeTime param trasfered' );

    like( $transfered_params{post_data},
        qr/"localeCode":"de_DE"/, 'LocaleCode trasfered' );

    is_deeply(
        $transfered_params{data},
        {
            req_type  => 'POST',
            post_data => [
                {
                    localeCode      => 'de_DE',
                    sessionLifeTime => 1000
                }
            ]
        },
        'Request type with uuid is POST'
    );
};


subtest 'Test unicode chars' => sub {
    plan tests => 1;

    $client->f_request(
        [ 'sites', '123' ],
        {
            req_type  => 'put',
            post_data => [
                {
                    ownerInfo => {
                        personalName => 'Василиус Пупкинус'
                    }
                }
            ],
        }
    );

    like(
        $transfered_params{post_data},
        qr/Василиус Пупкинус/,
        'Unicode char is ok in request'
    );
};


subtest 'Test utf-8' => sub {
    no utf8;
    plan tests => 1;

    $client->f_request(
        [ 'sites', '123' ],
        {
            req_type  => 'put',
            post_data => [
                {
                    ownerInfo => {
                        personalName => 'Василиус Пупкинус'
                    }
                }
            ],
        }
    );

    like(
        $transfered_params{post_data},
        qr/Василиус Пупкинус/,
        'utf8 char is ok in request'
    );
};

t/04_requests.t  view on Meta::CPAN

    my $client = t::lib::Mock->new(
        username => 'test',
        password => 'passw0rd',
        server   => '127.0.0.1'
    );

    $client->create_site( state => 'regular' );
    my $p = $client->get_request_params;

    like( $p->{url}, qr{/api/5.3/sites/}, 'URL for create_site is ok' );
    like( $p->{post_data}, qr{"state":"regular"},
        'post_data for create_site is ok' );
    is( $p->{data}->{req_type}, 'POST', 'Reqtype for create_site is ok' );
};

subtest 'gen_token' => sub {
    $client->gen_token( uuid => '6d3f6f9f-55b2-899f-5fb4-ae04b325e360' );
    my $p = $client->get_request_params;

    like(
        $p->{url},
        qr{/api/5.3/sites/[\d\w\-]+/token/},
        'URL for gen_token is ok'
    );
    like( $p->{post_data}, qr{"sessionLifeTime":"1800"},
        'post_data for gen_token is ok' );
    is( $p->{data}->{req_type}, 'POST', 'Reqtype for gen_token is ok' );
};

# URI: /api/5.3/sites/{site_uuid}/deploy
subtest 'deploy' => sub {
    $client->deploy(
        uuid  => '6d3f6f9f-55b2-899f-5fb4-ae04b325e360',
        title => 'Tiitle'
    );
    my $p = $client->get_request_params;

    like(
        $p->{url},
        qr{/api/5.3/sites/[\d\w\-]+/deploy},
        'URL for deploy is ok'
    );

    like( $p->{post_data}, qr{"generic","en_US","Tiitle"}, 'post_data for deploy is ok' );

    is( $p->{data}->{req_type}, 'POST', 'Reqtype for deploy is ok' );
};

# /api/5.3/sites/{site_uuid}/
subtest 'get_site_info' => sub {
    $client->get_site_info(
        uuid  => '6d3f6f9f-55b2-899f-5fb4-ae04b325e360',
    );
    my $p = $client->get_request_params;

t/04_requests.t  view on Meta::CPAN

        state => 'trial'
    );
    my $p = $client->get_request_params;

    like(
        $p->{url},
        qr{/api/5.3/sites/[\d\w\-]+/},
        'URL for change_site_properties is ok'
    );

    like( $p->{post_data}, qr{"state":"trial"}, 'post_data for change_site_properties is ok' );

    is( $p->{data}->{req_type}, 'PUT', 'Reqtype for change_site_properties is ok' );
};


# /api/5.3/sites/{siteUuid}/publish
subtest 'publish' => sub {
    $client->publish(
        uuid  => '6d3f6f9f-55b2-899f-5fb4-ae04b325e360',
    );

t/04_requests.t  view on Meta::CPAN

subtest 'change_promo_footer' => sub {
    $client->change_promo_footer( message => 'test' );
    my $p = $client->get_request_params;

    like(
        $p->{url},
        qr{/api/5.3/system/promo-footer},
        'URL for change_promo_footer is ok'
    );

    is( $p->{post_data}, q/["test"]/, 'Post data for change_promo_footer is ok');

    is( $p->{data}->{req_type}, 'PUT', 'Reqtype for change_promo_footer is ok' );
};


done_testing;

t/lib/Mock.pm  view on Meta::CPAN

# ABSTRACT: mock for testing API::ParallelsWPB

# VERSION
# AUTHORITY

my %send_request_params = ();

{
    no warnings 'redefine';
    *API::ParallelsWPB::_send_request = sub {
        my ( $self, $data, $url, $post_data ) = @_;

        %send_request_params = (
            self      => $self,
            url       => $url,
            data      => $data,
            post_data => $post_data
        );

        my $res = HTTP::Response->new;
        # Mocking HTTP response for different methods
        if ( $url =~ m{/api/5.3/sites/$} ) {
            # Create site request
            $res->code( 200 );
            $res->content( '{"response":"6d3f6f9f-55b2-899f-5fb4-ae04b325e360"}' );
        }
        else {



( run in 2.224 seconds using v1.01-cache-2.11-cpan-ceb78f64989 )