API-ParallelsWPB

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


    api_version
        API version, used in API url constructing. Optional parameter.

    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

    map { confess "Field '" . $_ . "' required!" unless $self->{ $_ } } qw/username password server/;

    return bless $self, $class;
}

# "free" request. Basic method for requests



sub f_request {
    my ( $self, $url_array, $data ) = @_;

    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

=item debug

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

=item timeout

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

        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


    $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 );

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'
);

subtest 'Test GET request' => sub {

    plan tests => 2;

    $client->f_request( [qw/ system version /], { req_type => 'get' } );

    is( $transfered_params{url}, 'https://127.0.0.1/api/5.3/system/version/',
        'URL is ok' );

    is_deeply(
        $transfered_params{data},
        { req_type => 'GET' },
        'Request type is GET'
    );

};

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


subtest 'get_version' => sub {
    plan tests => 2;

    $client->get_version;
    my $p = $client->get_request_params;

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

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

subtest 'create_site' => sub {
    plan tests => 3;

    # after site creation uuid goes to $client, and methods with uuid required can be called without uuid in params
    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;

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

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

};


#  /api /5.3/sites/
subtest 'get_sites_info' => sub {
    $client->get_sites_info;
    my $p = $client->get_request_params;

    like(
        $p->{url},
        qr{/api/5.3/sites/},
        'URL for get_sites_info is ok'
    );

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

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

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

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



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

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

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


# /api/5.3/system/promo-footer
subtest 'get_promo_footer' => sub {
    $client->get_promo_footer;
    my $p = $client->get_request_params;

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

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


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

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

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


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

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

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

#  /api/5.3/sites/{site_uuid}/custom-properties
subtest 'get_sites_custom_variables' => sub {
    $client->get_sites_custom_variables;
    my $p = $client->get_request_params;

    like(
        $p->{url},
        qr{/api/5.3/system/custom-properties},
        'URL for get_sites_custom_variables is ok'
    );

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

#  /api/5.3/sites/{site_uuid}/custom-properties
subtest 'get_sites_custom_variables' => sub {
    $client->get_sites_custom_variables;
    my $p = $client->get_request_params;

    like(
        $p->{url},
        qr{/api/5.3/system/custom-properties},
        'URL for get_sites_custom_variables is ok'
    );

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

#  /api/5.3/sites/{site_uuid}/custom-properties
subtest 'set_sites_custom_variables' => sub {
    $client->set_sites_custom_variables;
    my $p = $client->get_request_params;

    like(
        $p->{url},
        qr{/api/5.3/system/custom-properties},
        'URL for set_sites_custom_variables is ok'
    );

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

#  /api/5.3/sites/{site_uuid}/custom-properties
subtest 'set_custom_trial_messages' => sub {
    $client->set_custom_trial_messages;
    my $p = $client->get_request_params;

    like(
        $p->{url},
        qr{/api/5.3/system/trial-mode/messages},
        'URL for set_custom_trial_messages is ok'
    );

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

#  /api/5.3/sites/{site_uuid}/custom-properties
subtest 'get_custom_trial_messages' => sub {
    $client->get_custom_trial_messages;
    my $p = $client->get_request_params;

    like(
        $p->{url},
        qr{/api/5.3/system/trial-mode/messages},
        'URL for get_custom_trial_messages is ok'
    );

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

#  /api/5.3/sites/{site_uuid}/custom-properties
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 0.485 second using v1.01-cache-2.11-cpan-a5abf4f5562 )