Skip to content

Commit

Permalink
Default WHM API calls to using v1.
Browse files Browse the repository at this point in the history
The list of calls available in v1 is a superset of those available in
v0, although sometimes the parameters differ.  Default to using API 1.
  • Loading branch information
brian m. carlson committed Nov 5, 2015
1 parent 70bd034 commit 4f84531
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@ Revision history for cPanel-PublicAPI
1.4 2015-09-15
Updated PublicAPI pod file to include description
of new ssl_verify_mode option.
Default WHM API to v1.

1.3 2015-09-14
Fix hash randomization problem on newer Perls.
Expand Down
11 changes: 10 additions & 1 deletion lib/cPanel/PublicAPI.pm
Expand Up @@ -29,7 +29,7 @@ package cPanel::PublicAPI;
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

our $VERSION = 1.3;
our $VERSION = 1.4;

use strict;
use Socket ();
Expand Down Expand Up @@ -181,6 +181,15 @@ sub whm_api {
if ( defined $format && $format ne 'xml' && $format ne 'json' && $format ne 'ref' ) {
$self->error("cPanel::PublicAPI::whm_api_request() was called with an invalid data format, the only valid format are 'json', 'ref' or 'xml'");
}

$formdata ||= {};
if ( ref $formdata ) {
$formdata = { 'api.version' => 1, %$formdata };
}
elsif ( $formdata !~ /(^|&)api\.version=/ ) {
$formdata = "api.version=1&$formdata";
}

my $query_format;
if ( defined $format ) {
$query_format = $format;
Expand Down
3 changes: 3 additions & 0 deletions lib/cPanel/PublicAPI.pod
Expand Up @@ -178,6 +178,9 @@ The meaning of these parameters is:

=back

By default, WHM API v1 is used. If, for legacy reasons, you need to use v0,
please set the C<api.version> key to 0 in the formdata parameter.

For more information on what calls are available and how they can be referenced, please see the xml-api documentation at L<http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi>.

=head2 Querying cPanel's APIs
Expand Down
24 changes: 21 additions & 3 deletions t/01-api-format.t
Expand Up @@ -29,15 +29,33 @@ my $res = $pubapi->whm_api('loadavg');
is( ref $res, 'HASH', 'Returned format ok for ' . $PubApiTest::test_config->{'call'} );

$PubApiTest::test_config->{'test_formdata'} = 'hash';
$PubApiTest::test_config->{'formdata'} = { 'key' => 'value' };
$PubApiTest::test_config->{'formdata'} = { 'key' => 'value', 'api.version' => 1 };
$PubApiTest::test_config->{'call'} = 'whm_api-refform';
$res = $pubapi->whm_api( 'loadavg', { 'key' => 'value' } );
is( ref $res, 'HASH', 'Returned format ok for ' . $PubApiTest::test_config->{'call'} );

$PubApiTest::test_config->{'test_formdata'} = 'hash';
$PubApiTest::test_config->{'formdata'} = { 'key' => 'value', 'api.version' => 0 };
$PubApiTest::test_config->{'call'} = 'whm_api-refapi0';
$res = $pubapi->whm_api( 'loadavg', $PubApiTest::test_config->{'formdata'} );
is( ref $res, 'HASH', 'Returned format ok for ' . $PubApiTest::test_config->{'call'} );

$PubApiTest::test_config->{'test_formdata'} = 'hash';
$PubApiTest::test_config->{'formdata'} = { 'key' => 'value', 'api.version' => 1 };
$PubApiTest::test_config->{'call'} = 'whm_api-refapi1';
$res = $pubapi->whm_api( 'loadavg', $PubApiTest::test_config->{'formdata'} );
is( ref $res, 'HASH', 'Returned format ok for ' . $PubApiTest::test_config->{'call'} );

$PubApiTest::test_config->{'test_formdata'} = 'string';
$PubApiTest::test_config->{'formdata'} = 'one&two';
$PubApiTest::test_config->{'formdata'} = 'api.version=1&one&two';
$PubApiTest::test_config->{'call'} = 'whm_api-stringform';
$res = $pubapi->whm_api( 'loadavg', $PubApiTest::test_config->{'formdata'} );
$res = $pubapi->whm_api( 'loadavg', 'one&two' );
is( ref $res, 'HASH', 'Returned format ok for ' . $PubApiTest::test_config->{'call'} );

$PubApiTest::test_config->{'test_formdata'} = 'string';
$PubApiTest::test_config->{'formdata'} = 'api.version=0&one&two';
$PubApiTest::test_config->{'call'} = 'whm_api-stringapi0';
$res = $pubapi->whm_api( 'loadavg', 'api.version=0&one&two' );
is( ref $res, 'HASH', 'Returned format ok for ' . $PubApiTest::test_config->{'call'} );

delete $PubApiTest::test_config->{'formdata'};
Expand Down

0 comments on commit 4f84531

Please sign in to comment.